zz zz Problem with initWithTarget function
Posts 9
Added by zz zz over 2 years ago

Hi,

I have a problem with initWithTarget function, which You have translated from Objective-C.
It is located in in CCCallFunctionN class and it looks like this:
initWithTarget (SelectorProtocol *pSelectorTarget, SEL_CallFuncN selector)

The problem is that this function look very different (for me) in Objective-C. I want to rewrite this tutorial: http://www.raywenderlich.com/352/how-to-make-a-simple-iphone-game-with-cocos2d-tutorial
to C++ (only rewrite code to C++), I think that it is good tutorial to begin with Cocos2d-x programming (I would post the code to this forum after I rewrite the code of this sample game from Objective-C).
I don't know how to use this function. In Objective-C as said in the simple-game tutorial You have only to write something like this (to implement the actions):

id actionMove = [CCMoveTo actionWithDuration:actualDurationposition:ccp(-target.contentSize.width/2, actualY)];
id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)];
[target runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];

I have only rewritten two functions, first and the third, I don't know how to translate to C++ the second.

The translated lines of code:

CCMoveTo *actionMove = CCMoveTo::actionWithDuration(actualDuration, ccp(-target->getContentSize().width/2, actualY));
target->runAction(CCSequence::actions(actionMove, actionMoveDone));
and the attempt to translate the second line...:
SelectorProtocol *selectorProtocol = new SelectorProtocol();
CCCallFuncN *actionMoveDone = CCCallFuncN::actionWithTarget(selectorProtocol, SEL_CallFuncN(target));

(I don't even know at all what You mean by "SelectorProtocol", it is some class to join the game node and the action (function), right?)
Please, help me and translate this one line of code to the C++ in the right way.

Strawberry Milkshake RE: Problem with initWithTarget function
Posts 79
Added by Strawberry Milkshake over 2 years ago

Try this instead:

target->runAction( CCSequence::actions(
    CCMoveTo::actionWithDuration(actualDuration, ccp(-target->getContentSize().width/2, actualY)),
    CCCallFuncN::actionWithTarget(this, callfuncN_selector(HelloWorld::spriteMoveFinished)), 
    0l) );

What "id" is depends on the context. The CCMoveTo action could be represented by a CCFiniteTimeAction (which is the argument for CCSequence::actions), for example.

You can find more examples in the "tests" program, it's all in there :)
(open Visual Studio or your favorite IDE and search for "CCSequence::actions" in the current solution to get a list of all relevant files).

Also, don't forget to terminate the list of actions with a null pointer (nil in Objective-C, in C++ usually written as 0l, 0 or NULL).

zz zz RE: Problem with initWithTarget function
Posts 9
Added by zz zz over 2 years ago

Thanks very much! ;)
Edit:
Your version didn't work at first look.... I had to change my function called "spriteMoveFinished" - it must look like this:
void HelloWorld::spriteMoveFinished(CCNode* sender).


(1-2/2)