A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [as3] Fastest way for Flipping tiles horiz, vert, sprite sheets

  1. #1
    Senior Member
    Join Date
    Nov 2005
    Posts
    192

    [as3] Fastest way for Flipping tiles horiz, vert, sprite sheets

    The tilesheets I use only have top/left facing sets and I assume that you have to implement your own flipping functions or at least I think they are flipped, not rotated.

    This is my current drawing function that does NOT flip. It gets information from a tile array, then copies a 16x16 tile from a spriteSheet onto the main bitmapdata. Tiles in the spriteSheet are numbered linearly from 0 and this number can be turned into row/col coordinates of the sheet with simple division.
    Code:
    		private var rect:Rectangle = new Rectangle(0, 0, tileSize, tileSize);
    		private var p:Point = new Point();
    override public function redraw() {
    			var t:TimeStuff = new TimeStuff();
    			var data:Array = currentMap.getData();
    			var sheets:Array = currentMap.getSheets()
    			var w:int = currentMap.getMapW();
    			var h:int = currentMap.getMapH();
    			//for (var z:int = 0; z < 20;z++){		
    			for (var y:int = 0; y < h; y++) {
    				for (var x:int = 0; x < w; x++) {
    					var tile:Tile = data[0][y][x];
    					var sheet:int = tile.sheet;
    					rect.x = sheets[sheet].getTileCol(tile.sheetNumber) * tileSize; 
    					rect.y = sheets[sheet].getTileRow(tile.sheetNumber) * tileSize;
    					p.x = x * tileSize;
    					p.y = y * tileSize;
    					layers[0].bitmapData.copyPixels(sheets[sheet].getBMD(), rect, p);
    					
    				}
    			}
    			//}
    			//layers[0].bitmapData.unlock();
    			trace("t:" + t.timeElapsed());
    		}
    Right now flipping is pretty expensive time wise. For example, doing the commented 20x loop above takes the same time it takes to flip every tile on the screen individually only once, mainly because I have to:

    1. get original tile from sprite sheet
    2. flip it onto a separate bitmapData with draw and edit: transformation matrix
    3. return the flipped bitmapData
    4. copyPixel the flipped bitmapData onto the screen

    code for above... similar except rect does not change and getTile instead of getBMD is used:
    Code:
    		public function redraw2() {
    			var t:TimeStuff = new TimeStuff();
    			var data:Array = currentMap.getData();
    			var sheets:Array = currentMap.getSheets()
    			var w:int = currentMap.getMapW();
    			var h:int = currentMap.getMapH();
    			//for (var z:int = 0; z < 50;z++){		
    			for (var y:int = 0; y < h; y++) {
    				for (var x:int = 0; x < w; x++) {
    					var tile:Tile = data[0][y][x];
    					var sheet:int = tile.sheet;
    					p.x = x * tileSize;
    					p.y = y * tileSize;
    					layers[0].bitmapData.copyPixels(sheets[sheet].getTile(tile.sheetNumber, 0), rect, p);
    					
    				}
    			}
    			//}
    			trace("t:" + t.timeElapsed());
    		}
    flipping or no flipping code below
    Code:
    public function getTile(number:int, flip:int):BitmapData{
    			var row:int=number/width;
    			var col:int=number % width;
    			rect.y = row * tileSize;
    			rect.x = col * tileSize;
    			bmdt.copyPixels(bmd, rect, p);
    			if (flip == 0) {
    				bmdt2.draw(bmdt, flipVerticalMatrix);
    				return bmdt2;//flipped
    			}
    			return bmdt;//nonflipped
    		}
    I don't think I can optimize the flipping much more without doing something completely different... such as copying the sprite sheet then flipping each tile individually(in code). Then it would just use a different sheet to copy if it needs to be flipped. The tradeoff is the memory of the spritesheet around 20-50 kb and the additional loading time for the map, which I guess shouldn't be too bad?

    Anyone have any ideas? Cropping/creating right/bot facing tiles from the sets in photoshop is the last thing I want to do.
    Last edited by Flyingcow; 07-02-2009 at 07:49 PM.

  2. #2
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    err, why not use transformation matrix or simple scaleX?
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  3. #3
    Senior Member
    Join Date
    Nov 2005
    Posts
    192
    BitmapData.draw() uses a transformation matrix. I guess I didn't really make that clear
    bmdt2.draw(bmdt, flipVerticalMatrix);

    scaleX doesnt work with bitmapDatas

  4. #4
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    why not make a copy of your sprite sheet, flip it at the start of the game and just substitute the flipped handle when you wanna draw the flipped sprite for flips sake!
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  5. #5
    Senior Member
    Join Date
    Nov 2005
    Posts
    192
    Quote Originally Posted by BlinkOk View Post
    why not make a copy of your sprite sheet, flip it at the start of the game and just substitute the flipped handle when you wanna draw the flipped sprite for flips sake!
    I thought that this was gonna be the only solution. I'll see how long it takes to make horizontal/vertical of everything... the lazy way to do it... if it's too long ill have to add stuff to my map data about what needs to be flipped.

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