Posted By

lorenz82 on 11/16/10


Tagged

image as3 bitmap blitting


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

lorenz82
weavermedia
tspitzr
tombarc
pepperpunk


AS3 Animation using Blitting


 / Published in: ActionScript 3
 

  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. }

Report this snippet  

You need to login to post a comment.