Respond to popScene - cocos2d-iphone

I am using pushScene and popScene in my cocos2d-iphone game (1.0.1).
So I have this:
Scene1, Scene2 (Scene2 being the pushed scene).
Now I use popScene, so I have
Scene1.
Is there a way to run a method in Scene1 when it is recovered from the popScene method? I mean, I want Scene1 to realize that it is back to work. I tried putting something in the onEnter method, but really didn't work (either the screen was black or the touches didn't work)

Alright, fixed.
You can indeed use the onEnter method, but you have to call [super onEnter] first.
I was doing this:
-(void)onEnter {
[self doSomething];
}
But it should have been like this:
-(void)onEnter {
[super onEnter];
[self doSomething];
}

Related

Adding cclayer to scene -touches goes down

Always the same problem .
I have a scene and i am adding it a CClayer from another class , which is some background with ccmenu on him .
When touching it, touches goes under this layer, and i dont want it .
otherClass *layer=[otherClass alloc]init]; //otherClass returns a cclayer .
[self addChild: layer];
layer is good, and is above my scene , but the touches goes down.
is there a way in cocos2d to enable ONLY touches at the top layer ??
I must change the touches priority now ?
You need to change touch priority and set to swallowsTouches.
To do this, register your layer on touchDispatcher setting these parameters (you can also see CCLayer registerWithTouchDispatcher method as example):
[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];

trying to detect touch ended and touch moved for CCSprite

I have a CCSprite subclass, and initially I had set it up with a
So I had the following code:
-(void)onEnter {
[super onEnter];
[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
-(void)onExit {
[super onExit];
[[[CCDirector sharedDirector] touchDispatcher] removeDelegate:self];
}
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
if ([self containsTouch:touch]) {
// do stuff
return YES;
}
return NO;
}
But then I realized that I actually didn't want to use touchBegan, because I want to detect if a sprite has been dragged downward-- So I wanted to use touchMoved and touchEnded instead of touchBegan.
However, when I implement those methods, they are not called...
How can I tell when the sprite's touch ended, and if it was "swiped"?
Enabling multiple touches: In the applicationDidFinishLaunching:application method in your appdelegate, set multiple touches to YES: [glView setMultipleTouchEnabled:YES];
Then in your CCLayer subclass (the class you are working in for detecting touches), in the init method, add self.isTouchEnabled = YES;
Now your multi touch methods should get called.
Swiping: cocos2d does not support gestures out of the box. You will likely have to work yourself. You can start with the apple event handling guide about gestures. The How To Drag and Drop Sprites with Cocos2D totorial at raywenderlich.com hepled me.
In order for the dispatcher to call your methods (moved, ended, cancelled), you have to first claim the touch ie. you will process the events. That is done in ccTouchBegan, when you return YES. After that you will receive the other events.
CCTouchableSprite - my touchable sublcass of CCSprite with Objective-C blocks, you can use touchMoved to detect what you want.

In cocos2d, is there something paralled to onEnter and onExit?

I can capture scene change events by its onEnter and onExit methods. But when scene change events takes time, like fade in or fade out, onEnter is called to early (right before the fading) and onExit is called to late (after the fading completed).
I want another onEnter called right after the fading completed and another onExit called right before the fading. Can i?
There's a second onEnter callback just for transitions, it's called onEnterTransitionDidFinish. But like it has been mentioned, this will only fire if CCScheduler is being used in conjunction with CCSceneTransition.
Use a CCSequence with your CCFadeIn and then add a CCCallFunc after it.
onEnter and onExit are to do with CCNode object allocation and removal, not physical views.
Sample code:
[scene runAction:[CCSequence actions:
[CCFadeIn actionWithDuration:0.45f],
[CCCallFunc actionWithTarget:scene selector:#selector(fakeOnEnter:)], nil]];
Inside your scene object you will need a method as such,
-(void) fakeOnEnter:(id)sender {
// your code to run after the fadein
}

Is there a Button such as UIButton in COCOS2D?

I try to search a button such as UIButton in cocos2d..
UIButton can press several buttons at same time.
but CCmenuitem can't..
Is there anyclass in cocos2d such as uibutton?
Extend CCMenu to support multitouch. It was designed to use targeted touches, that is, single touch interactions. With a little elbow grease, it wouldn't be difficult at all to extend it to support multitouch.
CCMenuItems are used instead of UIButton in cocos2d. A single CCMenuItem inside your CCMenu can exactly work like the UIButton.
You can create that in the following way.
//inside your .m file
-(id) init
{
if( (self=[super init] )) {
CCMenuItem *yourMenuItem = [CCMenuItemImage itemFromNormalImage:#"normalImage.png" selectedImage:#"selectedImage.png" target:self selector:#selector(menuItemPressed:)];
yourMenuItem.position = ccp(60, 60);
CCMenu *yourMenu = [CCMenu menuWithItems:yourMenuItem, nil];
yourMenu.position = CGPointZero;
[self addChild:yourMenu];
}
}
There's a good article by Ray Wenderlich on this here.
However, if your main requirement is to add a UIButton only then this discussion might be helpful.
I would also take a look at SneakyInput if you are having trouble setting up your own buttons; it's very easy to use.

UIButton of previous scene overlaps CCSprite

I have a UIButton that moves around randomly on the screen. On clicking on the button, a new scene is loaded that, for now, contains a CCSprite. Here is the code:
//in init
CCSprite *a = [CCSprite spriteWithFile:#"a.png"];
[a setPosition:ccp(0,0)];
[self addChild:a];
Pretty straightforward, and it stumps me why after 'replaceScene' the UIButton of theHelloWorldScene.m is still visible, right on top of the Sprite. Where am I going wrong?
I'm assuming since you're adding a UIButton into a cocos2d Scene you're using the openGLView
something like:
[[[CCDirector sharedDirector] openGLView] addSubView:button];
If this is the case, then before you replace your HelloWorldScene you'll need call something similar to
[button removeFromSuperview]
Where button is the name of your UIButton (in both instances).
A suggestion though would be to use a CCMenu with a CCMenuItem on your HelloWorldScene as UIKit objects don't really mesh very well with Cocos2d.
You really aren't giving enough information. The simplest answer however is that if you do not want the button to be visible anymore then remove the child (the button). You can set a tag on the button and then use [layer getChildByTag:(NSInteger)].