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];
Related
I'm creating a game in cocos2d v3. In the CCScene, I add a CCNode that contains all the components for my HUD. In the CCNode, there are CCButtons added. I want to be able to handle the touch of these buttons in my CCScene. Is that possible? And if so, how is it done elegantly?
Code that I tested this morning and is working:
CCNode header file (Header class):
#property (nonatomic, retain) CCButton *goldButton;
I set up the basics of the gold button in the implementation file of the CCNode (positioning, sprite frame, etc).
CCScene implementation file (PlayScene class):
-(void) setup {
_header = [[Header alloc] init];
[_header.goldButton setTarget:self selector:#selector(goldButtonTapped)];
}
Seems a little weird setting the target of the button inside the scene, but it works. I am wondering of the 'best practice', if you will, for this situation.
Since I'm having performance issues with using a UIView parallaxing images in the background of a transparent Cocos2D scene (it was fine in iOS 6), I think I may use the notification node to run the parallax since I need the parallax running between scene transitions. I've tested setting the notifications node, but I haven't figured out how to make it display in a different z-order. I want it to display under everything in the scene.
Right now for my test I have
CCLayer *layer = [CCLayer node];
CCLabelTTF *labelTest = [CCLabelBMFont labelWithString:#"test" fntFile:#"readySetGo.fnt"];
labelTest.position = CGPointMake(100,100);
[layer addChild:labelTest];
[[CCDirector sharedDirector] setNotificationNode:layer]; // Layer should be placed here
[layer onEnter]; // Schedule for updates
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.
we are about to finish our app for the iphone.
all the game is in one class helloWorldLayer.mm . using cocos2d.
Now i need to add the game menu.
2 ways.
adding it in the same class on the init method and just call it from there as a sprite with buttons .
make another class to be the gameMenu class and call this class from the delegate with:
[[CCDirector sharedDirector] runWithScene: [gameMenu scene]];
then from the game menu to load the game scene with :
[[CCDirector sharedDirector] replaceScene: [HelloWorldLayer scene]];
is that method ok ? do i have to add something else? release something?
does memory is better with the first or the second ?
thanks a lot !
A little game menu screen scene should be better - mostly for the sake of organization. Having your menu in a separate scene or in the game class itself shouldn't make much difference, but I would still prefer to have it separately. You could also do some fancy scene transition effect.
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)].