2010-05-20 3 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

안녕하세요. 에디터에서 코드를 들여 쓰면 코드가 제대로 배치됩니다. 내가 너를 고쳤어. –

답변

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] 범위의 임의의 숫자로 지정하십시오. –

+0

예, 오른쪽으로 돌리면 플레이어가 도로로 돌아옵니다. –

관련 문제