how to remove timer - cocos2d-iphone

i have this timer
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateTimerFunc) userInfo:nil repeats:YES];
and on some function i want to completly remove this...how?

You should not use NSTimer in Cocos2d, instead you should do:
[self schedule:#selector(updateTimerFunc) interval:1.0];
And to remove it:
[self unschedule:#selector(updateTimerFunc)];
or:
[self unscheduleAllSelectors];
If you have an update-function you could use: [self scheduleUpdate];

Related

Scene Change Crash in cocos2d

I am having a problem when trying to push a button and going to the next scene. I get the SIGABRT error. I don't know what the problem is:
[...]
//Play Button
CCMenuItem *playbutton;
playbutton = [CCMenuItemFont itemWithString:#"Play" target:self selector:#selector(playButtonMeathod:)];
CMenu *menu = [CCMenu menuWithItems:itemAchievement,playbutton,itemLeaderboard, nil];
[menu alignItemsHorizontallyWithPadding:20];
[menu setPosition:ccp( size.width/2, size.height/2 - 50)];
// Add the menu to the layer
[self addChild:menu];
-(void)playButtonMeathod{
// Create a scene transition that uses the "RotoZoom" effect
CCTransitionRotoZoom *transition = [CCTransitionRotoZoom transitionWithDuration:1.0 scene:[Level_1 scene]];
// Tell the director to run the transition
[[CCDirector sharedDirector] replaceScene:transition];
[...]
One issue might be that the method signature is incorrect in your CCMenuItemFont selector target. Try:
CCMenuItemFont *playbutton = [CCMenuItemFont itemWithString:#"Play" target:self selector:#selector(playButtonMeathod)];

How to remove a target with delay time

I try to delete a target with delay time, code likes this
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [self convertTouchToNodeSpace: touch];
targetsToRemove = [[NSMutableArray array] init];
for (CCSprite *target in _targets) {// here _targets is NSMutableArray
if (CGRectContainsPoint(target.boundingBox, location)) {
[targetsToRemove addObject:target];
}
}
for (CCSprite *target in targetsToRemove) {
if (target.tag == 1) {
[target setTexture:[[CCTextureCache sharedTextureCache] addImage:#"1.png"]];
[_targets removeObject:target];
[self removeChild:target cleanup:YES];
}
else {
CCLOG(#"remove target immediately");
}
}
}
If the target.tag = 1, I want to delay 0.5 to show its images, and then remove it, I try to use scheduleOnce, but it crashs, so how can I do?
I'm not sure how to write this in Objective-C (I'm only familiar with Cocos2D-XNA which is C#), but see if this works:
Make a CCSequence, first action in the sequence is CCDelayTime with 0.5 as argument, and the second action in the sequence is CCRemoveSelf. Then just run this sequence on the target sprite.
Hope this is helpful to you. You may want to try https://gamedev.stackexchange.com/ for more help.
Try this way:
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
...
for (CCSprite *target in targetsToRemove) {
if (target.tag == 1) {
[target setTexture:[[CCTextureCache sharedTextureCache] addImage:#"1.png"]];
CCDelayTime *delay =[CCDelayTime actionWithDuration:3.0f];
CCCallFunc *removeTarget = [CCCallFunc actionWithTarget:self selector:#selector(removeSprites:data:)data:target];
[target runAction:[CCSequence actionWithActions:delay, removeTarget, nil]];
}
}
-(void)removeSprites:(id)sender data:(CCSPrite *)sprtToDelete
{
if(!_targets)
[_targets removeObject:sprtToDelete];
[sprtToDelete removeFromParentAndCleanUp:YES];
}
Here you are running two actions on each to be deleted sprite. So on first action you wait for 3 seconds and in second action you call a method with parameter as this sprite. And in that method you clean up your sprite.
Hope this will help. LEt me know if not.

CCSprite comes and after 3 sec it'll remove automatically ?[Done]

I am working on project with cocos2d-android.
What I need this time is : A CCSprite comes on the screen and stay 3-4 sec and remove automatically. What class is available to do this work
If anybody have done this thing earlier. Suggest me the way ?
I'll give you code example in Objective-c cause i've never dealt with cocos2d-android, i believe it's pretty straightforward
CCSprite *spriteToDisplayAndRemove = [CCSprite spriteWithFile:#"filename.png"];
[self addChild:spriteToDisplayAndRemove];//say CCLayer adds our sprite
CCDelayTime *delay = [CCDelayTime actionWithDuration:3];
CCCallBlock *block = [CCCallBlock actionWithBlock:^{
[self removeChild:spriteToDisplayAndRemove];
}];
[self runAction:[CCSequence actions:delay, block, nil]];
EDIT:
Since blocks are unavailable in cocos2d-android you might use CCCallFunc instead. Again, Objective-c sample:
CCSprite *spriteToDisplayAndRemove = [CCSprite spriteWithFile:#"filename.png"];
spriteToDisplayAndRemove.tag = 100;
[self addChild:spriteToDisplayAndRemove];//say CCLayer adds our sprite
CCDelayTime *delay = [CCDelayTime actionWithDuration:3];
CCCallFunc *callFunc = [CCCallFunc actionWithTarget:self selector:#selector(removeSprite)];
[self runAction:[CCSequence actions:delay, callFunc, nil]];
And here is your removeSprite method:
-(void)removeSprite
{
CCSprite *sprite = [self getChildByTag:100];
[self removeChild:sprite];
}

Using glScissor on a modal layer in cocos2d

I am coding a modal layer in cocos2d and I would like to use the OpenGL glScissor API call to crop the inside of a CCScrollLayer that I am using,
Basically,
Present some kind of Modal sprite
Put CCScrollLayer with assets on the modal sprite
I want to crop the CCScrollLayer so that it does not overflow the sprite.
Seems pretty simple.
The problem I'm having is that the visit function never seems to be get hit and thus glScissor never gets implemented.
I'm not sure what I'm doing wrong.
The way I do modals is I use blocks to handle a Yes and No condition/state.
// This appears in my "Pick City scene"
-(id) init
{
if ((self = [super init]))
{
NSLog(#"City list");
for (City *cityObj in listOfCities)
{
citySpriteOff = [CCSprite spriteWithSpriteFrameName:#"city.png"];
citySpriteOn = [CCSprite spriteWithSpriteFrameName:#"city_on.png"];
int x = [cityObj.x intValue];
int y = [cityObj.y intValue];
CCMenuItemSprite *mapMenuItem = [CCMenuItemSprite itemFromNormalSprite:citySpriteOff selectedSprite:citySpriteOn target:self selector:#selector(btnCity:)];
[mapMenuItem setTag:i];
[mapMenuItem setIsRelativeAnchorPoint:YES];
[mapMenuItem setAnchorPoint:CGPointMake(0, 0)];
[mapMenuItem setPosition:CGPointMake(x, y)];
[mapMenuItem setIsEnabled:YES];
[mapMenu addChild:mapMenuItem];
i++;
} // next
[self addChild:mapMenu z:2];
}
}
-(void) btnCity:(id)sender
{
NSLog(#"clicked a city button");
int tag = [sender tag];
City *cityObj = [listOfCities objectAtIndex:tag];
NSLog(#"You clicked on city: %#", cityObj.name);
[self lockLayers];
CCLayer *layer = [CCLayer node];
[self addChild:layer z:100];
[MapModalLayer ConfirmCity:cityObj onLayer:layer yesBlock:^{[self btnConfirmedCity:cityObj];} noBlock:^{[self unlockLayers];}];
}
-(void) btnConfirmedCity:(City *)cityObj
{
NSLog(#"confirmed city: %#", cityObj.name);
}
#pragma mark - Lock/Unlock layers
-(void) lockLayers
{
[self MenuStatus:NO Node:self];
}
-(void) unlockLayers
{
[self MenuStatus:YES Node:self];
}
// Disabled/Enable layers
-(void) MenuStatus:(BOOL)_enable Node:(id)_node
{
for (id result in ((CCNode *)_node).children) {
if ([result isKindOfClass:[CCMenu class]]) {
for (id result1 in ((CCMenu *)result).children) {
if ([result1 isKindOfClass:[CCMenuItem class]]) {
((CCMenuItem *)result1).isEnabled = _enable;
}
}
}
else
[self MenuStatus:_enable Node:result];
} // next
}
The actual modal code appears here,
#implementation MapModalLayer
- (id)init {
self = [super init];
if (self) {
// This method never seems to be called
NSLog(#"MapModalLayer init");
}
return self;
}
// This method never seems to be called
- (void) visit {
NSLog(#"Visit");
if (!self.visible)
return;
glPushMatrix();
glEnable(GL_SCISSOR_TEST);
glScissor(50, 50, 100 , 150);
[super visit];
glDisable(GL_SCISSOR_TEST);
glPopMatrix();
}
+ (void) CloseAlert: (CCSprite*) alertDialog onCoverLayer: (CCLayer*) coverLayer executingBlock: (void(^)())block {
// shrink dialog box
[alertDialog runAction:[CCScaleTo actionWithDuration:kAnimationTime scale:0]];
// in parallel, fadeout and remove cover layer and execute block
// (note: you can't use CCFadeOut since we don't start at opacity 1!)
[coverLayer runAction:[CCSequence actions:
[CCFadeTo actionWithDuration:0.2f opacity:0],
[CCCallBlock actionWithBlock:^{
[coverLayer removeFromParentAndCleanup:YES];
if (block) block();
}],
nil]];
}
+(void) ConfirmCity:(City *)cityObj onLayer:(CCLayer *)layer yesBlock :(void (^)())yesBlock noBlock:(void (^)())noBlock
{
CCLayerColor *coverLayer = [CoverLayer new];
[layer addChild:coverLayer z:INT_MAX]; // put to the very top to block application touches
[coverLayer runAction:[CCFadeTo actionWithDuration:kAnimationTime opacity:80]]; // smooth fade-in to dim with semi-transparency
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCSprite *dialog = [CCSprite spriteWithSpriteFrameName:#"modal.png"];
[dialog setPosition:CGPointMake(winSize.width/2,winSize.height/2)];
[dialog setTag:kDialogTag];
//
// We put our CCScrollLayer *scroller content here... it doesn't matter what it is right now
//
//
// Finally we put our accept/reject buttons
//
// Tick/Cross buttons
CCSprite *closeButtonOn = [CCSprite spriteWithSpriteFrameName:#"btn_close.png"];
CCSprite *tickButtonOn = [CCSprite spriteWithSpriteFrameName:#"btn_accept.png"];
// add one or two buttons, as needed
CCMenuItemSprite *opt1Button = [CCMenuItemSprite itemFromNormalSprite:closeButtonOn
selectedSprite:nil
block:^(id sender){
// close alert and call opt1block when first button is pressed
[self CloseAlert:dialog onCoverLayer: coverLayer executingBlock:noBlock];
} ];
[opt1Button setPosition:CGPointMake(-200, -120)];
// create second button, if requested
CCMenuItemSprite *opt2Button = [CCMenuItemSprite itemFromNormalSprite:tickButtonOn
selectedSprite:nil
block:^(id sender){
// close alert and call opt2block when second button is pressed
[self CloseAlert:dialog onCoverLayer: coverLayer executingBlock:yesBlock];
} ];
[opt2Button setPosition:CGPointMake(40, -120)];
CCMenu *menu = [CCMenu menuWithItems:opt1Button, opt2Button, nil];
[menu setContentSize:dialog.contentSize];
[dialog addChild:menu z:2];
[coverLayer addChild:dialog];
}
#end
I think its because I only ever use MapModalLayer's private methods only.
But I am not sure.
Is there a way to allow me to use the glScissor in a modal like explained above?
I've tried moving the glScissor code to the display of the modal, but it never seems to do anything.
To confirm it is working, I moved the glScissor code to the parent and it seems to work fine.
Thus, how do I make the modal layer use/work with glScissor?
I have since resolved this problem.
I used the Viewport from http://pastebin.com/tWsEbxvJ
The way I did it was:
CoverLayer *coverLayer = [CoverLayer new];
[layer addChild:coverLayer z:INT_MAX]; // put to the very top to block application touches
[coverLayer runAction:[CCFadeTo actionWithDuration:kAnimationTime opacity:80]]; // smooth fade-in to dim with semi-transparency
// ------------------------------
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCSprite *dialog = [CCSprite spriteWithSpriteFrameName:#"modal.png"];
[dialog setPosition:CGPointMake(winSize.width/2,winSize.height/2)];
[dialog setTag:kDialogTag];
.. // Build your pagesArray here for ScrollLayer...
// Now create the scroller and pass-in the pages (set widthOffset to 0 for fullscreen pages)
CCScrollLayer *scroller = [[CCScrollLayer alloc] initWithLayers:pagesArray widthOffset: 350];
[scroller setShowPagesIndicator:NO];
// finally add the scroller to your scene
//[dialog addChild:scroller];
Viewport *cn = [[Viewport alloc] initWithRect:CGRectMake(3, 0, dialog.contentSize.width-12, dialog.contentSize.height-5)];
[cn addChild:scroller];
[scroller release];
[coverLayer addChild:dialog z:1];
This allows me to put a modal on a layer, put a CCScrollLayer and make sure that the contents do not spill over the internals of the modal sprite I have made (this is why the width configuration looks a bit odd).
Thanks.

how to pop a layer in cocos2d

please look the following picture
i want to do this function,when i click some button,it pop out a layer
my code is
-(id)init{
if (self = [super init]) {
CCMenuItem *successbtn = [CCMenuItemImage itemFromNormalImage:#"success.png"
selectedImage:#"success.png"
target:self
selector:#selector(successgame:)];
CCMenu *ccMenu = [CCMenu menuWithItems:successbtn, nil];
ccMenu.position=ccp(950,700);
[self addChild:ccMenu z:1 tag:2];
}
return self;
}
-(void)successgame:(id)sender{
//how can i write here?
}
so how can i write?
There is two possibilities. Either just add the button when you really want to show it and remove it from the Scenegraph as soon as it is not needed anymore.
Alternatively just make it invisible with the visible-property.