In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

SpriteSheet Animation using setVector

Modified code from Lee Brimelow posted here : https://plus.google.com/110495278155587072613/posts/U8oXcET3TE9

Performance example of using .setVector to draw a animation.
Get Adobe Flash player
by leichtgewicht 31 Aug 2011
/**
 * Copyright leichtgewicht ( http://wonderfl.net/user/leichtgewicht )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/y0A5
 */

/**
 * Modified code from Lee Brimelow posted here : https://plus.google.com/110495278155587072613/posts/U8oXcET3TE9
 *
 * Performance example of using .setVector to draw a animation.
 */
package {
    import flash.display.StageQuality;
    import flash.utils.describeType;
    import flash.display.LoaderInfo;
    import flash.display.Bitmap;
    import flash.system.LoaderContext;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.system.Security;
    import flash.display.Loader;
    import flash.net.URLRequest;
    import flash.display.Sprite;
    import flash.geom.Rectangle;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.display.BitmapData;
    
    import net.hires.debug.Stats;

    [SWF(frameRate=60)]
    public class VectorAnimationExample extends Sprite
    {
        
        // Holds all current blit sprites
        private var animations:Vector.<VectorDataAnimation> = new Vector.<VectorDataAnimation>();
        private var numAnimations:int = 0;
        
        public function VectorAnimationExample()
        {
            Security.loadPolicyFile("http://assets.wonderfl.net/crossdomain.xml");

            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.quality = StageQuality.MEDIUM;
            
            // WonderFl Specific loading
            var imageLoader: Loader = new Loader();
            imageLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, init );
            imageLoader.load(  new URLRequest( "http://assets.wonderfl.net/images/related_images/a/aa/aa1e/aa1e38cac5ca15481f555e095be7e4f0c86ac3fa" ), new LoaderContext(true) );
        }
        
        private function init(e:Event):void
        {
            // Initializes the sprite sheet frames
            SnakeData.init(Bitmap(LoaderInfo(e.target).content).bitmapData);
            
            // You gotta track that shit
            addChild(new Stats());
            
            // Start with 50, good to compare
            addAnimations(50);
            
            // When you click the stage you get 10 more
            stage.addEventListener(MouseEvent.CLICK, onClick);
            
            addEventListener(Event.ENTER_FRAME, loop);
        }
        
        protected function onClick(event:MouseEvent):void
        {
            // Create another 10 more
            addAnimations(10);
        }
        
        protected function addAnimations(amount:int):void
        {
            for(var i:int=0; i<amount; ++i)
            {
                var s:VectorDataAnimation = new VectorDataAnimation(SnakeData.snake, SnakeData.maxSize);
                s.x = Math.random() * stage.stageWidth;
                s.y = Math.random() * stage.stageHeight;
                addChildAt(s,0);
                animations.push(s);
            }
            numAnimations = animations.length;
        }

        
        protected function loop(event:Event):void
        {
            // Loop through snakes and render them
            for(var i:int=0; i<numAnimations; ++i)
            {
                var ani: VectorDataAnimation = animations[i];
                ani.rotation += 2.0;
                ani.render();
            }
        }
    }
}

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.geom.Rectangle;
import flash.geom.Point;

 
/* This class is generated from the JSON that was exported from Texture 
       Packer. It contains the rects for all the frames that are in the sprite 
       sheet. They are stored in a static vector. */
class SnakeData {
    
    public static const snake:Vector.<Vector.<uint>> = new Vector.<Vector.<uint>>(20);
    public static const maxSize:Rectangle = new Rectangle();
    public static const rects:Vector.<Rectangle> = new Vector.<Rectangle>(20);
    {
        rects[0] = new Rectangle(0,0,106,62);
        rects[1] = new Rectangle(106,0,104,62);
        rects[2] = new Rectangle(210,0,104,64);
        rects[3] = new Rectangle(314,0,102,64);
        rects[4] = new Rectangle(0,64,100,64);
        rects[5] = new Rectangle(100,64,100,64);
        rects[6] = new Rectangle(200,64,100,64);
        rects[7] = new Rectangle(300,64,100,64);
        rects[8] = new Rectangle(400,64,98,66);
        rects[9] = new Rectangle(0,130,100,64);
        rects[10] = new Rectangle(100,130,100,66);
        rects[11] = new Rectangle(200,130,100,66);
        rects[12] = new Rectangle(300,130,100,66);
        rects[13] = new Rectangle(400,130,100,66);
        rects[14] = new Rectangle(0,196,102,64);
        rects[15] = new Rectangle(102,196,102,64);
        rects[16] = new Rectangle(204,196,104,64);
        rects[17] = new Rectangle(308,196,106,64);
        rects[18] = new Rectangle(0,260,106,62);
        rects[19] = new Rectangle(106,260,106,64);
    }
    
    public static function init(sheet:BitmapData):void
    {
        var rect: Rectangle;
        var i: int;
        for( i = 0; i<rects.length; ++i){
              rect = rects[i];
              if( rect.width > maxSize.width ) {
                  maxSize.width = rect.width;
              }
              if( rect.height > maxSize.height ) {
                  maxSize.height = rect.height;
              }
        }
        
        // Create a proper blipping size;
        const bmp: BitmapData = new BitmapData(maxSize.width, maxSize.height, true, 0);
        const p: Point = new Point();
        
        for( i = 0; i<rects.length; ++i){
              rect = rects[i];
              bmp.copyPixels(sheet, rect, p);
              snake[i] = bmp.getVector(maxSize);
              bmp.fillRect(maxSize, 0);
        }
    }
}

class VectorDataAnimation extends Sprite
{
    // Main Bitmap
    private var bm:Bitmap;
    
    // Current frame as vector
    private var frame:uint;
    
    // Amount Frames
    private var frames:uint;
    
    // Each frame as vector
    private var frameData:Vector.<Vector.<uint>>;
    
    // Size of the Animation bmp
    private var size:Rectangle;
    
    public function VectorDataAnimation(frameData:Vector.<Vector.<uint>>, size:Rectangle)
    {
        this.frameData = frameData;
        this.size = size;
        
        mouseEnabled = false;
        mouseChildren = false;        
        
        frame = Math.random()*frames;
        frames = frameData.length;
        
        // Setup main bitmap
        bm = new Bitmap(new BitmapData(size.width, size.height));
        bm.x -= size.width * 0.5;
        bm.y -= size.height * 0.5;
        bm.smoothing = true;
        
        addChild(bm);            
    }
    
    public function render():void
    {
        // Determine what frame to show next
        ( frame == frames-1) ? frame = 0 : ++frame;
        
        bm.bitmapData.setVector(size, frameData[frame]);
    }
}