I'm attempting to spawn a sprite on a timer (every 2 seconds) and have it move across the screen. I can only manage to move the first one that is spawned, I know it is only doing it because the variable is only set to the first spawned sprite but I'm not sure how to get it to apply to every sprite.
I tried using a group but it would spawn the new sprite at the location of the old one instead of at (0, random).
function preload() {
game.load.image('ball', 'assets/ball.png');
}
var ball;
function create() {
game.physics.startSystem(Phaser.Physics.ARCADE);
ball = game.add.sprite(0, game.world.randomY, 'ball');
game.time.events.repeat(Phaser.Timer.SECOND * 2, 10, createBall, this);
}
function update() {
ball.position.x += 1;
}
function createBall() {
var ball = game.add.sprite(0, game.world.randomY, 'ball');
game.physics.enable(ball, Phaser.Physics.ARCADE);
ball.body.collideWorldBounds = true;
}
