I am trying to make CCLabelTTF change its text every 10 seconds using an array of strings.
This is the code I have so far, but it is giving me this error!
2013-09-07 15:47:34.618 MazeIt[6271:1b503] -[TitleLayer opacity]: unrecognized selector sent to instance 0xa553bb0
If anyone who is more experienced with runAction and CCLabel could help me out, it would be extremely helpful!
This is the code I have:
I have _list which is the array of strings, _text which is CCLabelTTF which I want to manipulate
In init _text and _list is created:
[self schedule:#selector(callback) interval:10.0f];
-(void) callback
{
id fadeIn = [_text runAction:[CCFadeTo actionWithDuration:0.5 opacity:127]];
id fadeOut = [_text runAction:[CCFadeTo actionWithDuration:0.5 opacity:255]];
id change = [CCCallFunc actionWithTarget:_text selector: #selector(changeText:)];
id sequence = [CCSequence actions: fadeIn, change, fadeOut, nil];
[self runAction: sequence];
}
- (void)changeText:(id)sender{
[_text setString:#"You completed no levels!"];
// [_text setString:_list[0]];
// [_text setString:_list[_next%[_list count]]];
_next++;
}
Thank you!!!
LOL my bad it was a really simple error, and some tweaking fixed it!
I am just going to leave this up here in case someone else is trying to do the same thing!
The correct code is:
-(void) callback
{
id fadeIn = [CCFadeTo actionWithDuration:0.5 opacity:0];
id fadeOut = [CCFadeTo actionWithDuration:0.5 opacity:255];
id change = [CCCallFunc actionWithTarget:self selector: #selector(changeText:)];
id sequence = [CCSequence actions: fadeIn, change, fadeOut, nil];
[_text runAction: sequence];
}
Related
I have this:
-(void)fadeBackground
{
ccColor4B color = {0,0,0,255};
CCLayerColor *fadeLayer = [CCLayerColor layerWithColor:color];
[self addChild:fadeLayer z:7];
fadeLayer.opacity = 0;
id fade = [CCFadeTo actionWithDuration:1.0f opacity:200];//200 for light blur
id calBlk = [CCCallBlock actionWithBlock:^{
//show pause screen buttons here
//[self showPauseMenu];
}];
id fadeBack = [CCFadeTo actionWithDuration:2.0f opacity:0];
id sequen = [CCSequence actions:fade, calBlk, fadeBack, nil];
[fadeLayer runAction:sequen];
}
How do I stop the actions while the fadein occurs and resume them when the fadeBack occurs?
[[CCDirector sharedDirector] pause]; & [[CCDirector sharedDirector] resume]; will pause and resume the schedulers and actions throughout all the Sprites/Layers or any other cocos2d Nodes.
If you want to pause/resume a particular CCLayer along with children its containing,
////for pausing
[myLayer pauseSchedulerAndActions];
for(CCNode *child in myLayer.children){
[child pauseSchedulerAndActions];
}
///for resuming
[myLayer resumeSchedulerAndActions];
for(CCNode *child in myLayer.children){
[child resumeSchedulerAndActions];
}
To pause, you can use this call, need to call same for each menu in game.
[self pauseSchedulerAndActions];
[menu pauseSchedulerAndActions];
To resume:
[self resumeSchedulerAndActions];
[menu pauseSchedulerAndActions];
Most talk about buttons for cocos2d seems to be directed to CCMenu, where CCMenu AFAIK is meant for having a row or column in center of screen.
I need to place buttons randomly on screen and have yet to find a simple out of the box solution for this. I did try CCControlButton but didn't get it to work (CCScale9Sprite spriteWithSpriteFrameName loads incorrectly from sprite atlas).
For now I try to use this. It uses only one button in each CCMenu. So my screen will have lots of CCMenu instances, one for each button.
+(CCMenu*)button:(NSString*)spriteframename at:(const CGPoint)POINT block:(void(^)(id sender))block {
CCSprite* sprite1 = [CCSprite spriteWithSpriteFrameName:spriteframename];
CCSprite* sprite2 = [CCSprite spriteWithSpriteFrameName:spriteframename];
CCMenuItem* menuitem = [CCMenuItemImage itemWithNormalSprite:sprite1 selectedSprite:sprite2 block:block];
CCMenu* menu = [CCMenu menuWithItems:menuitem, nil];
menu.contentSize = sprite1.contentSize;
menu.position = POINT;
return menu;
}
This is a better way which I changed to.
Put this in like init:
CCMenuItem* menuitemRetry = [[self class] buttonWithSpriteframenameOff:#"retry_off.png" on:#"retry_on.png" at:ccp(198, 184) block:^(id sender) {
// Do something
}];
CCMenuItem* menuitemMenu = [[self class] buttonWithSpriteframenameOff:#"menu_off.png" on:#"menu_on.png" at:ccp(362, 184) block:^(id sender) {
// Do something else
}];
CCMenu* menuLow = [CCMenu menuWithItems:menuitemMenu, menuitemRetry, nil];
menuLow.position = CGPointZero;
[self addChild:menuLow];
This also needed
+(CCMenuItem*)buttonWithSpriteframenameOff:(NSString*)spriteframenameOff on:(NSString*)spriteframeOn at:(const CGPoint)POINT block:(void(^)(id sender))block {
CCMenuItem* menuitem = [CCMenuItemImage itemWithNormalSprite:[CCSprite spriteWithSpriteFrameName:spriteframenameOff] selectedSprite:[CCSprite spriteWithSpriteFrameName:spriteframeOn] block:block];
menuitem.position = POINT;
return menuitem;
}
basically I want to do this:
-(void)doIt:(void(^)())block {
[CCSequence *sequence = [CCSequence actions:
[CCDelayTime actionWithDuration:1.0f],
(block ? [CCCallBlock actionWithBlock:block] : nil), nil];
[self runAction:sequence];
}
So that I can do:
[self doIt:^{ [self somethingElse]; }];
as well as:
[self doIt:nil];
rather than having to do when I have no need for a callback:
[self doIt:^{}];
... Any suggestions on how to do this sort of thing?
You could just check to make sure the block exists with
if(block)
{
//Sequence with block
}
else
{
//Sequence without block
}
I want to create a toolbar (initially hidden) with items that can be dragged. If a button is tapped, the toolbar will appear buttom-up (just like the animation of keyboard). I just like to ask how to do it in cocos2d.
Thanks for the response!
I used this code for drawer open and close.
-(void)showMyCocos2DDrawer
{
CGSize s = [[CCDirector sharedDirector] winSize];
self.position = ccp(-s.width,0.0f); //do this in ur init method :)
CGPoint pos =ccp(0.0f, 0.0f );
id moveTo = [CCMoveTo actionWithDuration:0.5f position:pos];
id calFun = [CCCallFunc actionWithTarget:self selector:#selector(animDone)];
id seq = [CCSequence actions:moveTo, calFun, nil];
[self runAction:seq];
}
-(void)hideCocos2DDrawer
{
CGSize s = [[CCDirector sharedDirector] winSize];
CGPoint pos =ccp(-s.width, 0.0f);
id moveTo = [CCMoveTo actionWithDuration:0.3f position:pos];
id calFun = [CCCallFunc actionWithTarget:self selector:#selector(goBack)];
id seq = [CCSequence actions:moveTo, calFun, nil];
[self runAction:seq];
}
-(void) animDone
{
//write in code here..
}
-(void)goBack
{
//write out code here..
}
I have this code:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:temp/10.0f
target:self
selector:#selector(verify)
userInfo:nil
repeats:YES];
CCMoveTo *moveTo = [CCMoveTo actionWithDuration:temp position:positionX];
id doneAction = [CCCallFuncN actionWithTarget:timer selector:#selector(invalidate)];
id sequence = [CCSequence actions: moveTo, doneAction, nil];
[self.container runAction:sequence];
the problem is that the timer is not invalidated and continues to run after the animation ends.
what am I missing?
thanks.
Change your CCCallFuncN to CCCallFunc
id doneAction = [CCCallFunc actionWithTarget:timer selector:#selector(invalidate)];
CCCallFuncN looks for an object to pass into the function using withObject:. You don't need this.