ActionScript to move an object
- Started
- Last post
- 10 Responses
- tymeframe
I wasted many hours at work today trying to figure this one out. I simply want to move an object (mc, instance.. whatever), on an X axis, from one position to another.
Can anyone offer a simple solution or tutorial on this?
- flashbender0
the tween class will show you the way.
- tymeframe0
perfecto. thanks!
- pengoboy0
actually I'd install the fuse kit...will change your life.
- dijitaq0
if you're using as3 try this http://code.google.com/p/tweener…
too bad fuse doesn't work on as3
- pengoboy0
Go is under development but soon enough...
- CyBrain0
Some simple scripts (without Penner equations for easing) can be done with a simple setInterval function. Put a movie clip on the stage and put this action script on a frame in the same timeline.
// WHEN THE MOVIE STARTED
var startTime:Number;
// TO BE COMPARED FOR EACH CALL OF m
var now:Number;
// TIME SINCE THE MOVEMENT STARTED
var elapsed:Number;
// HOW LONG YOU WANT THE MOVEMENT TO BE IN MILLISECONDS.
var theDuration:Number = 5.0 * 1000;
// CALL THIS FUNCTION EVERY 30TH OF A SECOND WHICH IS THE FRAME RATE.
m = function() {
// KEEP now UP TO DATE.
now = getTimer();
// USE ELAPSED FOR POSITION.
elapsed = now - startTime;
// POSITION IS THE PERCENTAGE OF TIME PER DURATION. SUBTRACT THE WIDTH OF square_mc TO STOP IT WHILE IT'S AGAINST THE RIGHT EDGE OF THE STAGE.
square_mc._x = elapsed / theDuration * (Stage.width - square_mc._width);
// ALWAYS CLEAR THE INTERVAL WHEN DONE TO PREVENT DRAINING THE PROCESSOR.
if (elapsed >= theDuration) {
clearInterval(mId);
}
}
// IDENTIFY AND START CALLING THE FUNCTION.
mId = setInterval(m, 1000 / 30);
- flashbender0
^ why on earth would you do that instead of using the tween class?
- univers0
If you are using Actionscript 2 this is what I do.
Create Variables that you can use to designate what your X coordinate and Y Coordinate is. The Number designates it will only take numbers which improves code efficiency.
var instanceXloc:Number = 0;
var instanceYloc:Number = 0;Then right under the variables for your X and Y coordinates create a variable as to what speed you want. Higher the number the slower it will go. Vis-a-Vis.
var instancespeed:Number = 0:Now create an onEnterFrame function that will do the tween.
instancename.onEnterFrame = function ()
{
this._parent._x +=(this._parent.instanceXloc - this._x)/this._parent.instancesp...
this._parent._y +=(this._parent.instanceYloc - this._y)/this._parent.instancesp...
}Lets say if you have a button to change the X or Y Coords, this would be how you do it.
mybuttoninstance.onRelease = function ()
{
this._parent.instanceXloc = 100;
}The result is that it changes the instance Xloc varaiable to coordinate umber 100. Then when the script goes through the onEnterFrame, it will re-plug in the Xloc Variable, and cause a smooth movement to x-coordinate 100. Why it does that is that it's taking the desired Xloc Number (Coordinate) then in relationship to where it is currently. _x it will divide itself down so it has a smooth fast to slow moving tween.
I hope this helps!
- elee0
check out tweenlite
http://blog.greensock.com/tweenl…
HUGE timesaver