A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: actionscript movement

  1. #1
    Junior Member
    Join Date
    Mar 2005
    Posts
    29

    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

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    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. -

  3. #3
    Junior Member
    Join Date
    Mar 2005
    Posts
    29
    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

  4. #4
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    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. -

  5. #5
    Junior Member
    Join Date
    Mar 2005
    Posts
    29
    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?

  6. #6
    Member
    Join Date
    May 2007
    Posts
    35
    Quote 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.

  7. #7
    Junior Member
    Join Date
    Mar 2005
    Posts
    29
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center