2010-05-20 128 views
0

在這個如何可以旋轉汽車時,它走向與賽道側。賽車遊戲碰撞條件

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

歡迎來到SO。如果你在編輯器中縮進代碼,它會很容易地找到它。我已經爲你解決了這個問題。 – 2010-05-20 14:22:29

回答

1

我的猜測是你正在檢查你的carOver_road函數中的碰撞。請注意,我沒有測試過您的代碼,也沒有測試過我的解決方案。

現在你用下面的條件語句檢查你的碰撞:

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

取決於你多麼想旋轉你的車的時候這種說法是真實的,你根本就

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

當然,這一切都取決於你的車在哪裏指向。但是你已經擁有了你的函數中定義的所有東西。


更新:根據你以前的一個問題,我知道你需要什麼這個website基本上找到。當汽車撞上草地時,它會旋轉,使玩家回到路上。

+0

讓num爲在[0,360)範圍內的隨機數;-) – 2010-05-20 19:02:51

+0

如果您正確旋轉將玩家帶回馬路。 – 2010-05-21 04:25:55