Jump to content

Animating a tween with controls


Recommended Posts

So I have a character (sprite) that whenever you press a key it'll move it along the y position and once it reaches the top, send it back down to the bottom.

So I've been doing this

   game.input.keyboard.onDownCallback = function() {
      player.y -= game.height / 4 + 20
      if (player.y < 0) {

I've been doing this. So basically, every key down move the player up and if it goes beyond the screen send it back down.


So I want to add some nice animation to this. So instead of just jumping up, it kind of travels up so I've added a tween,

      currentPosFish = game.height / 4 + 20;
        y: currentPosFish
      }, 1000Phaser.Easing.Quadratic.InOuttrue);


But the thing with this is, it just sets it at the y position. How can I set a tween so it increments like what I was doing above?


Link to comment
Share on other sites

On 06/04/2017 at 4:59 PM, samid737 said:

There are many examples available on Phaser website on how to achieve your animation and here is an example that might be useful to you :



Thanks for the reply but it's not what i'm asking. I know I can animate a tween to some x y position. What I'm asking is if I can animate a tween to alignIn something? like the alignIn method I've used previously so instead of just animating it to some x y pos use alignIn and when it's aligning into something, animate it

Link to comment
Share on other sites

I noticed that within the callBack function you are adjusting the player.y, but for the tween you are passing playerFish,is this correct?

You might be able to use onComplete event  to call a custom function that resets the player using alignIn when the tween is finished animating:



Link to comment
Share on other sites

Not sure about tweening an alignTo call. Could be possible but I don't know the details of how. 

If it isn't possible, then tweening the y property is how you'll have to do it. Is there an easy way to find out what the sprite's y value would be set to by the alignTo call? If there's an easy way to do that, then you could find the resulting y value and tween to that. 

One approach, which may well be horribly inefficient, would be to make an invisible rectangle at your sprite's position, make the alignTo call on it, get the resulting y value, and then tween your sprite to that y value. 


I'm sure there's a better way to handle it, but that was my first thought reading your post. Would love to know how you solve this!

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...