car race game collision condition.
- by ashok patidar
in this how can rotate car when it goes to collied with the track side.
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.text.TextField;
import flash.ui.Keyboard;
import Math;
/**
* ...
* @author Ashok
*/
public class F1race extends MovieClip
{
public var increment:Number = 0; //amount the car moves each frame
public var posNeg:Number = 1;
public var acceleration:Number = .05; //acceleration of the car, or the amount increment gets increased by.
public var speed:Number = 0; //the speed of the car that will be displayed on screen
public var maxSpeed:Number = 100;
public var keyLeftPressed:Boolean;
public var keyRightPressed:Boolean;
public var keyUpPressed:Boolean;
public var keyDownPressed:Boolean;
public var spedometer:TextField = new TextField();
public var carRotation:Number ;
public var txt_hit:TextField = new TextField();
public function F1race()
{
carRotation = carMC.rotation;
trace(carMC.rotation);
//addChild(spedometer);
//spedometer.x = 0;
//spedometer.y = 0;
addChild(txt_hit);
txt_hit.x = 0;
txt_hit.y = 100;
//rotation of the car
addEventListener(Event.ENTER_FRAME, onEnterFrameFunction);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed,false);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased,false);
carMC.addEventListener(Event.ENTER_FRAME, carOver_road)
}
public function carOver_road(event:Event):void {
//trace(texture.hitTestPoint(carMC.x,carMC.y,true),"--");
/* if(!texture.hitTestPoint(carMC.x,carMC.y,true))
{
txt_hit.text = "WRONG WAY";
if(increment!=0)
{
increment=1;
}
}
else
{
txt_hit.text = "";
//increment++;
}*/
if (roadless.hitTestPoint(carMC.x - carMC.width / 2, carMC.y,true))
{
trace("left Hit" + carMC.rotation);
//acceleration = .005;
//if(carMC.rotation>90 || carMC.rotation>90
//carMC.rotation += 2;
if ((carMC.rotation >= 90) && (carMC.rotation <= 180))
{
carMC.rotation += 3;
carMC.x += 3;
}
if ((carMC.rotation <= -90) && (carMC.rotation >= -180))
{
carMC.rotation += 3;
texture.y -= 3;
}
if ((carMC.rotation > -90) && (carMC.rotation <= -1))
{
carMC.rotation += 3;
texture.y -= 3;
}
if(increment<0)
{
increment += 1.5 * acceleration;
}
if(increment>0)
{
increment -= 1.5 * acceleration;
}
}
if (roadless.hitTestPoint(carMC.x + carMC.width / 2, carMC.y,true))
{
trace("left right");
//carMC.rotation -= 2;
if(increment<0)
{
increment += 1.5 * acceleration;
}
if(increment>0)
{
increment -= 1.5 * acceleration;
}
}
if (roadless.hitTestPoint(carMC.x, carMC.y- carMC.height / 2,true))
{
trace("left right");
//carMC.rotation -= 2;
if(increment<0)
{
increment += 1.5 * acceleration;
}
if(increment>0)
{
increment -= 1.5 * acceleration;
}
}
if (roadless.hitTestPoint(carMC.x, carMC.y+ carMC.height / 2,true))
{
trace("left right");
//carMC.rotation -= 2;
if(increment<0)
{
increment += 1.5 * acceleration;
}
if(increment>0)
{
increment -= 1.5 * acceleration;
}
}
if ((!roadless.hitTestPoint(carMC.x - carMC.width / 2, carMC.y, true)) && (!roadless.hitTestPoint(carMC.x, carMC.y- carMC.height / 2,true)) && (!roadless.hitTestPoint(carMC.x, carMC.y+ carMC.height / 2,true)) && (!roadless.hitTestPoint(carMC.x, carMC.y+ carMC.height / 2,true)))
{
//acceleration = .05;
}
}
public function onEnterFrameFunction(events:Event):void
{
speed = Math.round((increment) * 5);
spedometer.text = String(speed);
if ((carMC.rotation < 180)&&(carMC.rotation >= 0)){
carRotation = carMC.rotation;
posNeg = 1;
}
if ((carMC.rotation < 0)&&(carMC.rotation > -180)){
carRotation = -1 * carMC.rotation;
posNeg = -1;
}
if (keyRightPressed) {
carMC.rotation += .5 * increment;
carMC.LWheel.rotation = 8;
carMC.RWheel.rotation = 8;
steering.gotoAndStop(2);
}
if (keyLeftPressed) {
carMC.rotation -= .5 * increment;
carMC.LWheel.rotation = -8;
carMC.RWheel.rotation = -8;
steering.gotoAndStop(3);
}
if (keyDownPressed) {
steering.gotoAndStop(1);
carMC.LWheel.rotation = 0;
carMC.RWheel.rotation = 0;
increment -= 0.5 * acceleration;
texture.y -= ((90 - carRotation) / 90) * increment;
roadless.y = texture.y;
if (((carMC.rotation > 90)&&(carMC.rotation < 180))||((carMC.rotation < -90)&&(carMC.rotation > -180))) {
texture.x += posNeg * (((((1 - (carRotation / 360)) * 360) - 180) / 90) * increment);
roadless.x = texture.x;
}
if (((carMC.rotation <= 90)&&(carMC.rotation > 0))||((carMC.rotation >= -90)&&(carMC.rotation < -1))) {
texture.x += posNeg * ((carRotation) / 90) * increment;
roadless.x = texture.x;
}
increment -= 1 * acceleration;
if ((Math.abs(speed)) < (Math.abs(maxSpeed))) {
increment += acceleration;
}
if ((Math.abs(speed)) == (Math.abs(maxSpeed)))
{
trace("hello");
}
}
if (keyUpPressed)
{
steering.gotoAndStop(1);
carMC.LWheel.rotation = 0;
carMC.RWheel.rotation = 0;
//trace(carMC.rotation);
texture.y -= ((90 - carRotation) / 90) * increment;
roadless.y = texture.y;
if (((carMC.rotation > 90)&&(carMC.rotation < 180))||((carMC.rotation < -90)&&(carMC.rotation > -180))) {
texture.x += posNeg * (((((1 - (carRotation / 360)) * 360) - 180) / 90) * increment);
roadless.x = texture.x;
}
if (((carMC.rotation <= 90)&&(carMC.rotation > 0))||((carMC.rotation >= -90)&&(carMC.rotation < -1))) {
texture.x += posNeg * ((carRotation) / 90) * increment;
roadless.x = texture.x;
}
increment += 1 * acceleration;
if ((Math.abs(speed)) < (Math.abs(maxSpeed))) {
increment += acceleration;
}
}
if ((!keyUpPressed) && (!keyDownPressed)){
/*if (increment > 0 && (!keyUpPressed)&& (!keyDownPressed)) {
//texture.y -= ((90-carRotation)/90)*increment;
increment -= 1.5 * acceleration;
}
if((increment==0)&&(!keyUpPressed)&& (!keyDownPressed))
{
increment = 0;
}
if((increment<0)&&(!keyUpPressed)&& (!keyDownPressed))
{
increment += 1.5 * acceleration;
}*/
if (increment > 0)
{
increment -= 1.5 * acceleration;
texture.y -= ((90 - carRotation) / 90) * increment;
roadless.y = texture.y;
if (((carMC.rotation > 90)&&(carMC.rotation < 180))||((carMC.rotation < -90)&&(carMC.rotation > -180))) {
texture.x += posNeg * (((((1 - (carRotation / 360)) * 360) - 180) / 90) * increment);
roadless.x = texture.x;
}
if (((carMC.rotation <= 90)&&(carMC.rotation > 0))||((carMC.rotation >= -90)&&(carMC.rotation < -1))) {
texture.x += posNeg * ((carRotation) / 90) * increment;
roadless.x = texture.x;
}
}
if (increment == 0)
{
increment = 0;
}
if (increment < 0)
{
increment += 1.5 * acceleration;
texture.y -= ((90 - carRotation) / 90) * increment;
roadless.y = texture.y;
if (((carMC.rotation > 90)&&(carMC.rotation < 180))||((carMC.rotation < -90)&&(carMC.rotation > -180))) {
texture.x += posNeg * (((((1 - (carRotation / 360)) * 360) - 180) / 90) * increment);
roadless.x = texture.x;
}
if (((carMC.rotation <= 90)&&(carMC.rotation > 0))||((carMC.rotation >= -90)&&(carMC.rotation < -1))) {
texture.x += posNeg * ((carRotation) / 90) * increment;
roadless.x = texture.x;
}
}
}
}
public function keyPressed(event:KeyboardEvent):void
{
trace("keyPressed");
if (event.keyCode == Keyboard.LEFT)
{
keyLeftPressed = true;
}
if (event.keyCode == Keyboard.RIGHT)
{
keyRightPressed = true;
}
if (event.keyCode == Keyboard.UP)
{
keyUpPressed = true;
}
if (event.keyCode == Keyboard.DOWN)
{
keyDownPressed = true;
}
}
public function keyReleased(event:KeyboardEvent):void
{
trace("keyReleased...");
//increment -= 1.5 * acceleration;
//increment--;
if (event.keyCode == Keyboard.LEFT)
{
keyLeftPressed = false;
}
if (event.keyCode == Keyboard.RIGHT)
{
keyRightPressed = false;
}
if (event.keyCode == Keyboard.UP)
{
keyUpPressed = false;
}
if (event.keyCode == Keyboard.DOWN)
{
keyDownPressed = false;
}
}
}
}