Action scripting to move an object.

  • Thread starter brandy
  • Start date
In summary: However, this can get complicated if you have a lot of elements to animate. In that case, you may want to look into using frames.
  • #1
brandy
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?
 
Technology news on Phys.org
  • #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.
 

Related to Action scripting to move an object.

1. How do I make an object move using action scripting?

In order to make an object move using action scripting, you will need to use the onEnterFrame event handler. Within this event handler, you can use the moveTo function to change the position of the object.

2. Can I control the speed of the object's movement using action scripting?

Yes, you can control the speed of the object's movement by using the setProperty function and changing the _x or _y property values. You can also use the onClipEvent(enterFrame) event handler to continuously update the position of the object at a specific speed.

3. How do I make an object move along a specific path using action scripting?

To make an object move along a specific path, you can use the onEnterFrame event handler to update the position of the object at each frame. Within the event handler, you can use the moveTo function to change the position of the object to a specific coordinate on the path.

4. Is it possible to make an object move based on user interaction using action scripting?

Yes, you can make an object move based on user interaction by using event handlers such as on(mouseDown) or on(keyPress). Within these event handlers, you can use the moveTo function to change the position of the object based on the user's actions.

5. Can I make an object move smoothly using action scripting?

Yes, you can make an object move smoothly by using the onEnterFrame event handler and changing the position of the object by small increments at each frame. You can also use easing functions, such as easeOut or easeInOut, to create a smoother movement effect.

Similar threads

  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
0
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
9
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
6
Views
3K
Back
Top