phaser/examples/camera/basic follow.js

73 lines
1.5 KiB
JavaScript
Raw Normal View History

2013-10-22 02:58:20 +00:00
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('background','assets/misc/starfield.jpg');
game.load.image('ufo','assets/sprites/space-baddie.png');
game.load.image('player','assets/sprites/phaser-dude.png');
2013-10-22 02:58:20 +00:00
}
var player;
var fixed;
var cursors;
2013-10-22 02:58:20 +00:00
function create() {
game.add.tileSprite(0, 0, 2000, 2000, 'background');
game.world.setBounds(0, 0, 1400, 1400);
2013-10-22 02:58:20 +00:00
for (var i = 0; i < 100; i++)
2013-10-22 02:58:20 +00:00
{
game.add.sprite(game.world.randomX, game.world.randomY, 'ufo');
}
fixed = game.add.sprite(300, 320, 'player');
fixed.fixedToCamera = true;
fixed.cameraOffset.x = 300;
fixed.cameraOffset.y = 300;
player = game.add.sprite(150, 320, 'player');
2014-02-21 15:53:07 +00:00
player.physicsEnabled = true;
cursors = game.input.keyboard.createCursorKeys();
2013-10-22 02:58:20 +00:00
game.camera.follow(player);
2013-10-22 02:58:20 +00:00
}
function update() {
2014-02-21 15:53:07 +00:00
player.body.setZeroVelocity();
2013-10-22 02:58:20 +00:00
if (cursors.up.isDown)
2013-10-22 02:58:20 +00:00
{
2014-02-21 15:53:07 +00:00
player.body.moveUp(200)
2013-10-22 02:58:20 +00:00
}
else if (cursors.down.isDown)
2013-10-22 02:58:20 +00:00
{
2014-02-21 15:53:07 +00:00
player.body.moveDown(200);
2013-10-22 02:58:20 +00:00
}
if (cursors.left.isDown)
2013-10-22 02:58:20 +00:00
{
player.body.velocity.x = -200;
2013-10-22 02:58:20 +00:00
}
else if (cursors.right.isDown)
2013-10-22 02:58:20 +00:00
{
2014-02-21 15:53:07 +00:00
player.body.moveRight(200);
2013-10-22 02:58:20 +00:00
}
2013-10-22 02:58:20 +00:00
}
function render() {
game.debug.cameraInfo(game.camera, 32, 32);
game.debug.spriteCoords(player, 32, 200);
game.debug.spriteCoords(fixed, 600, 200);
2013-10-22 02:58:20 +00:00
}