Forum > C++ > Problem with initWithTarget function

Problem with initWithTarget function

By zygmuntix Posted 2011-02-15 10:55 Comments 2
zygmuntix
  • Posts: 9

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 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*+ , I think that it is good tutorial to begin with Cocos2d-x programming .
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 :
<pre>
id actionMove = ;
id actionMoveDone = ;
];
</pre>
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:
<pre>
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));

</pre>
(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.

StrawberryMilkshake
  • Posts: 79

#1 RE: 2011-02-15 11:18

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).

zygmuntix
  • Posts: 9

#2 RE: 2011-02-15 12:56

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).

Loggin to reply

Copyright © 2010 - 2013 Cocos2d-x.orgClustrmaps