赛车比赛碰撞情况

时间:2010-05-20 14:20:04

标签: actionscript-3

在这种情况下如何在汽车与赛道侧发生碰撞时旋转车。

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 个答案:

答案 0 :(得分:1)

我的猜测是你正在检查carOver_road功能中的碰撞。请注意,我没有测试过你的代码,也没有测试过我的解决方案。

现在,您正在使用以下条件语句检查碰撞:

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

根据您在该声明为真时旋转汽车的程度,您只需执行

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

当然这一切都取决于你的汽车指向的位置。但是你已经在你的函数中定义了所有这些。


更新:根据您之前的一个问题,我意识到您需要的内容基本上可以在此website找到。当汽车撞到草地时,它会旋转以使玩家回到路上。