0

So created a Sprite to which I add other Sprites which are game tiles. Each tile is 60 x 60 px big. In result I've the Sprite with about 200 childs (those tiles). When I try to startDrag() the container sprite the lag when moving it is very noticeable..

Is there a way like to join the tile Sprites so the container would have only 1 child Sprite instead of 200? Because it lags so much probably cause it needs to move (change the x and y) all those 200 tiles.. Am I correct?

In this case I can't use the cacheAsBitmap property, cause user can zoom in or zoom out the map..

Rihards
  • 10,241
  • 14
  • 58
  • 78

3 Answers3

3

Glycerine & Aurel do touch the crux of the real solutions. However I'd like to add.

You are correct by the way, when you said it has to manage so many sprite locations when you move the container around. CacheAsBitmap sure does does tackle this to great extent but the real solution is blitting. Try this link for that :

http://www.adobe.com/devnet/flash/articles/blitting_mc.html

It doesn't matter if a user zoom or something of that sort is required cause you can always switch between bitmap data & the original vector sprites. Your problem arises in moving.managing lotsa sprites, so just before doing that use optimizations, after that let them be back to their selves.

loxxy
  • 12,990
  • 2
  • 25
  • 56
1

I've had the same issue before. Is it possible to 'join' them together - in a sense.

When you add your 200 sprites onto a screen - I assume you put them all into another parent sprite.

A this point - you will take a snapshot, or a screenshot - or a photographic replica (whatever you want to call it) of all the sprites and write the image (bitmapData) to a parent sprite. At this point. delete/remove/hide/nullify the original sprites and you'll be left with a sprite containing bitmap data.

One big image to move about and zooming and the like is no bother.

If you need code - ask. It's time consuming code so you tell me first then I'll write it :P

Glycerine
  • 7,157
  • 4
  • 39
  • 65
  • Hmm, snapshot idea is good too. Anyway, if i create the whole map as bitmap i need to create it in 3 sizes (zoomed in once, zoomed in twice and the normal sized one) and keep those somewhere cause zooming the normal bitmap will make it look ugly when zoomed in.. but I believe that is not a problem.. – Rihards Nov 16 '10 at 13:27
  • naw... Zooming is fine. Cache the final product as bitmap, allow smoothing - when zooming in FP 9.5 and above it'll look smooth. My favorite trick is to use Z camera rather than scaling. As it uses GPU acceleration. Tell you what I'll build an example tonight- remind me and I'll do it. – Glycerine Nov 16 '10 at 14:56
0

Hm, joining them would actually be quite hard... You would need to get the graphics, the code and all and put that into the parent...

I don't think that is the problem - you should do something else... In this case, I think that by "tiles" you mean that the parent would be a tile map, correct? So, you probably have a 2-dimensional array (array of arrays) with tile types - instead of parsing that array at initialization, creating A LOT of Sprites, try re-parsing it in each frame (it is faster), but add only the Sprites that are possible to see. That is - their X position (after adding the zoom and camera X) is greater than -sprite.width, where the height is also scaled by the zoom, and lesser than stage.width + sprite.width (again, width after zoom). Same goes for Y, only with the height attributes.

Aurel Bílý
  • 7,068
  • 1
  • 21
  • 34
  • Yes, tile map is the parent. But when moving the tile map sprite how to do all that stuff to display the sprite when they should be visible? The map, each tile, can have items on it and basically it's complicated. ;D – Rihards Nov 13 '10 at 16:39
  • You have to store the display items in an array. Most likely an array containing just Objects defining the desired Sprite. Then - loop through the whole array, selecting only the items that can be seen. From these objects make the sprites and position them... Simple. – Aurel Bílý Nov 13 '10 at 17:22