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 10-15-2004, 01:46 PM   #1
RampnRail
Junior Member
 
Join Date: Jun 2004
Posts: 1
random directions

i was making a quick thing where you could use the arrow keys to drive a car around in an arena and wanted to add in other cars that moved around randomly which you could crash into.
i managed to program the player car,

code:
onClipEvent (enterFrame) {
if (Key.isDown(Key.UP)) {
speed += 2;
} else {
if (Key.isDown(Key.DOWN)) {
speed -= 1;
} else {
speed *= .9;
}
}
if (Math.abs(speed)>25) {
speed *= .6;
}
if (Key.isDown(Key.LEFT)) {
_rotation -= speed;
}
if (Key.isDown(Key.RIGHT)) {
_rotation += speed;
}
speed *= .9;
x = Math.sin(_rotation*(Math.PI/180))*speed;
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
if (!_root.land.hitTest(_x+x, _y+y, true)) {
_x += x;
_y += y;
} else {
speed *= -.5;
}
}



but i cant program the computer car to move randomly based upon the same physics.
working with flash 5, please help

Last edited by RampnRail; 10-15-2004 at 01:57 PM.
RampnRail is offline   Reply With Quote
Old 10-15-2004, 02:28 PM   #2
jbum
Senior Member
 
jbum's Avatar
 
Join Date: Feb 2004
Location: Los Angeles
Posts: 2,920
Here's one way to do it. This uses a random number from 0-4. If 1,2,3,4 the computer is presing an arrow key, and if 0, the computer is doing nothing.

code:

onClipEvent (enterFrame) {
// change action every second
if (lastActionTime == undefined or getTimer() - lastActionTime > 1000)
{
lastActionTime = getTimer();
action = random(5) + 1;
}

if (action == 1) {
speed += 2;
} else {
if (action == 2) {
speed -= 1;
} else {
speed *= .9;
}
}
if (Math.abs(speed)>25) {
speed *= .6;
}
if (action == 3) { // left
_rotation -= speed;
}
if (action == 4)) { // right
_rotation += speed;
}
speed *= .9;
x = Math.sin(_rotation*(Math.PI/180))*speed;
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
if (!_root.land.hitTest(_x+x, _y+y, true)) {
_x += x;
_y += y;
} else {
speed *= -.5;
}
}

__________________
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 10:45 PM.


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

    

Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


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