To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Search this Thread Display Modes
Old 09-19-2004, 07:09 PM   #1
polnareff
Actionscripter
 
Join Date: Jan 2004
Location: Montreal, Quebec
Posts: 128
Random movement direction

Hey all,
I was playing around with a big patch of code that I read about before in one of the other threads here. It basically made it so that a movie clip bounces back when it collides with the boundaries of the movie.

Here is the complete code:

code:
onClipEvent (load) {
// when the clip loads, give it the folloing properties
speedX = 15;
// horizontal speed
speedY = 15;
// vertical speed
score1 = 0;
// score for player 1 is 0
score2 = 0;
// score for player 2 is 0
}
onClipEvent (enterFrame) {
// when a new frame is reached ...
// remeber that the x-axis is horizontal
this._X += speedX;
// give it horizontal velocity X
this._y += speedY;
// give it vertical velocity Y
if (this._X>=485) {
// if the mc reg point is at or equal to 485
speedX = -speedX;
// reverse velocity X
} else if (this._X<=15) {
// else if mc reg is at or equal to 15
speedX = -speedX;
// reverse velocity X
}
// remeber that the y-axis is vertical
if (this._Y>=285) {
// if mc reg is at or equal to 285
speedY = -speedY;
// reverse velocity Y
} else if (this._Y<=15) {
// else if mc reg is at or equal to 15
speedY = -speedY;
// reverse velocity Y
}
}



Now I am using a circle for my movie clip, and I was wondering why it always starts by moving in the same direction. I don't see any part of the code that specifies what angle it would start moving...

Any help would be great,
Thanks.
polnareff is offline   Reply With Quote
Old 09-19-2004, 07:17 PM   #2
pellepiano
Senior Member
 
pellepiano's Avatar
 
Join Date: Feb 2000
Location: Stockholm, Sweden
Posts: 15,137
This is the direction...

speedX = 15;
// horizontal speed
speedY = 15;
__________________

-Pelle Piano
// Image Gallery
www.studiobild.com
// Photo Blog
http://talesofthepixel.blogspot.com
pellepiano is offline   Reply With Quote
Old 09-19-2004, 10:16 PM   #3
polnareff
Actionscripter
 
Join Date: Jan 2004
Location: Montreal, Quebec
Posts: 128
Yeah, but that also changes the speed of the ball, so isn't there a way to make it so that I can have a seperate set of coordinates for just the direction it will move?
polnareff is offline   Reply With Quote
Old 09-19-2004, 11:09 PM   #4
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
If you use sine and cosine, you can create a velocity for the ball at a particular angle and speed.

speed = 10; // set desired speed
angle = random(360); // set desired angle

speedX = Math.cos(angle*Math.PI/180)*speed;
speedY = Math.sin(angle*Math.PI/180)*speed;

In my own programs, I typically use the variable names vx and vy instead of speedX and speedY, to more clearly indicate that these represent velocity in a particular direction.

If you'd like to figure out the current speed and angle from speedX, and speedY, there's a way to do that to (using Math.atan2 and Math.sqrt).
__________________
jbum is offline   Reply With Quote
Old 09-19-2004, 11:48 PM   #5
polnareff
Actionscripter
 
Join Date: Jan 2004
Location: Montreal, Quebec
Posts: 128
I see, but do you mind explaining to my exactly what cosine and sine are? I've heard of them, just never really knew what they were used for Thanks.
polnareff is offline   Reply With Quote
Old 09-20-2004, 02:09 AM   #6
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
Kind of hard to explain in a paragraph - but basically, they provide a way to convert a radius and an angle to an x,y position on the edge of a circle.

Given a circle whose center is cx,cy and whose radius is r, and given an angle a,
then the position corresponding to that angle on the edge of the circle is given by

x = cx+cos(a)*r
y = cy+sin(a)*r

some folks switch the cos/sin around, and may or may not negate the y value - it all depends on where you want to start on the circle and which direction you rotate it (clockwise or counter-clockwise).

In the following movie, sine is shown as a blue line, and cosine as a green line. Drag the mouse around inside the circle to play with them.

Sine/Cosine Illustration


However, to use these functions, the angle, often expressed as a number from 0 - 360 degrees, must be converted to radians, which go from 0 - 2*PI. And yes, this has something to do with the fact that the circumfrence of a circle is equal to 2*PI*r).

To convert degrees to radians, multiply by PI/180, which is what I did in my original example.

I've left out a lot of info, which you will find in any trigonometry text, or you might want to search the Physics forum.



- Jim
__________________

Last edited by jbum; 09-20-2004 at 02:29 AM.
jbum is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:03 PM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.