3

我正在使用 Flex 和 AS3 尝试创建游戏,我想知道如何轻松地制作动画。我更喜欢使用精灵表图像。我要去头并发布代码(它真的很短)我到目前为止,有人可以看看它并告诉我添加动画支持的最佳/最简单/最简单的方法吗?先感谢您。

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
  styleName = "plain"
    xmlns="cyanprime.*" 
    layout="absolute"
    width="600"
    height="400"
    frameRate="100"
    applicationComplete="initApp()">

    <mx:Script>
        <![CDATA[
            public function initApp():void
            {
                stage.addEventListener(KeyboardEvent.KEY_UP, keyUp); 
                stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown); 
                Mouse.hide();
                canvas.init();

            }

            private function keyDown(event:KeyboardEvent):void
            {
                canvas.KeyDownHandler(event);
            }

            private function keyUp(event:KeyboardEvent):void
            {
                canvas.KeyUpHandler(event);
            }
        ]]>
    </mx:Script>

  <MyGameCanvas id="canvas" width="100%" height="100%" themeColor="#ff0000" />
</mx:Application>

...

package cyanprime{

    import mx.core.UIComponent;
    import mx.controls.Image;
    import flash.events.*;
    import flash.utils.*;
    import flash.display.*;
    import flash.ui.Keyboard;

    public class MyGameCanvas extends UIComponent{
        [Embed(source="player.gif")]
        private var playerImage:Class;
        private var playerSpeed:int = 5;
        private var keys:Array = new Array();
        private var player:DisplayObject = new playerImage();
        private var ticker:Timer;



        public function init():void{
            // set up player
            player.x = 50;
            player.y = 50;
            addChild(player);

            for(var i:int = 0; i < 300; i++)
            {
                keys[i] = false;
            }

            ticker = new Timer(10); 
            ticker.addEventListener(TimerEvent.TIMER, onTick);
            ticker.start();

        }

        public function controls():void{
            if(keys[Keyboard.RIGHT])
                player.x += playerSpeed;

            if(keys[Keyboard.LEFT])
                player.x -= playerSpeed;

            if(keys[Keyboard.UP])
                player.y -= playerSpeed;

            if(keys[Keyboard.DOWN])
                player.y += playerSpeed;
            }

        public function KeyDownHandler(event:KeyboardEvent):void{   
            keys[event.keyCode] = true;

        }

        public function KeyUpHandler(event:KeyboardEvent):void{
            keys[event.keyCode] = false;
        }

        public function onTick(evt:TimerEvent):void {
            controls();
        }       
    }
}
4

1 回答 1

3

您可以使用TweenLite之类的补间库,这可能是最简单的方法。如果您想了解 Actionscript 中动画制作的具体细节,Keith Peters 所著的 Actionscript 3.0 Animation一书是首屈一指的。

于 2009-07-15T03:41:49.683 回答