dcsimg
A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: Moving movie clips across the stage

  1. #1

    Moving movie clips across the stage

    Hello Im using flash mx.

    Im trying to move a bunch of movie clips across the stage when the mouse rolls left and then rolls right. I want the movie clips to move left when the mouse moves right and vice-versa. With a bit of help I got this script but it doesnt seem to work the way I want it to. Is there a easier way of doing this ?

    Script :
    Frame 1:
    Code:
    //movie clip instance is number1
    x1 = getProperty (number1,_x); 
    if (x1 < -50) { 
       setProperty("number1", _x, -50); 
    } 
    if (x1  > 50) { 
       setProperty("number1", _x,  50); 
    }
    Frame2:
    Code:
    mpos = _xmouse; 
    x1 = getProperty (number1,_x); 
    speed = (mpos - 20) / 25; 
    setProperty("number1", _x, x1 - speed); 
    gotoAndPlay("loop");
    A good example of what I want to do can be found here http://www.allamericanrejects.com/home.asp

  2. #2
    Banned NTD's Avatar
    Join Date
    Feb 2004
    Posts
    3,438
    Hi,

    I didn't look at the example like, but here is a quick function for moving an object based on mouse position.......

    code:

    function accelScroller(myTarget) {
    myMiddle = Stage.width/2;
    this.onEnterFrame = function() {
    moveDist = myMiddle-_root._xmouse;
    trace(moveDist);
    myTarget._x += moveDist/10;
    if (myTarget._x>Stage.width) {
    myTarget._x = 0;
    } else if (myTarget._x<0) {
    myTarget._x = Stage.width;
    }
    }
    }
    accelScroller(ball);



    Regards
    NTD

  3. #3
    Almost, I need it to only scroll so far left and stop and so far right and stop. The code you wrote just keeps moving and moving the movie clip. Take a look at the example and you will see what I meen.

    Thanks so much any further help would be great.

  4. #4
    Banned NTD's Avatar
    Join Date
    Feb 2004
    Posts
    3,438
    Change the if statement in the function to control the position of the object......

    code:

    if (myTarget._x>Stage.width) {
    myTarget._x = Stage.width;
    } else if (myTarget._x<0) {
    myTarget._x = 0;
    }


  5. #5
    Thanks but I cant seem to get this to work for more then one movie clip. Is there a way to make this work for multiple movie clips with diferent instences ?

    Thank Again

  6. #6
    Banned NTD's Avatar
    Join Date
    Feb 2004
    Posts
    3,438
    How many movieclips are you talking about? You can definitly extend the function to work with more than one movieclip. However, there are a couple of approaches. If you dont have that many, you can just extend the function. If you have a large number, it might be a better practice to use a for loop to apply the code to all the necessary clips. To extend the function to work with more than one instance, just add more function parameters....... "myTarget,myTarget1,myTarget2"...

  7. #7
    Ok Im using about six different moivie clips. This is what I tried to do with no sucsess.

    Code:
    function accelScroller(myTarget) {
    	myMiddle = Stage.width/2;
    	this.onEnterFrame = function() {
    		moveDist = myMiddle-_root._xmouse;
    		trace(moveDist);
    		myTarget._x += moveDist/50;
    		if (myTarget._x>Stage.width) {
    			myTarget._x = Stage.width;
    		} else if (myTarget._x<190) {
    			myTarget._x = 190;
    		}
    	}
    }
    accelScroller(ball);
    //2nd
    function accelScroller2(myTarget2) {
    	myMiddle2 = Stage.width/2;
    	this.onEnterFrame = function() {
    		moveDist2 = myMiddle-_root._xmouse;
    		trace(moveDist2);
    		myTarget2._x += moveDist2/50;
    		if (myTarget2._x>Stage.width) {
    			myTarget2._x = Stage.width;
    		} else if (myTarget2._x<190) {
    			myTarget2._x = 190;
    		}
    	}
    }
    accelScroller(sky);
    Im not sure how to extend the function in the way suggested.

    it might be a better practice to use a for loop to apply the code to all the necessary clips.
    Would that be a better way of doing it ? If yes how would one do that ?

    Thanks so much

  8. #8
    Banned NTD's Avatar
    Join Date
    Feb 2004
    Posts
    3,438
    Hi,

    You can extend the function to work with more objects like this.....
    code:

    function accelScroller(myTarget1, myTarget2,myTarget3) {
    myMiddle = Stage.width/2;
    this.onEnterFrame = function() {
    moveDist = myMiddle-_root._xmouse;
    trace(moveDist);
    myTarget1._x += moveDist/26;
    myTarget2._x += moveDist/28;
    myTarget3._x += moveDist/30;
    if (myTarget1._x>Stage.width) {
    myTarget1._x = 0;
    } else if (myTarget1._x<0) {
    myTarget1._x = Stage.width;
    }
    if (myTarget2._x>Stage.width) {
    myTarget2._x = 0;
    } else if (myTarget2._x<0) {
    myTarget2._x = Stage.width;
    }
    if (myTarget3._x>Stage.width) {
    myTarget3._x = 0;
    } else if (myTarget3._x<0) {
    myTarget3._x = Stage.width;
    }
    }
    }
    accelScroller(ball, ball2,ball3);



    This was done off the original function I posted and scrolls from one side of the stage to the other for a continuous scroll. You will need to adjust the Stage.width values in the if statement to limit the continuous scroll. You can control the speed of the scroll with the number used as a divisor for "moveDist".

    NTD

  9. #9
    Hey man thanks so much. This is just what I needed.

    BUT

    Im still having troble controling exactly where the movie clip stops. When the mouse moves right I can control where it stops by using this.
    Code:
    function accelScroller(myTarget) {
    	myMiddle = Stage.width/2;
    	this.onEnterFrame = function() {
    		moveDist = myMiddle-_root._xmouse;
    		trace(moveDist);
    		myTarget._x += moveDist/5;
    		if (myTarget._x>Stage.width) {
    			myTarget._x = Stage.width;
    		} else if (myTarget._x<20) {
    			myTarget._x = 20;
    		}
    }
    }
    accelScroller(ball);
    This stops it 20 pixles b4 the left edge of the movie. However I cant seem to get it to stop 20 pixels away from the right edge of the movie, it always stops at 0. Any clues ?

    Also is there a way to make the movie clips decelerate ? Those 2 things would be the iceing on the cake !

    Thanks again ...and again...and again.
    Last edited by billy nugz; 10-25-2005 at 01:45 AM.

  10. #10
    Senior Member
    Join Date
    Apr 2002
    Posts
    131
    Quote Originally Posted by billy nugz
    Hey man thanks so much. This is just what I needed.

    BUT

    Im still having troble controling exactly where the movie clip stops. When the mouse moves right I can control where it stops by using this.
    Code:
    function accelScroller(myTarget) {
    	myMiddle = Stage.width/2;
    	this.onEnterFrame = function() {
    		moveDist = myMiddle-_root._xmouse;
    		trace(moveDist);
    		myTarget._x += moveDist/5;
    		if (myTarget._x>Stage.width-20) {
    			myTarget._x = Stage.width-20;
    		} else if (myTarget._x<20) {
    			myTarget._x = 20;
    		}
    }
    }
    accelScroller(ball);
    This stops it 20 pixles b4 the left edge of the movie. However I cant seem to get it to stop 20 pixels away from the right edge of the movie, it always stops at 0. Any clues ?

    Also is there a way to make the movie clips decelerate ? Those 2 things would be the iceing on the cake !

    Thanks again ...and again...and again.
    Perhaps this (bold part^^)

  11. #11
    Thanks I broke my face trying to figure that simple thing out for days LOL.

    Any gueses on the deceleration part ?

    Thanks so much.

  12. #12
    Banned NTD's Avatar
    Join Date
    Feb 2004
    Posts
    3,438
    Hi,

    For the deceleration part, you would need to define a point where the deceleration is to begin and divide the moveDist value by an increasing number or establish a new variable for the distance remaining and divide that by a constant number in an onEnterFrame event. Give it a try, if I get some time later today I'll give it a closer look. Here is a quick explanation of creating the effect of deceleration.....

    MOTION DECELERATION

    Say you have 2 points: an end point where you want a movie clip to end up, and the current position of the movie clip. If we take the distance between those 2 points and divide by 2, then move that distance, the movie clip has just traveled half the distance to the end point. Now if we take the distance between the new position of the clip and the end point and divide by 2, and move that distance, again we have moved half the distance to that end point. Notice that as the clip gets closer to the end point, half the distance becomes smaller and smaller, thus the motion appears to become "slower", thus creating the illusion of deceleration.
    The actionscript might look something like this:
    code:

    onClipEvent(load) {
    this._x = 100 ;
    endPosx = 250;
    }
    onClipEvent(enterFrame) {
    currentPosx = this._x;
    diffPosx = endPosx - currentPosx;
    movex = diffPosx/2;
    this._x = this._x + movex;
    }


    Again, all this does is find the current x position, find the distance to the end position, divide that distance by 2, add that distance to the current x position. I wrote it out like that so you could see all the steps, however, it could also be written shorter, like this:
    code:

    onClipEvent(load) {
    this._x = 100;
    endPosx = 250;
    }
    onClipEvent(enterFrame) {
    movex = (endPosx - this._x)/2;
    this._x += movex;
    }



    Hope it helps
    NTD

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