ActionScript to move an object

Out of context: Reply #9

  • Started
  • Last post
  • 10 Responses
  • 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!

View thread