forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursor key movement.js
73 lines (55 loc) · 1.67 KB
/
cursor key movement.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.stage.backgroundColor = '#007236';
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
game.load.image('phaser', 'assets/sprites/sonic_havok_sanity.png');
}
var cursors;
function create() {
// Modify the world and camera bounds
game.world.setBounds(-1000, -1000, 1000, 1000);
for (var i = 0; i < 100; i++)
{
game.add.sprite(game.world.randomX, game.world.randomY, 'mushroom');
}
// This will create a new object called "cursors", inside it will contain 4 objects: up, down, left and right.
// These are all Phaser.Key objects, so anything you can do with a Key object you can do with these.
cursors = game.input.keyboard.createCursorKeys();
}
function update() {
// For example this checks if the up or down keys are pressed and moves the camera accordingly.
if (cursors.up.isDown)
{
// If the shift key is also pressed then the world is rotated
if (cursors.up.shiftKey)
{
game.world.rotation += 0.05;
}
else
{
game.camera.y -= 4;
}
}
else if (cursors.down.isDown)
{
if (cursors.down.shiftKey)
{
game.world.rotation -= 0.05;
}
else
{
game.camera.y += 4;
}
}
if (cursors.left.isDown)
{
game.camera.x -= 4;
}
else if (cursors.right.isDown)
{
game.camera.x += 4;
}
}
function render() {
game.debug.renderCameraInfo(game.camera, 32, 32);
}