Jump to content

Tween a sprite on click


AidanDaniel97
 Share

Recommended Posts

So I have a multiplayer game set up, I had originally coded it so when the player clicks on an X,Y coordinate the server would move the player towards that coordinate on every update. I decided this would be too laggy if the server was under load and opted to use tweening instead. 

I have one issue though... when I click on a coordinate I have the player sprite tween and play an animation but if I click somewhere far away then it speeds up the animation and the sprite gets there quicker? 

example: 

I click 20px away from the player and it plays the whole animation and gets there in the correct amount of time

I click 300px away and it speeds through the animation and really quickly moves to the coordinate

is there any way to stop this? I tired to use game.tweens.frameBased but that has not worked? I also removed physics but that didn't help either

Link to comment
Share on other sites

So I managed to fix this - even though I am unsure if this is a conventional way of doing it! 

 

I used good old maths to find the distance of the two points, then calculated the time it would take if the player was going a certain speed and then set it to the tween's speed! Worked perfectly :) 

distance = Math.hypot(this.destX - this.playerSprite.x, this.destY - this.playerSprite.y);
speed = this.speed;
time = distance / speed;
				
//Add tween
this.tween = game.add.tween(this.playerSprite).to({
    x: this.destX,
	y: this.destY	
},time);
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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