2012-12-06 69 views
1

我想弄清楚如何在Flash中創建一個賽車遊戲(就像網上已經有很多教程)。AS3 - 與以下相機的賽車遊戲

但是不是保持水平靜態和走動的車 - 是否有可能保持玩家的車一動不動在屏幕的中心與繞球員的水平?

謝謝你的時間。

+0

部屋,繼承人什麼你試圖做一個例子,我曾在AS2前一陣子做它,配備了完整的源代碼http://ffiles.com/flash/games/helicopter_game_with_camera_follow_3159.html希望幫助 – joshua

+0

謝謝,但真正棘手的部分是保持玩家的旋轉相同,並旋轉水平。 – PROWNE

+0

只是做同樣的事情,除了動議BG而不是汽車 – joshua

回答

2

這是一個可運行的演示程序,它應該可以做你想做的事;使用上,下,左,右鍵來控制汽車。

重要的東西是在靠近底部的最後一個5個或6行。

package { 
import flash.display.Sprite; 
import flash.events.Event; 
import flash.events.KeyboardEvent; 
import flash.ui.Keyboard; 

public class Untitled2 extends Sprite{ 
    private var level:Sprite = new Sprite(); 
    private var car:Sprite = new Sprite(); 
    private var carVelocityX:Number = 0; 
    private var carVelocityY:Number = 0; 
    private var keyLeft:Boolean; 
    private var keyRight:Boolean; 
    private var keyUp:Boolean; 
    private var keyDown:Boolean; 

    public function Untitled2(){ 
     level.graphics.lineStyle(4, 0xFF0000); 
     level.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight); 

     car.graphics.beginFill(0x000000); 
     car.graphics.moveTo(-5, -5); 
     car.graphics.lineTo(5, -5); 
     car.graphics.lineTo(8, 0); 
     car.graphics.lineTo(5, 5); 
     car.graphics.lineTo(-5, 5); 

     addChild(level); 
     level.addChild(car); 
     car.x = 100; 
     car.y = 100; 

     addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true); 
     stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown, false, 0, true); 
     stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp, false, 0, true); 
    } 

    private function onKeyUp(event:KeyboardEvent):void{ 
     if(event.keyCode == Keyboard.LEFT) 
      keyLeft = false; 
     else if(event.keyCode == Keyboard.RIGHT) 
      keyRight = false; 
     else if(event.keyCode == Keyboard.UP) 
      keyUp = false; 
     else if(event.keyCode == Keyboard.DOWN) 
      keyDown = false; 
    } 

    private function onKeyDown(event:KeyboardEvent):void{ 
     if(event.keyCode == Keyboard.LEFT) 
      keyLeft = true; 
     else if(event.keyCode == Keyboard.RIGHT) 
      keyRight = true; 
     else if(event.keyCode == Keyboard.UP) 
      keyUp = true; 
     else if(event.keyCode == Keyboard.DOWN) 
      keyDown = true; 
    } 

    private function onEnterFrame(event:Event):void{ 
     // Other stuff for controlling the car 
     carVelocityX *= 0.98; 
     carVelocityY *= 0.98; 
     car.x += carVelocityX; 
     car.y += carVelocityY; 

     if(keyLeft) car.rotation -= 7; 
     if(keyRight) car.rotation += 7; 

     var carRotationRadians:Number = car.rotation/180 * Math.PI; 

     if(keyUp){ 
      var speed:Number = Math.sqrt(carVelocityX * carVelocityX + carVelocityY * carVelocityY); 
      speed += 0.1; 
      carVelocityX = Math.cos(carRotationRadians) * speed; 
      carVelocityY = Math.sin(carRotationRadians) * speed; 
     } 

     if(keyDown){ 
      carVelocityX *= 0.8; 
      carVelocityY *= 0.8; 
     } 

     // Add 90 degrees because we want the car pointing up 
     carRotationRadians += Math.PI * 0.5 
     // Rotate the cars position to get its stage coordinates 
     var carX:Number = Math.cos(-carRotationRadians) * car.x - Math.sin(-carRotationRadians) * car.y; 
     var carY:Number = Math.sin(-carRotationRadians) * car.x + Math.cos(-carRotationRadians) * car.y; 
     // Position and rotate the level 
     level.rotation = -car.rotation - 90; // Add 90 degrees because we want the car pointing up 
     level.x = stage.stageWidth * 0.5 - carX; 
     level.y = stage.stageHeight * 0.5 - carY; 
    } 

}// End class Untitled2 

}// End package 
+0

哇!謝謝,那很完美。 – PROWNE

2

是的。

你會想要做的是有車在不同的剪輯一切,只要你來變換(移動/旋轉)(一切你想要移動/旋轉),那麼汽車,你申請與其他一切相反。

我想象這是自上而下的,因此,例如,每個像素高達y軸的車通常會移動,你現在相同的量移動其他一切下來y軸。

同樣地,你通常會旋轉的車隔積極程度,你現在旋轉一切由相同度數的負數。

另一件你不得不擔心的事情就是創造世界的邊緣,因爲在你使用屏幕邊緣之前,現在你將能夠永遠走下去。