如何为精灵制作动画?

发布于 2024-07-26 19:41:57 字数 2821 浏览 8 评论 0原文

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

I'm using Flex and AS3 to try and create a game, and I'm wondering how I can animate things easily. I would prefer to use sprite sheet images. I'm going to go a head and post the code (It's really short) I got so far, can someone look over it and tell me the best/simplest/easiest way to add animation support? Thank you in advance.

<?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();
        }       
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨的味道风的声音 2024-08-02 19:41:57

您可以使用补间库,例如 TweenLite,这可能是最简单的方法。 如果您想了解 Actionscript 中动画的具体细节,请阅读 Actionscript 3.0 动画一书基思·彼得斯是首屈一指的。

You can use a tween library such as TweenLite, which is probably the easiest way. If you want to learn the nuts and bolts of animating in Actionscript, the book Actionscript 3.0 Animation by Keith Peters is second to none.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文