kadaj
May 22nd, 2009, 03:02 PM
How to move a movie a movieclip in a trajectory?
i.e, how to move a movieclip in a parabolic way using actionscript.
Please help
Thank You.
theCodeBot
May 22nd, 2009, 05:49 PM
How about a parabolic function? ;)
/* In order to choose a starting height and an ending point on the x-axis,
* We have to set the y- and second x-intercepts and do the algrebra.
* We will then use a function to tween position determined by these values.
* I would use TweenLite for this, but seeing as it's so simple,
* It's way less code compiled in the end to do it this way.
*/
var objectToMove:DisplayObject = someClipOnTheStage;
//The object we're moving
var yintercept:Number = 5; //Starting Y coord
var xoffset:Number = 30; //Starting X coord
var xintercept:Number = 200;
var vertexy:Number = 200;
var vertexx:Number = (xintercept+xoffset)/2;
//X and Y coordinates at the peak of the parabola
//Ending X coord when object falls off the screen on the y axis
var time:int = 2000; //Milliseconds it takes to tween
var xstep:Number = 10*(xintercept-xoffset)/time;
//How quickly to step up the x coordinate
var t:Timer = new Timer(10,time/10);
/*
* Every loop of the timer, the x coordinate increases
* by the amount of xstep and the ycoord is set based
* on the below function. Thus, parabolic motion!
*/
//Set initial position of object
objectToMove.x = xoffset;
objectToMove.y = yintercept;
/*
* Here's where that algebra comes in.
* The standard equation of an ellipse that opens down is:
* (x-h)^2 = 4P(y-k)
* Which can be graphed as
* y = ((x-h)^2)/(4P) + k
* Where 4P is the focal width,
* P is negative (because it opens down),
* and H and K are the x- and y-intercepts, respectively.
* We don't have 4P but need it,
* So we input the h and k that we have,
* Then plug in a point we know (the y-intercept)
* And solve for 4P, then simplify so the function
* becomes less ugly.
*/
var fourp:Number = Math.pow(xintercept-vertexx,2)/(0-vertexy);
//Trust me, that algebra works. I think.
function moveObject (e:TimerEvent):void
{
objectToMove.x += xstep;
//The formula described above
objectToMove.y = Math.pow(objectToMove.x-vertexx,2)/fourp + vertexy;
}
//Now that we have that mess set up,
//Setup the timer and start it.
t.addEventListener(TimerEvent.TIMER,moveObject);
t.start();
Please be warned that I did not test this code. I did not import the classes it needs (flash.events.Timer, flash.events.TimerEvent, and flash.display.DisplayObject). I did not put it into a class so you would be able to simple call a function and have all this work done for you. I did not simplify my algebra OR CHECK IT. I did not make any attempt at efficiency.
However, if you import the classes needed, it ought to work without a hitch, albeit it's very ugly code.
kadaj
May 23rd, 2009, 01:20 PM
Perfect!
Thanks You very much.
objectToMove.y = -Math.pow(objectToMove.x-vertexx,2)/fourp + vertexy; is what I wanted.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.