-
actionscript movement
i have this code below, for actionscript 2 of course, and it works fine.
Code:
onClipEvent (load) {
this.speed = 2;
this.distance = 500;
}
onClipEvent (enterFrame) {
if (this._x<this.distance) {
this._x += speed;
}
if (this._x>=(this.distance*0.8)) {
speed -= 0.05;
}
if (speed<0) {
speed = -2;
}
}
basicly, it a circle moves along the screen until it's x co-ordinate is at just before 500 then slows down then stops at 500 and then goes the other way. now after it goes the other way (to the left) i want it to again slow down and stop at a certain point (say 100), how would i go about doing that? also, if anyone could show me how to clean up the code a bit, that would be fantastic. thanks
-
Senior Member
Have you tried to change this line
this.distance = 500;
to
this.distance = 100;
?
- The right of the People to create Flash movies shall not be infringed. -
-
i want the circle to the right until 500, then go left untill 100 and go back to 500 again. sorry i must of not described that clear enough
-
Senior Member
Something like this:
PHP Code:
onClipEvent (load) {
this.speed = 2;
this.distance = 500;
reachedGoal = false;
}
onClipEvent (enterFrame) {
if (this._x < this.distance)
{
if (!reachedGoal)
{
this._x += speed;
}
}
if (reachedGoal)
{
this._x -= speed;
if (this._x <= this.distance - 400)
{
reachedGoal = false;
}
}
if (this._x >= (this.distance * 0.8))
{
speed -= 0.05;
}
if (this._x >= (this.distance))
{
speed -= 0.05;
reachedGoal = true;
}
if (speed < 0)
{
speed = -2;
}
}
- The right of the People to create Flash movies shall not be infringed. -
-
sorry that hasn't seemed to do it, it just keeps traveling to the left and goes way past 100. any idea why it's not working?
-
Originally Posted by darkson01
also, if anyone could show me how to clean up the code a bit, that would be fantastic. thanks
One idea could be to use the fl.transitions.Tween class. It isn't quite as flexible as what you have but it does make things simpler. For example, instead of of specifying a speed you specify the length (time, in seconds) of of the animation.
In your case:
Code:
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.Regular;
var myTween:Tween;
myTween = new Tween(myClip, "x", Regular.easeOut, myClip.x, 500, 1.0, true);
myTween.addEventListener(TweenEvent.MOTION_FINISH, onFinished);
function onFinished(event:TweenEvent):void
{
if (myTween.position == 500)
{
myTween.continueTo(100, 1.0);
}
else if (myTween.position == 100)
{
myTween.continueTo(500, 1.0);
}
}
Change myClip to be the name of your movieclip. You can also change Regular.easeOut to any other easing funtion such as Back.easeOut, Elastic.easeOut, Strong.easeInOut, etc. Just remember to import the right class.
-
thanks that worked fantastic!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|