2009-07-15 201 views
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(); 
     }  
    } 
} 

回答