views:

81

answers:

1

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;
   }
  }

 }

}
+1  A: 

My guess here is that you're checking the collision in your carOver_road function. Note that I haven't tested your code, nor tested my solution.

Now you're checking your collision using the following conditional statements:

if (roadless.hitTestPoint(carMC.x - carMC.width / 2, carMC.y,true))

Depending on how much you want to rotate your car when that statement is true, you simply do

    carMC.rotation += num;    //where num is a defined number.

Of course it all depends on where your car is pointing. But you already have all that defined in your function.


Update: based on one of your previous questions, I realize that what you need is basically found on this website. When the car hits the grass, it rotates to bring the player back to the road.

Christopher Richa
@Christopher let num be a random number in the range [0, 360) ;-)
pst
ya you r right it rotates to bring the player back to the road.
ashok patidar