mirror of
https://github.com/photonstorm/phaser
synced 2024-11-24 05:33:35 +00:00
82 lines
1.8 KiB
JavaScript
82 lines
1.8 KiB
JavaScript
|
|
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
|
|
|
|
function preload() {
|
|
|
|
game.load.tilemap('desert', 'assets/tilemaps/maps/desert.json', null, Phaser.Tilemap.TILED_JSON);
|
|
game.load.image('tiles', 'assets/tilemaps/tiles/tmw_desert_spacing.png');
|
|
game.load.image('car', 'assets/sprites/car90.png');
|
|
|
|
}
|
|
|
|
var map;
|
|
var layer;
|
|
|
|
var cursors;
|
|
var sprite;
|
|
var marker;
|
|
|
|
function create() {
|
|
|
|
map = game.add.tilemap('desert');
|
|
|
|
map.addTilesetImage('Desert', 'tiles');
|
|
|
|
layer = map.createLayer('Ground');
|
|
|
|
layer.resizeWorld();
|
|
|
|
marker = game.add.graphics();
|
|
marker.lineStyle(2, 0x00bff3, 1);
|
|
marker.drawRect(0, 0, 32 * 6, 32 * 6);
|
|
|
|
sprite = game.add.sprite(450, 80, 'car');
|
|
sprite.anchor.setTo(0.5, 0.5);
|
|
|
|
game.camera.follow(sprite);
|
|
|
|
cursors = game.input.keyboard.createCursorKeys();
|
|
|
|
|
|
game.input.onDown.add(randomiseTiles, this);
|
|
|
|
}
|
|
|
|
function randomiseTiles() {
|
|
|
|
map.random(layer.getTileX(sprite.x), layer.getTileY(sprite.y), 6, 6);
|
|
|
|
}
|
|
|
|
function update() {
|
|
|
|
marker.x = layer.getTileX(sprite.x) * 32;
|
|
marker.y = layer.getTileY(sprite.y) * 32;
|
|
|
|
sprite.body.velocity.x = 0;
|
|
sprite.body.velocity.y = 0;
|
|
sprite.body.angularVelocity = 0;
|
|
|
|
if (cursors.left.isDown)
|
|
{
|
|
sprite.body.angularVelocity = -200;
|
|
}
|
|
else if (cursors.right.isDown)
|
|
{
|
|
sprite.body.angularVelocity = 200;
|
|
}
|
|
|
|
if (cursors.up.isDown)
|
|
{
|
|
sprite.body.velocity.copyFrom(game.physics.velocityFromAngle(sprite.angle, 300));
|
|
}
|
|
|
|
}
|
|
|
|
function render() {
|
|
|
|
game.debug.renderText('Click to randomise tiles', 32, 32, 'rgb(0,0,0)');
|
|
game.debug.renderText('Tile X: ' + layer.getTileX(sprite.x), 32, 48, 'rgb(0,0,0)');
|
|
game.debug.renderText('Tile Y: ' + layer.getTileY(sprite.y), 32, 64, 'rgb(0,0,0)');
|
|
|
|
}
|