Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Action scripting to move an object.

  1. May 12, 2009 #1
    I need code to "tween" an object in macromedia flash (actionscript V2.0)
    The code is for a button but the button is not the object to be animated.
    the object is separate. and the button stays in position.
    help?
     
  2. jcsd
  3. May 12, 2009 #2

    minger

    User Avatar
    Science Advisor

    onFrameEvent(enterFrame){
    this._x = this._x + velocity
    }
    or perhaps
    onFrameEvent(enterFrame){
    dx = this._x - targetx
    dy = this._y - targety
    this._x = this._x + (dx/tweenfactor)
    this._y = this._y + (dy/tweenfactor)
    }
    Looking for something like this?
     
  4. May 12, 2009 #3
    i think so but wouldnt
    "this"
    mean that on enterframe the object moves.

    i dont know much about action script
    but i want it triggered by a button, not on entering the frame,

    the button is separte to the movie clip that i want to "tween" to the left and right.
     
  5. May 13, 2009 #4

    minger

    User Avatar
    Science Advisor

    Right, you cannot tween buttons, they are by default stationary and quite unexciting. In fact (at least back in the day), buttons were really only used to monitor mouse interaction. Basically, since you can do much "cooler" stuff with a movie clip than a button, the buttons would actually be movie clips.

    The idea is then you create a button, and set the alpha to 0%. Then, you can say when the invisible button which is on top and the same size as the movie clip is clicked, you tell the movie clip to go to a certain frame, which does something.

    So, in my example, you have an object that you want to move. It has two frames, both with stops on the frame actions. So, at first, it is on the first frame and does nothing.

    Then, when the button is clicked, you tell the movie clip to go to the next frame. On 'that' frame, you have your code that tells it to move per the code I gave a little bit ago.

    Now, what you can also do is tell it to stop moving. So, if you have a lot of moving objects running around, you don't want the ones that aren't moving much to actually be running code (it can slow down the movie/animation). So, you can modify the above cod eto something like:
    Code (Text):

    onFrameEvent(enterFrame){
      dx = this._x - targetx
      dy = this._y - targety
      dist = dx + dy
      if( dist > tolerance) {
        this._x = this._x + dx/tweenfactor
        this._y = this._y + dy/tweenfactor
      } else {
         nextFrame()
        }
    }
     
    So, when the object gets to where you want it, you tell it to go to the next frame, where you have anothe stop, which stops the enterFrame actions from computing.
     
  6. May 17, 2009 #5
    can you give me code for the one frame. because having multiple frames is what i kinda wanted to avoid.
     
  7. May 18, 2009 #6

    minger

    User Avatar
    Science Advisor

    I can, but with a single frame, the object needs to constantly check to see if a condition has been met. You can do this with a simple IF construct. So, let's give the movie clip that is to be moved an instance name of moving_mc. We could have this code on the invisible button:
    Code (Text):

    onMouseRelease(){
      moving_mc.test = 1
    }
     
    Wait, no that won't work. You'll need to declare a frame variable, not on the object. Because the object's variable will update it every frame, so if you initialize it to a value, then try to overwrite it based on a button mouse action, then it will "reset" the next frame.

    To be honest, the easiest way is two frames. Much cleaner and will run faster/better too.
     
  8. May 23, 2009 #7
    its just that i want the viewer to click on the next button and then the object be tweened right into position and when the viewer clicks previous button the object is tween left.
    using the two frames method its sounds like it could get complicated.
     
  9. May 26, 2009 #8

    minger

    User Avatar
    Science Advisor

    On each click you can then simply change the target location for the tween. So, put the target locations on an empty movie clip and use the button to cycle between them. You can either use two frames on the empty movie clip, or use IF statements. So, for instance, name your movie clip instance targetlocs. Then, your code will be slightly modified to:

    Code (Text):

    onFrameEvent(enterFrame){
      dx = this._x - targetloc.targetx
      dy = this._y - targetloc.targety
     
    targetloc will then have two frames which your button changse to change those targetx/targety values.
     
  10. May 30, 2009 #9
    ok but there is 5 sections the viewer is to flip between.
     
  11. Jun 1, 2009 #10

    minger

    User Avatar
    Science Advisor

    5 frames. Use whatever method you're most comfortable with. However, just realize that you do not want manual tweens. If you try to do this manually, then you'll need a specific tween for each motion; or 5! permutations = 120 different tweening actions.

    By simply using code that changes a target, you can let the moving movie clip go where it wants by simply changing the place that it's trying to get to.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Action scripting to move an object.
  1. Python Script (Replies: 2)

Loading...