Cocos2d layer and touches priority management - cocos2d-iphone

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];
}

Related

How to animate something in the the VOID statement? XCODE

-(void) redafter1
{
red = [CCMenuItemImage
itemFromNormalImage:#"red.png" selectedImage:#"redclick.png"];
red.position = ccp(175, 725);
redMenu = [CCMenu menuWithItems:red, nil];
redMenu.position = CGPointZero;
redMenu.scale = .75;
[self addChild:redMenu z:10];
}
How would I go about animating this object to move to another location on the screen? I am very new to this, please be basic in your explanation.
If you want to animate CCNode (this is base class of all cocos2d objects such as layers, sprites, labels, menuitems, etc.), you have to use actions mechanism. To move object, use CCMoveTo, CCMoveBy actions with runAction: method.
id moveAction = [CCMoveTo actionWithDuration: animationDuration position: targetPosition];
[nodeToAnimate runAction: moveAction];
In your case if you will run action on the object right after adding it to the some parent, that is visible (your scene or other visible parent), action will be started right after object appear.
You add [redMenu runAction:[CCMoveTo actionWithDuration:time position:place]]; (you choose time and position) at the end of your redafter1 function so when the parent method is eventually called by a method such as init your menu will move.Remember you cannot move a CCMenuItemImage because it's locked to the CCMenu's position. You have to move the CCMenu itself.Hope this was helpful.

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).

Create a left pointed UIBarButton in a UINavigation Bar Programmatically

I'm trying to create an app with a settings page just like IOS's native settings app. However I cannot find out how to create a UIBarButton that is left pointed. Basically I want a left pointed button on the top of a navigation bar, that you can set to call a function. I am using the engine cocos2d, if this helps. I am creating the entire app programmatically.
Here is my code for creating the UINavigationBar...
UINavigationBar *naviBarObj = [[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, 320, 44)] autorelease];
[[[CCDirector sharedDirector] openGLView] addSubview:naviBarObj];
[naviBarObj release];
UINavigationItem *item = [[[UINavigationItem alloc] initWithTitle:#"Title"] autorelease];
UIBarButtonItem *back = [[[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(back)] autorelease];
item.leftBarButtonItem = back;
[naviBarObj setItems:[NSArray arrayWithObjects:item, nil] animated:YES];
Although this creates a square back button.
My goal is to create something like this
I am aware that a UINavigationItem
UINavigationItem *back2 = [[[UINavigationItem alloc] initWithTitle:#"Settings"] autorelease];
with [naviBarObj setItems:[NSArray arrayWithObjects:back,item, nil] animated:YES];
will create that effect, but unfortunately I am not currently using a UINavigationController class. This is all made in a CCLayer.
Thank You!!
One way would be to use a custom back image, but that would be kind of tedious.
Another way would be like exactly what you mentioned in your last part, to create another "dummy" previous navigation item with the title set to the text you want to be on your back button, and then adding both items to the UINavigationBar.
UINavigationItem *item = [[[UINavigationItem alloc] initWithTitle:#"Title"] autorelease];
UINavigationItem *back2 = [[[UINavigationItem alloc] initWithTitle:#"Settings"] autorelease];
[naviBarObj setItems:[NSArray arrayWithObjects:back2,item, nil] animated:YES];
However I don't think you will need a UINavigationController for these to happen. In order to handle your back button event, I suppose you can try making your CCLayer a delegate of UINavigationBar, ie. UINavigationBarDelegate:
#interface MyLayer : CCLayer <UINavigationBarDelegate> {
}
then setting the delegate of your UINavigationBar to that layer:
naviBarObj.delegate = self;
and finally implement didPopItem to get notified of the event when the button is clicked:
- (void) navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item
{
[self back]; // or whatever
}
Let me know if this works.

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.

How to transfer a CCSprite from one parent to another?

I have a CCSprite called sprite that is a child of a CCLayer called movingLayer that is itself a child of the current CCLayer running my game logic, so it is self in this case. movingLayer is moving back and forth across the screen in a repeat forever action and sprite is riding along on it. I want to get sprite to "get off" of movingLayer and "get on" to self where it can do some actions of its own.
When I try to do this, I need to tell movingLayer to remove sprite and self to add sprite. This is what I did...
- (void)attack:(id)sender
{
[sprite stopAllActions];
[movingLayer removeChild:sprite cleanup:YES];
[self addChild:sprite z:1];
CGFloat distance = ccpDistance(sprite.position, ccp(sprite.position.x, -sprite.contentSize.height/2));
id goDown = [CCMoveTo actionWithDuration:distance/moveDuration position:ccp(sprite.position.x, -sprite.contentSize.height/2)];
id repeatDown = [CCRepeatForever actionWithAction:[CCSequence actions:[CCMoveTo actionWithDuration:0 position:ccp(sprite.position.x, sprite.contentSize.height/2+self.contentSize.height)], [CCMoveTo actionWithDuration:moveDuration position:ccp(sprite.position.x, -sprite.contentSize.height/2)], nil]];
id attackAction = [CCSequence actions:goDown, repeatDown, nil];
[sprite runAction:attackAction];
}
I think stopAllActions is redundant, but that's not the problem. If I remove sprite from movingLayer before adding it to self I crash for accessing a zombie, and if I try to add it to self first, I crash for trying to add something already added.
Have you tried setting cleanup to NO?
Alternatively, try to retain sprite before you remove it from movingLayer, then release it when you're done with it:
[sprite retain];
[movingLayer removeChild:sprite cleanup:YES];
[self addChild:sprite z:1];
[sprite release];