Example 9. Update

The update function is called at each frame.


Similar to previous example, we increase the angle of the sprite. However now, instead of clicking, it will automatically increase.


We increase the angle by 1 each time.


This the javascript file:



 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
// ex09. Update

var game = new Phaser.Game(800, 600, Phaser.AUTO);

var GameState = {

    preload: function() {

        game.load.image("red","images/red.png");
    },

    create: function() {

        // Set backgound color
        game.stage.backgroundColor = "#0FFF0F";
        
        // center coordinates
        var x = game.world.centerX;
        var y = game.world.centerY;

        // Red sprite, rotates around center (anchor point)
        this.red = game.add.sprite(x, y, "red");
        this.red.anchor.setTo(0.5);
    },

    update: function() {
        this.red.angle++;
    }
}

game.state.add('GameState', GameState);
game.state.start('GameState');


The output should be similar to last example.

No comments:

Post a Comment