Introducing Beam — Play & Create Interactive Video Shorts. Explore Beam →
var config = { type: Phaser.CANVAS, width: 800, height: 600, backgroundColor: '#2d2d2d', parent: 'phaser-example', pixelArt: true, physics: { default: 'impact', impact: { gravity: 200 } }, scene: { preload: preload, create: create, update: update } }; var game = new Phaser.Game(config); var player; var cursors; function preload () { this.load.tilemapTiledJSON('map', 'assets/tilemaps/maps/impact-tilemap.json'); this.load.image('kenney', 'assets/tilemaps/tiles/kenney.png'); this.load.image('player', 'assets/sprites/phaser-dude.png'); } function create () { var map = this.make.tilemap({ key: 'map' }); var tileset = map.addTilesetImage('kenney'); var layer = map.createStaticLayer(0, tileset, 0, 0); // Any tile with the collides property set to true (in Tiled) will be set to collide layer.setCollisionByProperty({ collides: true }); // Impact collisions with the world work by using slope IDs. An ID of 0 is non-colliding, an ID // of 1 is a solid rectangle, an ID of 2 is a 45 degree upward slope, etc. Check out this visual // reference: // https://github.com/photonstorm/phaser3-examples/blob/master/public/assets/tilemaps/tiles/slopes.png // We need to construct a collision map (of slope IDs) in order to register collisions with a // tilemap layer. We can do this in one of two ways: // Method 1. If we assign the slope IDs as tile properties in Tiled's tileset editor, we can // load them by passing in the property name, e.g. impact-tilemap.json has a property on every // tile called "slope": this.impact.world.setCollisionMapFromTilemapLayer(layer, { slopeProperty: 'slope' }); // Method 2. If we don't have slopes defined in Tiled, we can manually map tile index to slope // ID using an object: // var slopeMap = { 32: 1, 77: 1, 82: 1, 95: 24, 137: 2, 140: 24, 36: 2 }; // this.impact.world.setCollisionMapFromTilemapLayer(layer, { slopeMap: slopeMap }); // Note: the collision map is static! If you remove/change the colliding tiles, it will not be // updated. player = this.impact.add.image(64, 200, 'player'); player.setMaxVelocity(300, 400).setFriction(800, 0); player.body.accelGround = 1200; player.body.accelAir = 600; player.body.jumpSpeed = 300; this.cameras.main.setBounds(0, 0, map.widthInPixels, map.heightInPixels); this.cameras.main.startFollow(player); cursors = this.input.keyboard.createCursorKeys(); var help = this.add.text(16, 16, 'Arrow keys to move. Press "up" to jump.', { fontSize: '18px', fill: '#ffffff' }); help.setScrollFactor(0); } function update (time, delta) { var accel = player.body.standing ? player.body.accelGround : player.body.accelAir; if (cursors.left.isDown) { player.setAccelerationX(-accel); } else if (cursors.right.isDown) { player.setAccelerationX(accel); } else { player.setAccelerationX(0); } if (cursors.up.isDown && player.body.standing) { player.setVelocityY(-player.body.jumpSpeed); } }
var config = {
type: Phaser.CANVAS,
width: 800,
height: 600,
backgroundColor: '#2d2d2d',
parent: 'phaser-example',
pixelArt: true,
physics: {
default: 'impact',
impact: { gravity: 200 }
},
scene: {
preload: preload,
create: create,
update: update
}
};
var game = new Phaser.Game(config);
var player;
var cursors;
function preload ()
{
this.load.tilemapTiledJSON('map', 'assets/tilemaps/maps/impact-tilemap.json');
this.load.image('kenney', 'assets/tilemaps/tiles/kenney.png');
this.load.image('player', 'assets/sprites/phaser-dude.png');
}
function create ()
{
var map = this.make.tilemap({ key: 'map' });
var tileset = map.addTilesetImage('kenney');
var layer = map.createStaticLayer(0, tileset, 0, 0);
// Any tile with the collides property set to true (in Tiled) will be set to collide
layer.setCollisionByProperty({ collides: true });
// Impact collisions with the world work by using slope IDs. An ID of 0 is non-colliding, an ID
// of 1 is a solid rectangle, an ID of 2 is a 45 degree upward slope, etc. Check out this visual
// reference:
// https://github.com/photonstorm/phaser3-examples/blob/master/public/assets/tilemaps/tiles/slopes.png
// We need to construct a collision map (of slope IDs) in order to register collisions with a
// tilemap layer. We can do this in one of two ways:
// Method 1. If we assign the slope IDs as tile properties in Tiled's tileset editor, we can
// load them by passing in the property name, e.g. impact-tilemap.json has a property on every
// tile called "slope":
this.impact.world.setCollisionMapFromTilemapLayer(layer, { slopeProperty: 'slope' });
// Method 2. If we don't have slopes defined in Tiled, we can manually map tile index to slope
// ID using an object:
// var slopeMap = { 32: 1, 77: 1, 82: 1, 95: 24, 137: 2, 140: 24, 36: 2 };
// this.impact.world.setCollisionMapFromTilemapLayer(layer, { slopeMap: slopeMap });
// Note: the collision map is static! If you remove/change the colliding tiles, it will not be
// updated.
player = this.impact.add.image(64, 200, 'player');
player.setMaxVelocity(300, 400).setFriction(800, 0);
player.body.accelGround = 1200;
player.body.accelAir = 600;
player.body.jumpSpeed = 300;
this.cameras.main.setBounds(0, 0, map.widthInPixels, map.heightInPixels);
this.cameras.main.startFollow(player);
cursors = this.input.keyboard.createCursorKeys();
var help = this.add.text(16, 16, 'Arrow keys to move. Press "up" to jump.', {
fontSize: '18px',
fill: '#ffffff'
});
help.setScrollFactor(0);
}
function update (time, delta)
{
var accel = player.body.standing ? player.body.accelGround : player.body.accelAir;
if (cursors.left.isDown)
{
player.setAccelerationX(-accel);
}
else if (cursors.right.isDown)
{
player.setAccelerationX(accel);
}
else
{
player.setAccelerationX(0);
}
if (cursors.up.isDown && player.body.standing)
{
player.setVelocityY(-player.body.jumpSpeed);
}
}