Return to Snippet

Revision: 36003
at November 16, 2010 04:56 by lorenz82


Updated Code
package 
{    
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Point;
    import flash.geom.Rectangle;
 
    // Here we declare the properties of our SWF.
    [SWF(width="100", height="100", frameRate="20", backgroundColor="0x330066", quality="high", scale="noscale")] 
    public class Main extends Sprite 
    {        
        [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".
        private var PlaneBmp:Bitmap = new Plane();    //Crate a new Bitmap from it.
        private var FinalPlaneBmp:Bitmap = new Bitmap(); //Create an empty Bitmap.
        private var PlaneAnimationArr:Array = new Array();
 
        private var frameNumber:int; //Here we store the current frame number for blitting.
        private var point: Point = new Point(0, 0); //Anchor point, is usually 0,0.
        private var rect:Rectangle = new Rectangle(0, 0, 32, 32); //The rectangle that cut the section of the image that we need.
 
        public function Main():void {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
 
        private function init(e:Event = null):void {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            addEventListener(Event.ENTER_FRAME, update); 
            addChild(FinalPlaneBmp); //Let's add the empty Bitmap on the stage.
            FinalPlaneBmp.smoothing = true;
            FinalPlaneBmp.bitmapData = new BitmapData(32, 32); //Fill it's bitmapData with an image 32x32.
            for (var i:int = 0; i < 3; i++) {
                rect.x = 32 * i; //Shift the rectangle position of 32 every frame, for 3 frames in loop.
                PlaneAnimationArr.push(new BitmapData(32, 32)); // Create a new BitmapData in the Array.
                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.
            }
            FinalPlaneBmp.x = 50;
            FinalPlaneBmp.y = 50;
        }
 
        private function update(e:Event):void {            
            frameNumber++;
            if (frameNumber >= 3) frameNumber = 0;            
            FinalPlaneBmp.bitmapData = PlaneAnimationArr[frameNumber];            
            FinalPlaneBmp.rotation++;
        }        
    }    
}

Revision: 36002
at November 16, 2010 04:43 by lorenz82


Initial Code
package 
{    
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Point;
    import flash.geom.Rectangle;
 
    // Here we declare the properties of our SWF.
    [SWF(width="100", height="100", frameRate="20", backgroundColor="0x330066", quality="high", scale="noscale")] 
    public class Main extends Sprite 
    {        
        [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".
        private var PlaneBmp:Bitmap = new Plane();    //Crate a new Bitmap from it.
        private var FinalPlaneBmp:Bitmap = new Bitmap(); //Create an empty Bitmap.
        private var PlaneAnimationArr:Array = new Array();
 
        private var frameNumber:int; //Here we store the current frame number for blitting.
        private var point: Point = new Point(0, 0); //Anchor point, is usually 0,0.
        private var rect:Rectangle = new Rectangle(0, 0, 32, 32); //The rectangle that cut the section of the image that we need.
 
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }
 
        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            addEventListener(Event.ENTER_FRAME, update);
 
            addChild(FinalPlaneBmp); //Let's add the empty Bitmap on the stage.
            FinalPlaneBmp.smoothing = true;
            FinalPlaneBmp.bitmapData = new BitmapData(32, 32); //Fill it's bitmapData with an image 32x32.
            for (var i:int = 0; i < 3; i++) {
                rect.x = 32 * i; //Shift the rectangle position of 32 every frame, for 3 frames in loop.
                PlaneAnimationArr.push(new BitmapData(32, 32)); // Create a new BitmapData in the Array.
                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.
            }
            FinalPlaneBmp.x = 50;
            FinalPlaneBmp.y = 50;
        }
 
        private function update(e:Event):void {            
            frameNumber++;
            if (frameNumber >= 3) frameNumber = 0;            
            FinalPlaneBmp.bitmapData = PlaneAnimationArr[frameNumber];            
            FinalPlaneBmp.rotation++;
        }        
    }    
}

Initial URL


Initial Description


Initial Title
AS3 Animation using Blitting

Initial Tags
image

Initial Language
ActionScript 3