Well… I attempted to use an old script I found on flash-kit (website) on my car for a game…
But the car doesn’t move, it only rotates.
onClipEvent (enterFrame) {
if (Key.isDown(Key.UP)) {
speed += 1;
}
if (Key.isDown(Key.DOWN)) {
speed -= 1;
}
if (Math.abs(speed)>20) {
speed *= .7;
}
if (Key.isDown(Key.LEFT)) {
_rotation -= 15;
}
if (Key.isDown(Key.RIGHT)) {
_rotation += 15;
}
speed *= .98;
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 *= -.6;
}
}
I was hoping that someone who knows actionscript better could help me out?
I also want it to rotate more smoothly, and kind of jerk like a real car would.
Test results with both the collision block and the car script:
The car rotates, but it doesn’t move.
Edit: Found a code based for Flash MX (Not 2004).
onClipEvent (enterFrame) {
//When key UP is pressed, speed is increased
if (Key.isDown(Key.UP)) {
speed += 1;
} else {
//When key DOWN is pressed, speed is decreased
if (Key.isDown(Key.DOWN)) {
speed -= 1;
} else {
//If UP or DOWN aren't pressed then the speed decreases
speed *= .9
}
}
//The car will start to slow down after the speed of 25
if (Math.abs(speed)>25) {
speed *= .6;
}
//Turns the car left
if (Key.isDown(Key.LEFT)) {
_rotation -= speed;
}
//Turns the car right
if (Key.isDown(Key.RIGHT)) {
_rotation += speed;
}
//Moves the car
speed *= .9;
x = Math.sin(_rotation*(Math.PI/180))*speed;
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
if (!_root.move.hitTest(_x+x, _y+y, true)) {
_x += x;
_y += y;
} else {
speed *= -.3;
}
}
[/font]Test results:
The movieclip doesn’t do anything on keypress.
What changes on these need to be made to make it work?
I saw an example of the outcome in a SWF file, and it worked better…
But it won’t work for me.
Edit again:
Fixed:
onClipEvent(load)
{
speed =0 ;
}
Now it works fine:
Edit: The image is broken, the URL is: http://i44.photobucket.com/albums/f43/Person99_please/car.swf
I also have 2 other questions though…
-
How can I have cars chasing the movie clip, like police cars chasing the one seen above?
-
How can I have collision, like you hit a wall, and it brings you to a different frame?
-
How can I have other cars moving around, and if you hit them, you actually HIT them?