I am translating a piece of code from cococs2D to cocos2D-X. I came across the following lines that i cannot fathom out how to translate
[spriteBg runAction:[CCSequence actions:sc,[CCCallFuncO actionWithTarget:basketTimer_ selector:NSSelectorFromString([selectors objectAtIndex:0]) object:sprite], nil]];
Can someone please help me translate this to Cocos2d in Cocos2d-X ?
Kind Regards,
try this..
spriteBg->runAction::create(CCSequence::create(sc,CCCallFunc::create(this, callfunc_selector(myMethod)),NULL));
In your code the myMethod is replaced by the selector at the index 0 of the "selectors" which I'm guessing is an array of the selectors or a dictionary or something like that.
and "sc" is a predefined action which is to be run on spriteBg.
For cocos2d-x v 2.2.1:
NSSelectorFromString([selectors objectAtIndex:0] <- in cocos2d-x you can't create selector from string, so you must know function you want call, or keep selectors in container(but I never do this)
CCCalFuncO *call = CCCallFuncO::create(basketTimer_, callfuncO_selector(BasketTimerClass::BasketTimerMethod), sprite)
spriteBg->runAction(CCSequence::create(sc, call, NULL));
Every class in cocos2d-x and cocos2d-iphone have the same name, so you can easly find it in documentation:
CCSequence CCCalFuncO
Related
I'm trying to make a custom sprite, which could receive touch and handle the function as callback.
Okay, first step, receive the touch, easy, we can search it online everywhere.
The one I couldn't do is, I want to make it receive SOMETHING in the class the sprite is created, a function that will be called when the sprite is touched.
I was searching on internet and I think (not really sure) that SEL_Callfunc can do what I want, but I couldn't understand how this one work, so can you guys give me an example please?
For example, my custom class is BSprite, so when I create new object in HelloWorld.cpp, it should be
BSprite* sprite = BSprite::create("HelloWorld.png",HelloWorld::TouchCallback);
Thanks for reading :)
sprite->addTouchEventListener(CC_CALLBACK_0(HelloWorld::onTouchSprite, this));
void HelloWorld::onTouchSprite() {
}
Note: onTouchSprite method should not have any parameters
Okay, so I want my cat sprite to move up and down onClick of two (UP and DOWN) buttons.
I'm a beginner in cocos2d-x.
So, in mygame.h i have a global declaration of the sprite cat:
cocos2d::Sprite *cat;
In one function i create a new scene and add a cat in it.
cat = Sprite::create("cat.png");
cat->setScale(0.2);
cat->setPosition(0, 190);//(Director::getInstance()->getVisibleOrigin().x + 50, Director::getInstance()->getVisibleSize().height / 2);
layer->addChild(cat);
playscene->addChild(cat);
In another function(button callback) i have this code:
void HelloWorld::down(Object* pSender){
CCActionInterval* down = CCMoveBy::create(1.0f, Point(0.0, -20.0));
cat->runAction(down);
}
And everything's ok untill i press the up or down button.
It throws an error on the cat->runAction(down); line.
When i exemine the variable cat, it looks like I cant get to the position parameters. Its a memory read error..
In cocos2dx 3.0
you can write direct in runaction for any sprite.
spriteName->runAction(Sequence::create(MoveBy::create(1.0f,Point(398,565)),NULL));
It looks like you are mixing Cocos2D-X 2.x API's with Cocos2D-X 3.0 ones. I'm taking a stab in the dark guess and saying it looks like you're trying to use 3.0. You will need to change the following line:
CCActionInterval* down = CCMoveBy::create(1.0f, Point(0.0, -20.0));
To:
ActionInterval* down = MoveBy::create(1.0f, Point(0.0, -20.0));
Could someone help me, I am trying to access a CCLabelTTF that resides in a CCLayer subclass(GameLayer), but I want to access it from another Player class(also a CCLayer). I thought
[self getChildByTag: DEBUG_LABEL];
searches the scene and finds the object that matches it and returns a pointer to it, since all objects are stored in a tree data structure.
I was able to access the label through trial and error using the follwing code but would appreciate if someone could advise if I am not understanding the getChildByTag method or if there is a way of searching the scene for an object without using the code below.
CCLabelTTF *lbl = (CCLabelTTF *)[[[[CCDirector sharedDirector] runningScene] getChildByTag: GAME_LAYER_TAG] getChildByTag: DEBUG_LABEL_TAG];
Please advise.
getChildByTag only checks direct children of the parent CCNode calling it. It will not check children's children (grandchildren, if you will).
For example, if your node hierarchy looks like this:
MyCCLayer1->MyCCLayer2->MyCCSprite->MyCCLabel
Calling MyCCLayer1 only has direct access to MyCCLayer2 via the getChildByTag call. In turn, MyCCLayer2 could call getChildByTag to get MyCCSprite, and then MyCCSprite could call getChildByTag to get MyCCLabel.
I'm currently porting an ObjC cocos2d game to cocos2d-x, but I'm encountering some problems when trying to create a registerWithTouchDispatcher method, at the moment I'm doing
void GameLayer::registerWithTouchDispatcher()
{
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this,0,true);
}
but this gives an error 'No member named sharedDispatcher' in cocos2d::CCTouchDispatcher'.
Is there another way that this must be done in cocos2d-x?
If you are using 2.0, they have been merged in to CCDirector.
please use
CCDirector::sharedDirector()->getTouchDispatcher()
use those code instead ccdirector. put the code to cclayer init function.
setTouchMode(kCCTouchesOneByOne);
registerWithTouchDispatcher();
In the cocos2d-x you can do like this.
CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0);
How to remove animation from object in raphael?
var animation = Raphael.animation({opacity:.2}, 1000);
var circle = paper.circle(0, 0, 5).animate(animation.repeat(Infinity));
I want to perform animation on object until some moment in time. And the question is how to remove/stop animation in that specific moment?
Well, I really don't know why, but the fiddle works if don't pass any arguments to the stop method. Despite what Raphael's documentation says, I found a working example of an animation stopping in this site (is not the most beatiful site, by the way, but they have an example for each raphael method!)
Here you have the Fiddle working. http://jsfiddle.net/fKxqS/2/
Enjoy!
If you want to stop the animation after a specific timelapse...
setTimeout(circle.stop(animation), 500) //500 is milliseconds, so it's 0.5s
If you want to stop the animation after an event, such as a click
circle.click(function(){
circle.stop(animation)
})
Edit: Seems Raphael doesn't stop if repeat is set to Infinite, perhaps somebody knows a workaround, here's the fiddle: http://jsfiddle.net/fKxqS/