This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @mixin for-phone-only { | |
| @media (max-width: 599px) { @content; } | |
| } | |
| @mixin for-tablet-portrait-up { | |
| @media (min-width: 600px) { @content; } | |
| } | |
| @mixin for-tablet-landscape-up { | |
| @media (min-width: 900px) { @content; } | |
| } | |
| @mixin for-desktop-up { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * A class that wraps up our 2D platforming player logic. It creates, animates and moves a sprite in | |
| * response to WASD/arrow keys. Call its update method from the scene's update and call its destroy | |
| * method when you're done with the player. | |
| */ | |
| export default class Player { | |
| constructor(scene, x, y) { | |
| this.scene = scene; | |
| // Create the animations we need from the player spritesheet |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // First add object layer via Tiled, then | |
| const spawnPoint = map.findObject("Objects", obj => obj.name === "Spawn Point"); | |
| player = this.physics.add.sprite(spawnPoint.x, spawnPoint.y, "atlas", "player-front"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const config = { | |
| // ... | |
| physics: { | |
| default: 'arcade', | |
| arcade: { | |
| gravity: { y: 0 } // Top down, so no gravity | |
| } | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // setCollision - sets collision on the given tile or tiles within a layer by index | |
| world.setCollision(2); // single tile index | |
| world.setCollision([4,5,6,7,8,12,13,14,15]; // an array of tile indexes | |
| // setCollisionBetween | |
| world.setCollisionBetween(8,96) // start index, stop index | |
| // setCollisionByExclusion | |
| world.setCollisionByxclusion(18,24,54); // An array of tiles to exclude from collisions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function createSlopedTilemapCollider(upwardSlopes, downwardSlopes) { | |
| return function(sprite, tilemapLayer, collideCallback, processCallback, callbackContext) { | |
| this._mapData = tilemapLayer.getTiles(sprite.body.x, sprite.body.y, sprite.body.width, sprite.body.height, true); | |
| if (this._mapData.length === 0) { | |
| return; | |
| } | |
| for (var i = 0; i < this._mapData.length; i++) { | |
| var tile = this._mapData[i]; | |
| var slopeDistanceFromTileTop = 0; | |
| if (upwardSlopes.indexOf(tile.tile.index) >= 0 && sprite.body.right < tile.right) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Math.random() | |
| var rand = Math.random(); | |
| console.log(rand); // Logs something like: 0.45689123537939636 | |
| // Math.random() * n | |
| var randomPower = function(pow) { | |
| return Math.random() * pow; | |
| }; | |
| var rand100 = randomPower(100); | |
| console.log(rand100); // Logs something like 50.48097086345 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // In preload function | |
| this.load.spritesheet('player', | |
| 'assets/dude.png', | |
| { frameWidth: 32, frameHeight: 48 } | |
| ); | |
| // In create function | |
| player = this.physics.add.sprite(100, 450, 'player'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const config = { | |
| type: Phaser.AUTO, | |
| parent: "phaser-example", | |
| width: window.innerWidth, | |
| height: window.innerHeight, | |
| scene: { | |
| preload: preload, | |
| create: create, | |
| update: update | |
| } |
NewerOlder