|
| 1 | + |
| 2 | +var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, render: render }); |
| 3 | + |
| 4 | +function preload() { |
| 5 | + |
| 6 | + game.load.image('ball', 'assets/sprites/pangball.png'); |
| 7 | + |
| 8 | +} |
| 9 | + |
| 10 | +function create() { |
| 11 | + |
| 12 | + game.stage.backgroundColor = '#6688ee'; |
| 13 | + |
| 14 | + // Here we'll create a basic repeating event. |
| 15 | + |
| 16 | + // The way a repeating event works is that it is placed into the queue once, |
| 17 | + // and when it runs its 'repeatCounter' is reduced by 1 and it's moved back into the queue again. |
| 18 | + // To this end the queue will only ever have 1 event actually in it. |
| 19 | + |
| 20 | + // The first parameter is how long to wait before the event fires. In this case 2 seconds (you could pass in 2000 as the value as well.) |
| 21 | + // The second parameter is how many times the event will run in total. Here we'll run it 10 times. |
| 22 | + // The next two parameters are the function to call ('createBall') and the context under which that will happen. |
| 23 | + |
| 24 | + // Once the event has been called 10 times it will never be called again. |
| 25 | + |
| 26 | + game.time.events.repeat(Phaser.Timer.SECOND * 2, 10, createBall, this); |
| 27 | + |
| 28 | +} |
| 29 | + |
| 30 | +function createBall() { |
| 31 | + |
| 32 | + // A bouncey ball sprite just to visually see what's going on. |
| 33 | + |
| 34 | + var ball = game.add.sprite(game.world.randomX, 0, 'ball'); |
| 35 | + |
| 36 | + ball.body.gravity.y = 200; |
| 37 | + ball.body.bounce.y = 0.5; |
| 38 | + ball.body.collideWorldBounds = true; |
| 39 | + |
| 40 | +} |
| 41 | + |
| 42 | +function render() { |
| 43 | + |
| 44 | + game.debug.renderText("Time until event: " + game.time.events.duration.toFixed(0), 32, 32); |
| 45 | + game.debug.renderText("Next tick: " + game.time.events.next.toFixed(0), 32, 64); |
| 46 | + |
| 47 | +} |
0 commit comments