Kalahan Posted February 11, 2015 Share Posted February 11, 2015 I want to make a game where the player is only able to move between 5 positions along the bottom of the canvas using the left and right cursor keys on the keyboard. So the player starts at the central position #2. If he presses the left cursor key he should move to position #1 and if pressing the right cursor key he should move to position #3. After the first move he should be able to move between position #1 and #3. I use the Tween.to method to move my sprite to the specific position (X and Y cordinates) whenever the appropiate key isDown. This works the first time after loading the game. The sprite will move left to position #1 or right to position #3 properly. It will even move from position #3 to #1 and the other way around the way I expect it to. The problem is that after I change the position a third time the sprite moves back to it's original position. After a while back and forth multiple times. Even when I make sure to wait long enough between key presses to make sure the Tween is completed. I just don't understand why it's doing this. After a while it's happening so fast the player sprite is flickering betweens positions #1 and #3 constantly. var game = new Phaser.Game(350, 300, Phaser.AUTO, '', { preload: preload, create: create, update: update }); function preload() {game.load.image('player', 'assets/player.png');} function create() { game.physics.startSystem(Phaser.Physics.ARCADE); position1X = 50position2X = 150position3X = 250var game = new Phaser.Game(350, 300, Phaser.AUTO, '', { preload: preload, create: create, update: update }); function preload() {game.load.image('player', 'assets/player.png');} function create() { game.physics.startSystem(Phaser.Physics.ARCADE); positionY = 250position1X = 50position2X = 150position3X = 250 player = game.add.sprite(position2X, positionY, 'player');playerTween = game.add.tween(player); cursors = game.input.keyboard.createCursorKeys(); } function update() { if (cursors.left.isDown){ playerTween.to({x: position1X, y: positionY}, 60); playerTween.start();} else if (cursors.right.isDown){ playerTween.to({x: position3X, y: positionY}, 60); playerTween.start(); } } player = game.add.sprite(position2X, positionY, 'player');playerTween = game.add.tween(player); cursors = game.input.keyboard.createCursorKeys(); } function update() { if (cursors.left.isDown){ playerTween.to({x: position1X, y: positionY}, 60); playerTween.start();} else if (cursors.right.isDown){ playerTween.to({x: position3X, y: positionY}, 60); playerTween.start(); } } How can I fix this? Should I use tweens to move the player? Or should I use another method like sprite.moveToXY? Thanks in advance! Link to comment Share on other sites More sharing options...
rich Posted February 11, 2015 Share Posted February 11, 2015 The problem with using isDown inside the update loop is that almost certainly you're firing off a whole load of tweens. You need to add a check in there to ensure that another tween isn't create if one is already running (tween.isRunning will let you do this). Link to comment Share on other sites More sharing options...
Recommended Posts