2013-11-07 06:10:15 +00:00
|
|
|
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update });
|
2013-11-06 16:46:21 +00:00
|
|
|
|
|
|
|
function preload() {
|
|
|
|
|
|
|
|
game.load.image('phaser', 'assets/sprites/phaser-dude.png');
|
2013-11-07 06:10:15 +00:00
|
|
|
game.load.tilemap('desert', 'assets/maps/burd.json', null, Phaser.Tilemap.TILED_JSON);
|
|
|
|
game.load.tileset('tiles', 'assets/maps/ground_1x1.png', 32, 32);
|
|
|
|
game.load.spritesheet('trees', 'assets/maps/walls_1x2.png', 32, 64);
|
2013-11-06 16:46:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-07 06:10:15 +00:00
|
|
|
var map;
|
|
|
|
var tileset;
|
|
|
|
var layer;
|
|
|
|
|
2013-11-06 16:46:21 +00:00
|
|
|
var sprite;
|
|
|
|
var group;
|
|
|
|
var oldY = 0;
|
2013-11-07 06:10:15 +00:00
|
|
|
var cursors;
|
2013-11-06 16:46:21 +00:00
|
|
|
|
|
|
|
function create() {
|
|
|
|
|
2013-11-07 06:10:15 +00:00
|
|
|
// Create our tilemap to walk around
|
|
|
|
map = game.add.tilemap('desert');
|
|
|
|
tileset = game.add.tileset('tiles');
|
|
|
|
layer = game.add.tilemapLayer(0, 0, 800, 600, tileset, map, 0);
|
2013-11-06 16:46:21 +00:00
|
|
|
|
2013-11-07 06:10:15 +00:00
|
|
|
// This group will hold the main player + all the tree sprites to depth sort against
|
2013-11-06 16:46:21 +00:00
|
|
|
group = game.add.group();
|
|
|
|
|
2013-11-07 06:10:15 +00:00
|
|
|
// The player
|
2013-11-06 16:46:21 +00:00
|
|
|
sprite = group.create(300, 200, 'phaser');
|
2013-11-07 04:31:37 +00:00
|
|
|
|
2013-11-07 06:10:15 +00:00
|
|
|
// Some trees
|
|
|
|
for (var i = 0; i < 50; i++)
|
2013-11-06 16:46:21 +00:00
|
|
|
{
|
2013-11-07 06:10:15 +00:00
|
|
|
var x = game.math.snapTo(game.world.randomX, 32);
|
|
|
|
var y = game.math.snapTo(game.world.randomY, 32);
|
|
|
|
group.create(x, y, 'trees', game.rnd.integerInRange(0, 8));
|
2013-11-06 16:46:21 +00:00
|
|
|
}
|
|
|
|
|
2013-11-07 06:10:15 +00:00
|
|
|
// Move it
|
|
|
|
cursors = game.input.keyboard.createCursorKeys();
|
2013-11-06 16:46:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function update() {
|
|
|
|
|
|
|
|
sprite.body.velocity.x = 0;
|
|
|
|
sprite.body.velocity.y = 0;
|
|
|
|
|
2013-11-07 06:10:15 +00:00
|
|
|
if (cursors.left.isDown)
|
2013-11-06 16:46:21 +00:00
|
|
|
{
|
|
|
|
sprite.body.velocity.x = -200;
|
|
|
|
}
|
2013-11-07 06:10:15 +00:00
|
|
|
else if (cursors.right.isDown)
|
2013-11-06 16:46:21 +00:00
|
|
|
{
|
|
|
|
sprite.body.velocity.x = 200;
|
|
|
|
}
|
|
|
|
|
2013-11-07 06:10:15 +00:00
|
|
|
if (cursors.up.isDown)
|
2013-11-06 16:46:21 +00:00
|
|
|
{
|
|
|
|
sprite.body.velocity.y = -200;
|
|
|
|
}
|
2013-11-07 06:10:15 +00:00
|
|
|
else if (cursors.down.isDown)
|
2013-11-06 16:46:21 +00:00
|
|
|
{
|
|
|
|
sprite.body.velocity.y = 200;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sprite.y !== oldY)
|
|
|
|
{
|
2013-11-07 06:10:15 +00:00
|
|
|
// Group.sort() is an expensive operation
|
|
|
|
// You really want to minimise how often it is called as much as possible.
|
|
|
|
// So this little check helps at least, but if you can do it even less than this.
|
|
|
|
group.sort();
|
|
|
|
oldY = sprite.y;
|
2013-11-06 16:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|