A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Is this the most efficient way to display sprites from a sprite sheet?

  1. #1
    5+5=55 Schfifty Five's Avatar
    Join Date
    Jun 2006
    Posts
    698

    Is this the most efficient way to display sprites from a sprite sheet?

    I've got a sprite sheet bitmap in the library that is 2016x1920 pixels in size which is a sheet of 96x96 sprites and I wrote a function to display a specific sprite from the sprite sheet... I'm just wondering if I did it in the most efficient way? I have the swf set to 30 fps and when it's running it'll stay at 30 fps for a few seconds, then drop to around 10 or 15 for a second and then back up to 30. What am I doing wrong?
    Thanks!

    Code:
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    //Sprite sheet in the library with class name SpriteSheet:
    var spriteSheet:BitmapData = new SpriteSheet(2016,1920);
    
    //Display Object to show a sprite from the sprite sheet (each sprite is 96x96):
    var spriteDisplayer:Bitmap = new Bitmap(new BitmapData(96,96),PixelSnapping.ALWAYS,false);
    spriteDisplayer.cacheAsBitmap = true;
    
    //The current rectangle of the sprite sheet to display
    var currentSpriteRect = new Rectangle(0,0,96,96);
    
    //The bitmapData of the current sprite: 
    var currentSpriteBitmapData:BitmapData = new BitmapData(96,96);
    
    //Add the spriteDisplay to the display list:
    stage.addChild(spriteDisplayer);
    
    //Function to display a 96x96 piece of the sprite sheet in the spriteDisplayer bitmap:
    function displaySprite(sourcebmp:BitmapData,column:int,row:int,xPos:Number,yPos:Number):void {
    	currentSpriteRect.x = column*96;
    	currentSpriteRect.y = row*96;
    	currentSpriteBitmapData.copyPixels(sourcebmp,currentSpriteRect,new Point(0,0));
    	spriteDisplayer.bitmapData = currentSpriteBitmapData;
    	spriteDisplayer.x = xPos;
    	spriteDisplayer.y = yPos;
    }
    
    stage.addEventListener(Event.ENTER_FRAME, EnterFrame);
    
    var row:int=0;
    var column:int=0;
    var fpsTimer:Number = getTimer();
    var fps:Number = (getTimer()-fpsTimer)/1000;
    //EnterFrame function calculates fps, displays the current sprite, and increments the row and or column so that it will show a new sprite next frame.
    function EnterFrame(e:Event):void {
    	fps = Math.round(1000/(getTimer()-fpsTimer));
    	//Text field on the stage:
    	_fpstext.text = fps.toString();
    	fpsTimer = getTimer();
    	displaySprite(spriteSheet,row,column,0,0);
    	row++;
    	if (row>20) {
    		row=0;
    		column++;
    		if (column>19) {
    			column=0;
    		}
    	}
    }

  2. #2
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    I could be wrong, but if you have function argument it is being re-created every time function is called. Lets say function accepts bitmapdata object:

    function domagic (myBMD:BitmapData){

    Now every time you call the function, new BitmapData object is created and after function finishes, it is marked for deletion. It is deleted sometimes later when garbage collector kicks in.

    so, maybe you could use static BitmapData object instead of passing new one every time:

    function displaySprite(column:int,row: int,xPos:Number,yPos:Number):void {
    ...
    currentSpriteBitmapData.copyPixels(spriteSheet...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center