weratius Posted July 3, 2015 Share Posted July 3, 2015 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 movementFor 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 More sharing options...
weratius Posted July 3, 2015 Author Share Posted July 3, 2015 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 More sharing options...
LeeZolait Posted April 25, 2016 Share Posted April 25, 2016 Is there an other way to do tween with constant velocity for the sprite regardless the distance ? Link to comment Share on other sites More sharing options...
drhayes Posted April 26, 2016 Share Posted April 26, 2016 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 More sharing options...
Recommended Posts