In the header file
NSMutableArray **targets;
NSMutableArray **projectiles;
then this in the Init call:
*targets = init];
*projectiles = [[NSMutableArray alloc] init];
This is in the dealloc call:
[*targets release];
*targets = nil;
[*projectiles release];
*projectiles = nil;
This in same game logic:
target.tag = 1;
[targets addObject:target];
target.tag = 2;
;
This in a callback after an action is done:
if { // target
;
} else if { // projectile
;
}
This is how I converted it:
In My class:
NSMutableArray<CCSprite>targets;
NSMutableArray<CCSprite> *projectiles;
I don’t have the init call, is there one, or is the constructor good enough?
In the game logic:
target~~>setTag;
targets.addObject;
projectile~~>setTag;
*projectiles.addObject(projectile);
My destructor:
*targets.removeAllObjects;
*targets = NULL;
NSMutableArray is written based on std::vector.
It very important that you mustn’t add or remove elements in the traversal of std::vector, or it will crash.
Instead, in the first traversal, you can add the elements to delete into a new vector. Then in the second traversal of this new vector, delete the elements of your target vector.
The update function in Cocos2dSimpleGame/HelloWorldScene.cpp translated just like this
In fact, I have translated Cocos2dSimpleGame to c++, the most of game logic is done.
But it can only run on uphone by now. I would like to publish after make it multi-platform, on iphone & android. It takes more work to do.
Please give me your email address, I would send this semi-finished Cocos2dSimple sources to you.
Thanks for the help. I see what you mean by traversing through one vector list and deleting off the other vector list. My e-mail address is bumbastard-at-yahoo-dot-com
Thanks for the e-mail with your cpp conversion code in it. It turned out that I wasn’t converting the setup of the actions correctly.
I had for adding a target:
// Create the actions
CCIntervalAction actionMove;
actionMove = CCMoveTo::actionWithDuration.width/2, actualY));
CCCallFuncNactionMoveDone;
actionMoveDone = CCCallFuncN::actionWithTarget(target, callfuncN_selector(HelloWorld::spriteMoveFinished));
target~~>runAction);
You had this for adding a target:
CCFiniteTimeAction actionMove = CCMoveTo::actionWithDurationactualDuration, ccp.width/2, actualY));
CCFiniteTimeActionactionMoveDone = CCCallFuncN::actionWithTarget);
target~~>runAction(CCSequence::actions(actionMove, actionMoveDone, NULL));
Mine would crash in the spriteMoveFinished callback on the target, when I called sprite->removeObject(target).
The call stack pointed at the end() call in the NSMutableArray class
Since I am new to cocos2d, I’m not sure what the big difference in code between mine and your version of setting up the actions and why it would cause such a crash.
Again thanks for the help. My demo isn’t crashing more