Question

Okay so I'm trying to create a program that reads a map from a csv file and then draw each tile using a tilesheet. Reading in the map works fine and I could draw certain tiles depending on the value read in but only if I embedded the images. Obviously this is impractical when it comes to having >20 different tiles; embedding them all just wouldn't be smart.

This is my code for drawing the tiles from the tilesheet.

package  
{
import flash.display.Graphics;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.Point;

public class Tile extends Sprite
{
    [Embed(source="../bin/lib/TileSheet.png")]
    private var BitmapClass:Class;
    private var tileBitmap:Bitmap = new BitmapClass();
    var tileSize = 25;
    var tileSheetLength = 20;
    var sheetColumns:int = tileBitmap.bitmapData.width / tileSize;
    var pt:Point = new Point(0, 0);
    var bmp:Bitmap = new Bitmap(new BitmapData(tileSize, tileSize, true, 0));

        public function Tile(collide:Boolean, id:int) 
        {
            Draw(id);
        }
        private function Draw(id:int):void
        {
            var col:int = id % sheetColumns;
            var row:int = Math.floor(id / sheetColumns);
            var rect:Rectangle = new Rectangle(col * tileSize, row * tileSize, tileSize, tileSize);
            bmp.bitmapData.copyPixels (tileBitmap.bitmapData, rect, pt, null, null, true);
            this.addChild(bmp);
        }

        public function Update():void
        {

        }
}

}
'

So what I need help with is optimising this code so that I can run it around 1,900 times rather than the 910-911 times it can handle right now before just closing without errors. If there is a better way of doing this please let me know and any constructive criticism is always appreciated!

Was it helpful?

Solution

You have a Tile class which has a BitmapClass instance. Perhaps that should be a static property (belonging to the class, not every instance) to begin with. My guess is you're using the memory for the whole tile sheet every since time you instantiate a single tile which you probably don't want to do.

Another thing I'm noticing is you're creating a new BitmapData for each tile, when in fact you probably just need the tile data (it's id/coordinates) so you can copy pixels into the final BitmapData which gets displayed on stage. Perhaps you need to a class to manage resources(embedded bitmaps) and another to manage the different Tile instance(which should hold render data and references to pixels, but shouldn't store the actual data) and copying to the main buffer.

Also, it's a good idea to use BitmapData's lock() and unlock() functions for performance when doing multiple pixel operations on an image.

Have a look at Lee Brimelow's Sprite Sheet tutorials (part 1,2 and especially 3). They're really easy to follow and useful.

Lee Brimelow

Also, it might be worth having a look at the have a look at the GPU accelerated IsoHill library.

IsoHill

I've used IsoHill for a project before and it's quite fast, but it's best to get comfortable with the basics first, otherwise this might seem a bit much.

Hirsch&Mann project for Siemens using IsoHill

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top