object array mobile development
Im currently creating a tile-based game for android. Using java via dalvik JVM. im fretting over a decision to represent objects in a particular map. should i use an id based map (2 dimensional integer array) and place game logic in a separate function in the game engine, or create an object array (2 dimensional array of game objects) and store game logic within the class methods. i am thinking about the cost of object creation and garbage collec开发者_高级运维tion vs extensibility.
If you're concerned about the cost of memory allocation and deallocation (which for games you should be), do whatever you can to minimize the dynamic memory requirements of your application.
What class(es) the logic is attached to shouldn't matter; that'll be up to you to define what's simplest. This is separate from the memory needs, though; I don't think that the amount of functionality in an object affects allocation performance terribly much.
For what it's worth, I think the 2D integer array might be a good approach, and you can even keep your objects as objects; just make an array of objects that can trivially get reused when they need to change, and make it so that they can be marked "unused." Then you don't need to worry about the memory allocator (which can be slow slow slow on Android due to the possible need to reclaim resources from other applications).
精彩评论