activate Button from added CCScene - cocos2d-iphone

I am adding a new layer to a scene. The new layer is loaded from a ccb file which includes buttons:
[[CCDirector sharedDirector] pause]; //pauses current scene
CCScene *pauseMenue = [CCBReader loadAsScene:#"Pause"];
pauseMenue.positionType = CCPositionTypeNormalized;
pauseMenue.position= ccp(0.5,0.5);
[self addChild:pauseMenue]; //adds the pause Layer
How do I get the buttons to work? Using the CCControl Selector doesn't seem to work…
-Pause.ccb doesn't consists of further ccb files.
-Pause.ccb has its custom class in which I tried to implement the selectors.

Related

Cocos2d V3 CCLayer replacement?

Using cocos2d v2 i was able to set some other class as a layer , and add it to some scene.
I did it with :
BackgroundLayer *background=[[BackgroundLayer alloc] init];
[self addChild:[background set]]; //returns a Node
Where the background layer was a CCLayer .
Now i am trying to do the exact same where the background layer is a CCNode ,
but it wouldn’t add it to the other scene , just perform its Init method .
How would i add some other CCNode class to another CCScene class as a layer ?
Thanks ,
CCLayer no longer exists in Cocos2d V3, to be honest I see no reason for CCLayer anyways.
I think you are looking for CCNodeColor in this case.
For example:
CCScene *scene = [[CCScene alloc] init];
CCNodeColor *nodeColor = [CCNodeColor nodeWithColor:[CCColor redColor]];
[scene addChild:nodeColor];

Cocos2d layer and touches priority management

I have my main scene, above it i set a new layer with CCLayer ,this layer has a button.
but when i hit that button (CCMenu), the layer behind it is also get the touches and do stuff.
I want to enable ONLY the upper layer touches, not the one under it .
How can i do that ? (setting touch priority ? how ? )
edit:
my layer is like that :
-(CCLayer*)showHelpLayer
{
self.isTouchEnabled=YES;
[[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self priority:-256 swallowsTouches:YES];
...
...
[self addChild:menu];
[menu setHandlerPriority:-257];
return self;
}
and i am adding it to the main scene like that :
helpLayer *hlp=[[helpLayer alloc]init];
[hlp showHelpLayer];
[self addChild:hlp z:100];
you could toy with priorities, but it gets nasty. I only do that in very very specific, well contained circumstances. The best approach is to disable input on your "underneath scene(s)" before you push the new layer and control objects.
If you chose to play with priorities, remember that all menus default to this priority (cocos2D 2.xx) :
kCCMenuHandlerPriority = -128,
so if you play with priority, i would put the layer at -256 (swallowing touches) and setHandlerPriority to -257 for your menu. So anything that falls through your menu is caught by the layer and swallowed (ie not passed 'below').
example for priority approach. The fly-through menu is a class that extends CCNode and does this onEnter, after creating all menu objects in the init method :
- (void)onEnter {
[super onEnter];
MPLOGDEBUG(#"");
[[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self priority:-256 swallowsTouches:YES];
[_backMenu setHandlerPriority:-257];
[_toggleOptionsMenu setHandlerPriority:-257];
[_dialogMenu setHandlerPriority:-257];
[_labelMenu setHandlerPriority:-257];
}

How to after recreate of same sprite, continue touch events

i have 2 layers and on ccTouchMoves event i have to destroy and recreate sprite to move from 1st layer to 2nd
i did this something like that
-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
sprite = [CCSprite spriteWithFile:#"file.png"];
[[self parent] addChild: sprite]
if (sprite)
{
[sprite ccTouchBegan:touch withEvent:event];
// [character ccTouchMoved:touch withEvent:event];
}
[self removeFromParentAndCleanup:true];
}
sprite created and called method ccTouchBegan but after that method everything is terminate
how to call ccTouchMoved and ccTouchEnd just like simple touch event
If it's the same sprite, why destroy and recreate it? You can just keep on using the same sprite. In Kobold2D I added this method in a CCNode category to transfer ownership of a node from its current parent to a different parent:
-(void) transferToNode:(CCNode*)targetNode
{
CCNode* selfNode = [self retain];
[self removeFromParentAndCleanup:NO];
[targetNode addChild:selfNode z:selfNode.zOrder tag:selfNode.tag];
[selfNode release];
}
The important part is to remove the node (your sprite) from its current parent without cleaning up, so that schedulers and actions keep running. Then just add it as child to a different node (your 2nd layer).

Object Order in CCLayer ( cocos2d-iphone )

I initialize my CCLayer using the following init code:
- (id)init {
if((self=[super init])) {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.wantsFullScreenLayout = YES;
picker.allowsEditing = NO;
picker.showsCameraControls = NO;
picker.navigationBarHidden = YES;
picker.toolbarHidden = YES;
picker.cameraViewTransform= CGAffineTransformMakeScale(1.3, 1.33);
[[[CCDirector sharedDirector] openGLView] addSubview:picker.view];
CCSprite *gold = [CCSprite sprite];
gold.position = ccp(150, 150);
[self addChild:gold];
}
return self;
}
The CCSprite was above the camera view before the camera shutter opens, but when the shutter opens, the CCSprite is overlapped by the camera.
Can I rearrange the order of these 2 objects / put the camera view to the back ?
Not without some extra work.
To understand this you have to consider that you're adding the camera view to the openGLView by Cocos2D. This makes the camera view a "child" view of the Cocos2D view. This is similar to adding a child node to a CCScene or CCLayer, and then wanting to have that node drawn behind the scene or layer. You can't do this without changing the way the view hierarchy is setup.
So you will have to modify Cocos2D's startup so that the openGLView is not added to the UIWindow directly, but to another UIView.
UIView* dummyView = [[UIView alloc] initWithFrame:[window bounds]];
[dummyView autorelease];
[dummyView addSubview:[CCDirector sharedDirector].openGLView];
rootViewController.view = dummyView;
You can then add the camera view to the dummyView (not the openGLView or you'll have the same problem as before) and perform sendSubviewToBack on it so that it is in the background.
You will also have to initialize the OpenGL view of Cocos2D with kEAGLColorFormatRGBA8 pixelFormat in order to provide an alpha channel.
EAGLView* glView = [EAGLView viewWithFrame:[window bounds]
pixelFormat:kEAGLColorFormatRGBA8
depthFormat:0
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];
You also need to make the openGLView transparent:
[CCDirector sharedDirector].openGLView.opaque = NO;
Of course this only works if you don't render anything fullscreen on the cocos2d view. For example, if you provide a fullscreen background image for your Cocos2D view, nothing will show up because the background image is rendered on top of the camera view.
You can find a more detailed explanation in the second edition of my book. You can also download the book's source code from that link and see the examples of chapter 15. Or download Kobold2D and use the provided Cocos2D-With-UIKit-Views template project.

Cocos2D Disable CCMenu

I am new to Cocos2d. I gonna create a sample game which has Menu(CClayer) ,in which each menuitems add CClayer as a child to the Menu Layer. Now i add the settings layer as a child to the Menu layer. when i select the settings layer, the touch detected in Menu layer not in settings layer. how to disable CCMenu in Menu Layer.
Menu Layer:Which contains CCMenu ;
Settings Layer: which contains also CCMenu;
Help me,
From your description without codes i can only tell you this line of code..
MenuLayer.yourMenuObj.isTouchEnabled = NO;
In my MenuLayer(CCLayer) which has ccmenu declared in Init Method
{
CCMenuItem *Play = [ CCMenuItemFont itemFromString:#"Level Select" target:self
selector:#selector(toPlay) ];
CCMenuItem *options = [ CCMenuItemFont itemFromString:#"Options" target:self
selector:#selector(toSettings) ];
CCMenu *mainMenu=[CCMenu menuWithItems:startGame,settings,nil];
[mainMenu alignItemsVertically];
mainMenu.position = ccp(240,160);
[self addChild:mainMenu z:1];
}
-(void) toPlay
{
OptionsLayer *tOptionsLayer=[OptionsLayer node];
[self addChild:tOptionsLayer z:2];
}
When i touch the "Options" in Menu it shows the OptionsLayer as a child to the MenuLayer. The OptionsLayer have Menu Items , when i touch the Menu items in OptionsLayer , the touch Touched in Menulayer. so again it shows the OptionsLayer. how to disable the touch on menuitems in MenuLayer.