Be a Supporter!

need help with movie clip movement

  • 248 Views
  • 6 Replies
New Topic Respond to this Topic
Stefan987654123
Stefan987654123
  • Member since: Dec. 26, 2010
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
need help with movie clip movement 2011-05-20 20:46:31 Reply

I was just wondering how to move a movie clip so that it goes in a random order.. For example, i don't want it always to go in a certain pattern, i want it to go a different direction every 5 seconds, but to stay in the boundries of the stage...

Torrunt
Torrunt
  • Member since: May. 3, 2007
  • Offline.
Forum Stats
Member
Level 22
Programmer
Response to need help with movie clip movement 2011-05-20 23:43:18 Reply

Use a timer with a delay time of 5 seconds and in the event handler for it just make a random number and depending on the number, change the direction.


BBS Signature
swishcheese
swishcheese
  • Member since: May. 12, 2007
  • Offline.
Forum Stats
Member
Level 14
Programmer
Response to need help with movie clip movement 2011-05-20 23:53:10 Reply

At 5/20/11 08:46 PM, Stefan987654123 wrote: I was just wondering how to move a movie clip so that it goes in a random order.. For example, i don't want it always to go in a certain pattern, i want it to go a different direction every 5 seconds, but to stay in the boundries of the stage...

seeing your other post I know your using AS2...

I was bored so I decided to write you a tutorial, this tutorial is for coding write inside the movieClip as I saw you doing in other post:

The main idea is setting 2 different variables, for x and y, to random numbers. Based on those random numbers move the player. Also you must keep him inside the stage. Now we begin. Then changing the random numbers every 5 seconds.

onClipEvent(load){
	var xspeed = 0;
	var yspeed = 0;
	var counter = 0;
	var xsign = 0;
	var ysign = 0;
}

this is for setting up the variables.

xspeed will be the number added to the movieClips x position and yspeed will be the number added to the movieClips y position. The counter will keep track of time like a clock. and xsign and ysign will determine if we are moving the x position to the left or right, or if we are moving the y position to the up or down.

Now lets work on functions that will calculate the speed and random number:

onClipEvent(enterFrame){

function calcRandomNum(min,max){
	var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
    	return randomNum;
}

	function calcRandomSpeed(){
		
		xsign = calcRandomNum(0,1);
		ysign = calcRandomNum(0,1);
		
		if(xsign == 0){
			xspeed = calcRandomNum(0,3);
		}else{
			xspeed = -calcRandomNum(0,3);
		}
		
		if(ysign == 0){
			yspeed = calcRandomNum(0,3);
		}else{
			yspeed = -calcRandomNum(0,3);
		}
	}
}

calcRandomNum(min,max) will return a number between what ever min and max are. It does this by using the as2 random() function. calcRandomNum(1,3) will produce the results 1,2, or 3. So now we have a random generating function.

calcRandomSpeed() will use the random function above to produce a random speed. xsign and ysign will either be 0 or 1. 0 represents if the related speed will be positive and 1 represents that the related speed will be negative, this number is produced randomly using the calcRandomNum function. If x sign is 0 (or positive) it will set xspeed to be a random number between 0, 3 using the random function. If x sign is 1 (or negative) it will set xspeed to be a random number between 0, 3 using the random function, then it will change its sign to be negative. If xspeed is negative the player will move left, If xspeed is positive the player will move right. yspeed is calculated the same way that xspeed is, but if yspeed is negative the player will move up, If yspeed is positive the player will move down.
Now we can calculate a random speed, next we have to move the player, and keep him constricted in the stage area, this following code will also go in the enterFrame event:

if(counter == 0 || counter%(24*5) == 0){
		calcRandomSpeed();		
	}
	
	counter++;

Okay, now the first if statement calculates when five seconds is up then runs the function to calculate a new random speed. Also, if counter is zero, the movie just began, and we need to calculate a random speed. counter is increased by one every time through a frame. '%' is called the modulus operatio and calculates the remainder of two divided numbers, so 5%2 will equal 1. the 24 in the if statement is the frame rate of the movie (mine is 24) , if counter is 24 the movie wnet on for 1 second, so if you multiply 24 by 5 you get 120, so when counter is 120 it will be 5 seconds in and we need to calculate a new random speed. So if counter is 120, 120%(24*5) will equal zero, 5 seconds later counter will be 140, so 140%(24*5) will also equal zero, so when counter%(24*5) = 0 we need to calculate a new random speed.

Sweet, were almost there, now we have when to calculate a new random speed, now he has got to move, this also goes in the enterFrame event:

if(xspeed > 0 && this._x+this._width/2 < 550){
		this._x += xspeed;
	}else if(xspeed > 0) {
		xspeed = -1*xspeed;
	}
	if (xspeed < 0 && this._x-this._width/2 > 0){
		this._x += xspeed;
	}else if(xspeed < 0) {
		xspeed = -1*xspeed;
	}
	
	if(yspeed > 0 && this._y+this._height/2 < 400){
		this._y += yspeed;
	}else if(yspeed > 0) {
		yspeed = -1*yspeed;
	}
	
	if (yspeed < 0 && this._y-this._height/2 > 0){
		this._y += yspeed;
	}else if(yspeed < 0) {
		yspeed = -1*yspeed;
	}

