Jump to content

How to set the constant speed to Tween animation (not total duration) ?


weratius
 Share

Recommended Posts

Hello!

I have a tween animation:

var speed = (playerInfoObject.engine.speed != null) ? playerInfoObject.engine.speed * 15 : 0;var tween1 = game.add.tween(myShip).to({ x: galaxyGate.x, y: galaxyGate.y}, speed, Phaser.Easing.Linear.None, true);

This is an animation for a ship movement

For example:

I have a big tileSprite (space). When the tween starts in different points of this tileSprite the ship's speed is different too.

 

I need to make it the same from all it's starts points.

 

I have already tried smth. but that didn't help me =(

var position = {x: myShip.x, y: myShip.y};        var target = {x: galaxyGate.x, y: galaxyGate.y};         var speed = 5000;        var counter = 0;        var timePrev = Date.now();        var tween = game.add.tween(myShip)        .to(target , speed)         .onUpdateCallback(function(){            myShip.x = myShip.x;            myShip.y = myShip.y;            counter++;            if(counter === 100){                tween.stop();                var timeNow = Date.now();                var timeDiff = timeNow - timePrev;                speed = speed - timeDiff;                tween.to( target, speed)                tween.start();            }        })        .start();

Thank you in advance!!!

Link to comment
Share on other sites

That's funny!
But I have solved this problem like this:

var distance = Math.sqrt(Math.pow((myShip.x - galaxyGate.x), 2) + Math.pow((myShip.x - galaxyGate.x), 2));var time = (playerInfoObject.engine.speed != null) ? distance / playerInfoObject.engine.speed : 0;time = time * 1000;var tween1 = game.add.tween(myShip).to({ x: galaxyGate.x, y: galaxyGate.y}, time, Phaser.Easing.Linear.None, true);
Link to comment
Share on other sites

  • 9 months later...

At that point, why not use velocity?

If you are going to use a tween, you'll have to do the physics calculations yourself. Find out the distance the sprite has to travel, figure out how fast you want it to move, and get the time out of that like this:

var distance = 200; // or whatever distance you calculate.
var desiredSpeed = 50; // that's in pixels per second, more or less.
var time = distance / desiredSpeed; // this number is the number of seconds.

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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