2

我正在尝试制作“Achtung!die kurve” - 类似于 Actionscript 3.0 中的游戏。

到目前为止,我已经完成了其中一个“蠕虫”的运动,并且效果很好。

我想把它分成不同的类文件,但我似乎无法让它工作。我有一个 .fla 文件和 4 个 .as 文件。到目前为止,我只有其中两个中的代码。当我将所有代码直接放入 fla 文件时,它工作得非常好,但是当我将它放入我的播放器类时,它不会“添加孩子”到舞台上。

这是我的播放器 .as 文件: http: //pastebin.com/hZSxT9cu

我之所以评论 addChild,是因为我想在我的 mainFile.as

这是来自主文件: http: //pastebin.com/TAwUYrTU 我不太确定如何在主文件中调用它,所以我猜这将是问题所在。

所有文件都被命名并正确放置在同一个文件夹中,flash 说我的每个 .as 文件都针对 .fla 文件,所以这似乎不是问题。

任何帮助深表感谢。

此外,如果有人知道我可以如何改变我的精灵的速度,那就太棒了!似乎也无法使其正常工作。

最好的问候杰斯珀

4

1 回答 1

1

查看您引用的游戏,很明显您将需要一些碰撞检测。本关于像素级碰撞检测的教程可能有助于您入门。

使用您当前的设置,我认为提高绘图速度的唯一方法是提高 fla 的帧速率。

您不能将播放器实例作为子类添加到主类中,因为目前它无权访问它。实际上,在你的玩家类中创建多个精灵是非常低效的。一种更有效的方法是直接在播放器实例的图形实例中绘制矩形。

我认为以下内容可能是您尝试做的一个很好的起点。再次,我包含了一些评论,希望能解释我在做什么。

将此设置为您的 fla 中的文档类:

package  {

    import flash.display.MovieClip;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;

    public class MainTrink extends MovieClip {

        // define a list of players, their color, and controls
        // this should make it easier to vary the number of players
        private var players:Array = [
            {color: 0xff0000, left: Keyboard.A, right: Keyboard.S},
            {color: 0x0000ff, left: Keyboard.O, right: Keyboard.P},
            {color: 0x00ff00, left: Keyboard.NUMBER_1, right: Keyboard.NUMBER_2},
            {color: 0xffff00, left: Keyboard.LEFT, right: Keyboard.RIGHT},
        ];

        public function MainTrink() {

            var playerData:Object;
            var player:PlayerTrink;

            // Create a player instance for each player specified in the players array
            for (var i:int = 0; i < players.length; i ++) 
            {
                playerData = players[i];

                player = new PlayerTrink();
                player.color = playerData.color;
                player.init();

                this.addChild(player);

                // Add a reference to the instance back onto the players
                // list so that we can access it easily in other methods
                playerData.player = player;
            }

            // add listeners for keyboard events
            this.stage.addEventListener(KeyboardEvent.KEY_DOWN, _onKeyDown);
            this.stage.addEventListener(KeyboardEvent.KEY_UP, _onKeyUp);
        }

        private function _onKeyDown(event:KeyboardEvent):void
        {
            var playerData:Object;
            var player:PlayerTrink;

            // Check whether pressed key is a control for one of our players
            for (var i:int = 0; i < players.length; i ++)
            {
                playerData = players[i];
                player = PlayerTrink(playerData.player);

                if (event.keyCode == playerData.left)
                    player.leftKeyDown = true;

                if (event.keyCode == playerData.right)
                    player.rightKeyDown = true;
            }
        }

        private function _onKeyUp(event:KeyboardEvent):void
        {
            var playerData:Object;
            var player:PlayerTrink;

            for (var i:int = 0; i < players.length; i ++)
            {
                playerData = players[i];
                player = PlayerTrink(playerData.player);

                if (event.keyCode == playerData.left)
                    player.leftKeyDown = false;

                if (event.keyCode == playerData.right)
                    player.rightKeyDown = false;
            }
        }
    }

}

并将此文件包含在与 fla 相同的目录中:

    package  {

    // Need to import other classes referenced within this class
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.KeyboardEvent;
    import flash.events.Event;
    import flash.ui.Keyboard;

    // By convention classes are usually named using CamelCase
    public class PlayerTrink extends MovieClip {

        // Move your properties out of your class constructor
        private const TURNSPEED:Number = 0.1;

        private var posX:Number = 50;
        private var posY:Number = 50; 
        private var dirX:Number = 1;
        private var dirY:Number = 1;
        private var dir:Number = 0;

        private var _leftKeyDown:Boolean = false;
        private var _rightKeyDown:Boolean = false;

        private var _color:Number;

        public function PlayerTrink() {
            // constructor code
        }

        public function init():void
        {
            this.addEventListener(Event.ENTER_FRAME, _onEnterFrame);
        }

        private function _onEnterFrame(e:Event):void {
            if(_leftKeyDown) dir -= TURNSPEED;
            if(_rightKeyDown) dir += TURNSPEED;
            dirX = Math.cos(dir);
            dirY = Math.sin(dir);
            posX += dirX;
            posY += dirY;
            drawRect(posX, posY); 
        }

        private function drawRect(x:Number, y:Number):void {
            this.graphics.beginFill(_color);
            this.graphics.drawRect(x, y, 4, 4);
            this.graphics.endFill();
        }                           

        public function set leftKeyDown(value:Boolean):void 
        {
            _leftKeyDown = value;
        }

        public function set rightKeyDown(value:Boolean):void 
        {
            _rightKeyDown = value;
        }                   

        public function set color(value:Number):void 
        {
            _color = value;
        }        
    }   
}
于 2012-04-13T22:53:41.990 回答