Action scripting to move an object.

  • Thread starter brandy
  • Start date
  • #1
161
0
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?
 

Answers and Replies

  • #2
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?
 
  • #3
i think so but wouldn't
"this"
mean that on enterframe the object moves.

i don't 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.
 
  • #4
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:
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.
 
  • #5
can you give me code for the one frame. because having multiple frames is what i kinda wanted to avoid.
 
  • #6
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:
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.
 
  • #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.
 
  • #8
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:
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.
 
  • #9
ok but there is 5 sections the viewer is to flip between.
 
  • #10
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.
 

Suggested for: Action scripting to move an object.

Replies
2
Views
265
Replies
1
Views
753
Replies
2
Views
82
Replies
2
Views
788
Replies
11
Views
807
Replies
1
Views
674
Replies
6
Views
549
Back
Top