开发者

scrolling background

开发者 https://www.devze.com 2023-02-12 10:16 出处:网络
I\'d like to get a better understanding of how Flash renders display objects, so I have a better idea of how to optimize my games.

I'd like to get a better understanding of how Flash renders display objects, so I have a better idea of how to optimize my games.

I previously created a game that had a scrolling, repeating (and parallaxing) background. I tiled it and cached as bitmap, which was quite a pain when taking animating dis开发者_JAVA技巧play objects in the background into consideration. Is there a better way to do this?

Another question is if I have a large bitmap (or vector art that I have cached as bitmap), is there any kind of performance hit if it is completely off screen? How about if it is partially on screen - does flash have to render the entire bitmap, or just what it sees on screen?


If it's not within a scrollRect it's rendered, even if off-screen. It would perhaps be a good idea to have an ENTER_FRAME listener or something similar to detect when the clip reaches the stage width, then addChild another instance of your background to the end of the scrolling movieclip. Then, for performance, use scrollRect to only render what's on the stage at that current time.


You should only cacheAsBitmap if there is no animation, you are not scaling and not rotating it. Vertical and horizontal movement is fine. Otherwise, every time the MovieClip changes it will need to re-cache it, which costs more than not caching it at all.

If it is off the stage, remove it from the display list or set it's visibility to false.

You can do some good tests with a memory management component. Tweak stuff and see what impact it has to your memory/fps etc. Mrdoob has a really nice one. http://www.as3gamegears.com/profiling/mrdoob-stats/


You have a few options for your scrolling background:

  1. You can add your background assets to the display list, move them around and use scrollRect to clip to the visible area (basically what you have already been doing + scrollRect).

  2. Write your own custom rendering solution that keeps track of game objects state in memory but does not actually add them to the display list. You use a combination of bitmapData.lock(), copyPixels(), and bitmapData.unlock() to render your game objects to screen and you only render the visible area.

  3. Use an pre-existing game library which is already optimized. Someone else mentioned Flixel and that's a good option.

Which one you choose depends on what kind of game it is (raster vs vector) as well as what your goals are (do you want to learn more or do you want to get it done faster?). I recommend trying out #2 at some point if for no other reason than to get an understanding of how the underlying mechanics work.

There are quite a few useful discussions you can find about the difference between options #1 and #2. Here is one of them:

https://gamedev.stackexchange.com/questions/1081/rendering-performance-for-flash-games


Perhaps you could examine how a library like Flixel handles these issues and creates its optimizations.

I'm reasonably certain that memory is freed up at least when a cached bitmap is offscreen completely or hidden but I don't know about partially, I would expect not. Source That article may answer some your query anyway tbh :)

0

精彩评论

暂无评论...
验证码 换一张
取 消