跟随相机的赛车游戏

时间:2012-12-06 05:36:47

标签: actionscript-3 flash rotation racing

我正在试图弄清楚如何在Flash中创建一个赛车游戏(就像网上已经有很多教程一样)。

然而,不是保持水平静止并移动汽车 - 是否可以让玩家的汽车在屏幕中央保持不动并旋转玩家周围的水平?

2 个答案:

答案 0 :(得分:2)

是的。

你要做的是将汽车放在与其他一切不同的夹子上(你想要移动/旋转的所有东西),然后无论何时你转变(移动/旋转)汽车,你都应用与其他一切相反。

我想象这是自上而下的,所以例如汽车通常会移动的y轴上的每个像素,你现在沿着y轴移动其他所有像素相同的量。

同样,对于你通常会旋转汽车的每个正面度数,你现在用相同度数的负数旋转其他所有物体。

你必须担心的另一件事是创造世界的边缘,因为在你可以使用屏幕的边缘之前,现在你将能够永远地去。

答案 1 :(得分: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