使用Blitting的AS3动画

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Blitting的AS3动画相关的知识,希望对你有一定的参考价值。

  1. package
  2. {
  3. import flash.display.Bitmap;
  4. import flash.display.BitmapData;
  5. import flash.display.Sprite;
  6. import flash.events.Event;
  7. import flash.geom.Point;
  8. import flash.geom.Rectangle;
  9.  
  10. // Here we declare the properties of our SWF.
  11. [SWF(width="100", height="100", frameRate="20", backgroundColor="0x330066", quality="high", scale="noscale")]
  12. public class Main extends Sprite
  13. {
  14. [Embed(source = "../img/plane.png")] public var Plane:Class; //Take the image plane.png (it contains 3 frames at 32x32) and put it in the Class "Plane".
  15. private var PlaneBmp:Bitmap = new Plane(); //Crate a new Bitmap from it.
  16. private var FinalPlaneBmp:Bitmap = new Bitmap(); //Create an empty Bitmap.
  17. private var PlaneAnimationArr:Array = new Array();
  18.  
  19. private var frameNumber:int; //Here we store the current frame number for blitting.
  20. private var point: Point = new Point(0, 0); //Anchor point, is usually 0,0.
  21. private var rect:Rectangle = new Rectangle(0, 0, 32, 32); //The rectangle that cut the section of the image that we need.
  22.  
  23. public function Main():void {
  24. if (stage) init();
  25. else addEventListener(Event.ADDED_TO_STAGE, init);
  26. }
  27.  
  28. private function init(e:Event = null):void {
  29. removeEventListener(Event.ADDED_TO_STAGE, init);
  30. addEventListener(Event.ENTER_FRAME, update);
  31. addChild(FinalPlaneBmp); //Let's add the empty Bitmap on the stage.
  32. FinalPlaneBmp.smoothing = true;
  33. FinalPlaneBmp.bitmapData = new BitmapData(32, 32); //Fill it's bitmapData with an image 32x32.
  34. for (var i:int = 0; i < 3; i++) {
  35. rect.x = 32 * i; //Shift the rectangle position of 32 every frame, for 3 frames in loop.
  36. PlaneAnimationArr.push(new BitmapData(32, 32)); // Create a new BitmapData in the Array.
  37. PlaneAnimationArr[i].copyPixels(PlaneBmp.bitmapData, rect, point); //Copy the images that is under the rectangle at that position of the PlaneBmp and copy it in the Array.
  38. }
  39. FinalPlaneBmp.x = 50;
  40. FinalPlaneBmp.y = 50;
  41. }
  42.  
  43. private function update(e:Event):void {
  44. frameNumber++;
  45. if (frameNumber >= 3) frameNumber = 0;
  46. FinalPlaneBmp.bitmapData = PlaneAnimationArr[frameNumber];
  47. FinalPlaneBmp.rotation++;
  48. }
  49. }
  50. }

以上是关于使用Blitting的AS3动画的主要内容,如果未能解决你的问题,请参考以下文章

AS3 使用 ENTER_FRAME 对动态创建的影片剪辑进行动画处理

AS3。如何在动画过程中逐步推动敌人?

AS3 多个(影片剪辑按钮)为一个影片剪辑设置动画

as3 flash中的攻击动画问题

获取时间轴动画影片剪辑 AS3 的 y 位置?

ActionScript 3 AS3 Wave动画公式