|
1 | 1 |
|
2 | 2 | var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); |
3 | 3 |
|
4 | | -var baddie; |
5 | | -var keys = Phaser.Keyboard; |
6 | | - |
7 | 4 | function preload() { |
8 | 5 |
|
9 | 6 | game.load.image('background','assets/misc/starfield.jpg'); |
10 | | - game.load.image('ufo','assets/sprites/ufo.png'); |
11 | | - game.load.image('baddie','assets/sprites/space-baddie.png'); |
| 7 | + game.load.image('ufo','assets/sprites/space-baddie.png'); |
| 8 | + game.load.image('player','assets/sprites/phaser-dude.png'); |
12 | 9 |
|
13 | 10 | } |
14 | 11 |
|
| 12 | +var player; |
| 13 | +var fixed; |
| 14 | +var cursors; |
| 15 | + |
15 | 16 | function create() { |
16 | 17 |
|
17 | 18 | game.add.tileSprite(0, 0, 2000, 2000, 'background'); |
18 | 19 |
|
19 | | - game.world.setBounds(0, 0, 1400,1400); |
| 20 | + game.world.setBounds(0, 0, 1400, 1400); |
20 | 21 |
|
21 | | - for (var i = 0; i < 10; i++) |
| 22 | + for (var i = 0; i < 100; i++) |
22 | 23 | { |
23 | 24 | game.add.sprite(game.world.randomX, game.world.randomY, 'ufo'); |
24 | 25 | } |
25 | 26 |
|
26 | | - baddie = game.add.sprite(150, 320, 'baddie'); |
| 27 | + fixed = game.add.sprite(300, 320, 'player'); |
| 28 | + fixed.fixedToCamera = true; |
| 29 | + fixed.cameraOffset.x = 300; |
| 30 | + fixed.cameraOffset.y = 300; |
| 31 | + |
| 32 | + player = game.add.sprite(150, 320, 'player'); |
| 33 | + |
| 34 | + cursors = game.input.keyboard.createCursorKeys(); |
27 | 35 |
|
28 | | - game.camera.follow(baddie); |
| 36 | + game.camera.follow(player); |
29 | 37 |
|
30 | 38 | } |
31 | 39 |
|
32 | 40 | function update() { |
33 | 41 |
|
34 | | - baddie.body.velocity.setTo(0, 0); |
| 42 | + player.body.velocity.setTo(0, 0); |
35 | 43 |
|
36 | | - if (game.input.keyboard.isDown(keys.LEFT) && !game.input.keyboard.isDown(keys.RIGHT)) |
| 44 | + if (cursors.up.isDown) |
37 | 45 | { |
38 | | - baddie.body.velocity.x = -155; |
| 46 | + player.body.velocity.y = -200; |
39 | 47 | } |
40 | | - else if (game.input.keyboard.isDown(keys.RIGHT) && !game.input.keyboard.isDown(keys.LEFT)) |
| 48 | + else if (cursors.down.isDown) |
41 | 49 | { |
42 | | - baddie.body.velocity.x = 155; |
| 50 | + player.body.velocity.y = 200; |
43 | 51 | } |
44 | | - else if (game.input.keyboard.isDown(keys.UP) && !game.input.keyboard.isDown(keys.DOWN)) |
| 52 | + |
| 53 | + if (cursors.left.isDown) |
45 | 54 | { |
46 | | - baddie.angle = 90; |
47 | | - baddie.body.velocity.y = -155; |
| 55 | + player.body.velocity.x = -200; |
48 | 56 | } |
49 | | - else if (game.input.keyboard.isDown(keys.DOWN) && !game.input.keyboard.isDown(keys.UP)) |
| 57 | + else if (cursors.right.isDown) |
50 | 58 | { |
51 | | - baddie.angle = 90; |
52 | | - baddie.body.velocity.y = 155; |
| 59 | + player.body.velocity.x = 200; |
53 | 60 | } |
| 61 | + |
54 | 62 | } |
55 | 63 |
|
56 | 64 | function render() { |
57 | 65 |
|
58 | 66 | game.debug.renderCameraInfo(game.camera, 32, 32); |
| 67 | + game.debug.renderSpriteCoords(player, 32, 200); |
| 68 | + game.debug.renderSpriteCoords(fixed, 600, 200); |
| 69 | + |
| 70 | + game.debug.renderSpriteCoords(game.world._container, 32, 400); |
59 | 71 |
|
60 | 72 | } |
0 commit comments