Okay, Im only going to explain the change in xspeed, becuase the stuff I say will be the same for yspeed.

first we must calculate wether xspeed is negative or positive, because if the player is on the right edge of the stage and xspeed is positive, if we move the player xspeed, he will go off the stage, which We dont want to allow. But if xspeed is negative and on the right edge, he wont go off the stage, so we want to allow this. So we create two if statements seeing if xspeed is positive or negative. Now we want to restrict.
so if xspeed is positivehe will go off the right side of the stage. So on the first if state we want to also check to see if the players position is less than the right side x position. If these both are true then we can add xspeed to the players x position. Now, if you leave it like this, the player will hit the right edge and just stay on the right edge, like its glue to it until 5 more seconds is up (and even then if it calculates a positive speed it will stay there) so if the player isnt less then the right edge then we want to change the sign of the xspeed to get it off the edge. We do this by multipling xspeed by -1. THis same process use if xspeed is negative, or if yspeeed is positive or negative.

AWESOME, now It works like a charm, but one thing to think about, when calculating the random speed, xspeed and yspeed have the potenial of both being zero, if this happens the player will stand still, you may change this by change the min number to calculate to 1 or something but then the player cant go directly north, south, east, or west, so I added a little something to make it seem like the player isnt dead:

if(xspeed == 0 && yspeed == 0){
		this._rotation += 10;
	}else if(this._rotation != 0){
		this._rotation += 10;
	}

this simply checks if both speeds are zero, if they are it add 10 to its rotation spinning the character around until the five seconds is up, then when it change it evens itself out back to a zero rotation. This would be added in the enterFrame event as well.

Well, here is an example, and it can be modfied to fit your needs, hope this help :P
-Travis


BBS Signature
swishcheese
swishcheese
  • Member since: May. 12, 2007
  • Offline.
Forum Stats
Member
Level 14
Programmer
Response to need help with movie clip movement 2011-05-20 23:57:15 Reply

Ohhh, and here is the .swf example of this code.


BBS Signature
Stefan987654123
Stefan987654123
  • Member since: Dec. 26, 2010
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to need help with movie clip movement 2011-05-21 14:14:38 Reply

I did everything you said to do, but it's just staying still(the movie clip) this is all i put in the actions thing for the mc

onClipEvent(load){
var xspeed = 0;
var yspeed = 0;
var counter = 0;
var xsign = 0;
var ysign = 0;
}

onClipEvent(enterFrame){

function calcRandomNum(min,max){
var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
return randomNum;
}

function calcRandomSpeed(){

xsign = calcRandomNum(0,1);
ysign = calcRandomNum(0,1);

if(xsign == 0){
xspeed = calcRandomNum(0,3);
}else{
xspeed = -calcRandomNum(0,3);
}

if(ysign == 0){
yspeed = calcRandomNum(0,3);
}else{
yspeed = -calcRandomNum(0,3);
if(counter == 0 || counter%(24*5) == 0){
calcRandomSpeed();
}

counter++;
if(xspeed > 0 && this._x+this._width/2 < 550){
this._x += xspeed;
}else if(xspeed > 0) {
xspeed = -1*xspeed;
}
if (xspeed < 0 && this._x-this._width/2 > 0){
this._x += xspeed;
}else if(xspeed < 0) {
xspeed = -1*xspeed;
}

if(yspeed > 0 && this._y+this._height/2 < 400){
this._y += yspeed;
}else if(yspeed > 0) {
yspeed = -1*yspeed;
}

if (yspeed < 0 && this._y-this._height/2 > 0){
this._y += yspeed;
}else if(yspeed < 0) {
yspeed = -1*yspeed;
}
}
}
}

so, do you know what i did wrong?

swishcheese
swishcheese
  • Member since: May. 12, 2007
  • Offline.
Forum Stats
Member
Level 14
Programmer
Response to need help with movie clip movement 2011-05-21 17:51:11 Reply

if(ysign == 0){
yspeed = calcRandomNum(0,3);
}else{
yspeed = -calcRandomNum(0,3);
if(counter == 0 || counter%(24*5) == 0){
calcRandomSpeed();
}

should be:

if(ysign == 0){
yspeed = calcRandomNum(0,3);
}else{
yspeed = -calcRandomNum(0,3);
}
if(counter == 0 || counter%(24*5) == 0){
calcRandomSpeed();
}


BBS Signature
swishcheese
swishcheese
  • Member since: May. 12, 2007
  • Offline.
Forum Stats
Member
Level 14
Programmer
Response to need help with movie clip movement 2011-05-21 17:54:42 Reply

ops but then you have to close it again with another }

your calcRandomNum function brackets are wrong it should be like:

function calcRandomSpeed(){
		
		xsign = calcRandomNum(0,1);
		ysign = calcRandomNum(0,1);
		
		if(xsign == 0){
			xspeed = calcRandomNum(0,3);
		}else{
			xspeed = -calcRandomNum(0,3);
		}
		
		if(ysign == 0){
			yspeed = calcRandomNum(0,3);
		}else{
			yspeed = -calcRandomNum(0,3);
		}
	}

then after that the counter stuff.


BBS Signature