如何为精灵设置动画?

时间:2009-07-15 03:34:51

标签: flash actionscript-3 flux

我正在使用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();
        }       
    }
}

1 个答案:

答案 0 :(得分:3)

您可以使用补间库,例如TweenLite,这可能是最简单的方法。如果你想在Actionscript中学习动画的细节,那么这本书Actionscript 3.0 Animation by Keith Peters是首屈一指的。