1

我正在尝试弄清楚如何在 Flash 中创建赛车游戏(就像网络上已经存在的许多教程一样)。

然而,与其保持关卡静止并移动汽车 - 是否可以让玩家的汽车在屏幕中央保持静止并围绕玩家旋转关卡?

4

2 回答 2

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
于 2012-12-06T17:40:12.590 回答
2

是的。

您要做的是将汽车与其他所有东西(您想要移动/旋转的所有东西)放在不同的剪辑上,然后每当您要变换(移动/旋转)汽车时,您将逆向应​​用到所有东西别的。

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

同样,对于您通常将汽车旋转的每个正度数,您现在将其他所有部件旋转相同度数的负数。

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

于 2012-12-06T05:43:25.670 回答