Fastest way to copy or create a CCNode game object in cocos2d-iphone?
Background: I'm developing a game for the iPhone/iPad using cocos2d-iphone. Our engine provides the ability for the level designer to create new bad-guys by defining a number of properties such as speed, AI, weapons and main sprite texture in .plist files.
Problem: Currently, we build an NSDictionary of all of the prototypes before the level starts. For each entity we store a class name and an NSDictionary describing the parameters required to build a new instance of this entity. We create an instance of each prototype at load time to precache textures but destroy it immediately. When a new bad guy spawns, we use the stored NSDictionary and pass it to the init method like so (stripped of error checking for brevity);
IKPrototype* prototype = [prototypes objectForKey:prototypeID];
IKBaseEntity* entity = [[[NSClassFromString([prototype prototypeClass]) alloc] init] autorelease];
[entity setPrototypeID:prototypeID];
[entity parseParameters:[prototype parameters]];
return entity;
Now, this works a treat - but creating a number of bullets in the same frame causes noticeable slow down (a frame or two dropped) as the engine parses the parameters (sometimes 20-30 values). However, they're essentially the same every time and only change when added to the world (which happens later).
Question: What would be the most efficient method to store a prototype object for later duplicates to be spawned. Would serialization/deserialization work with a subclass of a CCNode, and would it be any quicker than what I'm doing? Would it be better to store a copy of the final object in my prototype class, and then do a 'deep copy' of that object for spawning? Would I have to worry about CCTexture references if I did this? Please briefly explain the required steps for a proposed solution.
Have been trying to think of the most efficient method for solving this problem for a while. I'm wondering if there's a method I've not thought of above that might be better? Thanks for reading this f开发者_开发问答ar.
One approach I've taken is to pre-create a bunch of objects while you are loading resources for the first time (say in init), the co-opting one of those resources at run-time by just changing its parameters and moving it from an array of inactive objects to active objects.
You might not have created enough, of course, but if these objects are sharing textures, etc. then you might want to create them as members of a CCBatchNode anyway and make it even more efficient to draw many of them.
精彩评论