0

I can't test this on my phone because I'm poor and a new iOS programmer so I don't know if this is just an issue with the simulator or what. Also very new to cocos2d so bear with me here. I am working on a starting screen for my game. I made a background image with a cloud in the top left and in the top right. Then put a CCMenuItemLabel on each cloud so that it looks all prettiful. It works tremendously.... until you try to click either of the buttons in which case nothing happens! Here's the code I have right now.

MainMenu : CCScene

@implementation MainMenu

-(id) init {

  // Play Label to left cloud
  CCLabelTTF * playLabel = [CCLabelTTF labelWithString:@"Play" fontName:@"Marker Felt" fontSize:24];
  playLabel.color = ccBLACK;
  CCMenuItemLabel * play = [CCMenuItemLabel itemWithLabel:playLabel target:self selector:@selector(playGame)];
  CCMenu * playmenu = [CCMenu menuWithItems:play, nil];
  playmenu.position = ccp(s.width/5,s.height - 55);
  [self addChild:playmenu z:10];

  // Options label to right cloud
  CCLabelTTF * optionsLabel = [CCLabelTTF labelWithString:@"Options" fontName:@"Marker Felt" fontSize:24];
  optionsLabel.color = ccBLACK;
  CCMenuItemLabel * options = [CCMenuItemLabel itemWithLabel:optionsLabel target:self selector:@selector(options)];
  CCMenu * optmenu = [CCMenu menuWithItems:options, nil];
  optmenu.position = ccp(s.width - s.width/5,s.height - 55);
  [self addChild:optmenu z:10];

  // Add background at z:-1 plus other

}

@end

s is my screen size and the rest seems to be pretty straight forward. This is all inside of a MainMenu.m which extends CCScene. As of right now my selectors are just NSLogs to make sure the clicking works. Which never runs for either of them.

What I've tried:

Based on a suggestion from the cocos2d forums I changed both selectors to @selector(playGame:) and then the methods to -(void)playGame:(id) sender but that didn't work either.

CaldwellYSR
  • 3,056
  • 5
  • 33
  • 50

3 Answers3

1

In the end I started a new project using the same template. I put the exact same code in from above and ran it. It works in this project. So I started copying and pasting things from the old project into this one to try to see what made it break and in the end nothing broke it. I guess it must have been something in the build phases or something but it all works now.

CaldwellYSR
  • 3,056
  • 5
  • 33
  • 50
0

Now I used same code in cocos2D 2.0 sample..Its working fine.

I guess you not enabled touches for your layer. use this code in your init method of CCLayer.

 self.isTouchEnabled = YES;
Guru
  • 21,652
  • 10
  • 63
  • 102
  • Yeah I have this same code in a similar project that uses CCMenuItemSprite's instead of labels and it works perfectly. In this instance I don't have a CCLayer... I'm extending a CCScene and adding sprites and menus to it. I'll try to do this to the scene though I didn't have to do it in my previous project I don't think. – CaldwellYSR Jul 26 '12 at 13:38
  • No go on this. CCScene doesn't have the isTouchEnabled property. – CaldwellYSR Jul 26 '12 at 13:41
  • @CaldwellYSR, I told for its parent CCLayer! Same code working fine in my system, cocos2D 2.0 – Guru Jul 26 '12 at 14:27
  • It doesn't have a parent CCLayer though is what I'm saying. Maybe that's my major malfunction here but I'm extending CCScene and then adding sprites and menus to the scene. No CCLayer's involved? – CaldwellYSR Jul 26 '12 at 14:44
  • @CaldwellYSR, then create CCLayer and add it to ur scene and all other menu and other objects to CClayer. Then that may solve ur problem. Make sure u set isTouchEnabled to YES. – Guru Jul 26 '12 at 15:56
  • This also didn't work. It's almost like something is swallowing the touches and not letting them get to the menu items. I can't find anything in the code that would be doing that though. – CaldwellYSR Jul 26 '12 at 16:05
0

I know its an old thread, but I faced the same problem today.

Here is what I had to do, to get it working,

  • Set the position for the CCMenuItemLabel * play to ccp(s.width/5,s.height - 55)
  • Change the position of CCMenu *playmenu to CGPointZero.

Example

play.position = ccp(s.width/5,s.height - 55);
playmenu.position=CGPointZero;
Deep Shah
  • 420
  • 2
  • 14