CCLabelBMFont for a menu item? - cocos2d-iphone

Hey I am crazy stuck on this. I want to use my cool heiro font sheet i got the CCLabelBMFont working great as labels and score variables but can't get them into the menu as clickable items! but it the CCLabelMBFont says specifically that you can use them as menu items -- see here:
Detailed Description
CCLabelBMFont is a subclass of CCSpriteBatchNode
Features:
* Treats each character like a CCSprite. This means that each individual character can be:
o rotated
o scaled
o translated
o tinted
o chage the opacity
* It can be used as part of a menu item.
* anchorPoint can be used to align the "label"
* Supports AngelCode text format
Yet i've looked all over the web and can't find one example of anybody getting a CCLabelBMFont as a menu item. Here's the code I have so far:
-(id) init
{
if( (self=[super init] )) {
CCLabelBMFont *homeTest = [CCLabelBMFont labelWithString:#"HomeTEST" fntFile:#"hieroTitle2.fnt"];
homeTest.position = ccp(0, 0);
//finalScoreFont.position = ccp(wrapper.position.x, wrapper.position.y-40);
[self addChild:homeTest z:2 tag:kTagHomeTest];
/* here's where I am lost and want to put the CCLabelBMFont
CCMenuItem *homeButton =
home.position = ccp(0, 0);
CCMenu *menu = [CCMenu menuWithItems:homeButton, nil];
menu.position = ccp(60, 50);
*/
[self addChild:menu z:3];
}
return self;
}

i found a clue and possible solution! Check out this guys code:
CCLabelBMFont *tlabel = [CCLabelBMFont labelWithString:#"Page 2" fntFile:#"customfont.fnt"];
CCMenuItemLabel *titem = [CCMenuItemLabel itemWithLabel:tlabel target:self selector:#selector(testCallback:)];
CCMenu *menu = [CCMenu menuWithItems: titem, nil];
menu.position = ccp(screenSize.width/2, screenSize.height/2);
Nice! he first makes the label and then uses that CCLabelBMFont as the CCMenuItemLabel Weeee! I would have never figured that out. I'm going to try it

Related

Scene Change Crash in cocos2d

I am having a problem when trying to push a button and going to the next scene. I get the SIGABRT error. I don't know what the problem is:
[...]
//Play Button
CCMenuItem *playbutton;
playbutton = [CCMenuItemFont itemWithString:#"Play" target:self selector:#selector(playButtonMeathod:)];
CMenu *menu = [CCMenu menuWithItems:itemAchievement,playbutton,itemLeaderboard, nil];
[menu alignItemsHorizontallyWithPadding:20];
[menu setPosition:ccp( size.width/2, size.height/2 - 50)];
// Add the menu to the layer
[self addChild:menu];
-(void)playButtonMeathod{
// Create a scene transition that uses the "RotoZoom" effect
CCTransitionRotoZoom *transition = [CCTransitionRotoZoom transitionWithDuration:1.0 scene:[Level_1 scene]];
// Tell the director to run the transition
[[CCDirector sharedDirector] replaceScene:transition];
[...]
One issue might be that the method signature is incorrect in your CCMenuItemFont selector target. Try:
CCMenuItemFont *playbutton = [CCMenuItemFont itemWithString:#"Play" target:self selector:#selector(playButtonMeathod)];

Cocos2d: ARC releasing CCSprite too soon?

I enabled ARC for my cocos2d project.
Now i try doing the following:
BuildTowerMenu *menu = [BuildTowerMenu menuAtLocation:tileScreenPos];
[self addChild:menu];
And in the BuildTowerMenu class:
+(id)menuAtLocation:(CGPoint)location {
return [[self alloc] initMenuAt:location];
}
-(id) initMenuAt:(CGPoint)location {
if (self = [super init]) {
self.position = location;
CCSprite *item1 = [CCSprite spriteWithFile:#"Icon.png"];
item1.position = location;
[self addChild:item1];
}
return self;
}
But for some reason, the Sprite never shows up. After a bit of debugging i see that when i return from menuAtLocation, the CCSprite is still in the Array of children of BuildTowerMenu, but empty (only got an id).
If i actually add the sprite from outside it works and the Sprite is displayed:
BuildTowerMenu *menu = [BuildTowerMenu menuAtLocation:tileScreenPos];
CCSprite *item1 = [CCSprite spriteWithFile:#"Icon.png"];
item1.position = location;
[menu addChild:item1];
[self addChild:menu];
Any hints on what i did wrong here?
P.S.: i added a breakpoint in the dealloc of CCSprite, which never gets called (i guess it should be called if ARC is releasing it)...
I think that the problem is that you set equal positions to your menu and sprite.
I mean that this part of code
CGPoint location = ccp(200.f, 200.f);
[menu setPosition: location];
[sprite setPosition: location];
[menu addChild: sprite];
will add your sprite with position position (400.f, 400.f), relatieve to the menu's parent. You are doing almost the same thing in your BuildTowerMenu's initMenuAt: method/
As already commented on the initial question_:
Ok, it had nothing to do with ARC; the Sprite which seemed to be released was there, it was just a debugger - bug which didnt display it correctly. The problem was actually the line self.position = location; After moving the setposition after the addChild, everything worked.

Cocos2d: CCMenu Buttons for Retina Devices Have small touchable area

For the project I'm working on I'm doing the following:
CCLabelBMFont *howtoplayGameLabelFont = [CCLabelBMFont labelWithString:#"HOW TO PLAY" fntFile:font];
[howtoplayGameLabelFont setFontsize:fontSize];
CCMenuItemLabel *howtoplayGameLabel = [CCMenuItemLabel itemWithLabel:howtoplayGameLabelFont target:self selector:#selector(startHowToPlay:)];
CCMenu *bottomMenu = [CCMenu menuWithItems:howtoplayGameLabel,creditsGameLabel, nil];
[bottomMenu alignItemsVerticallyWithPadding:padding];
[bottomMenu setPosition:ccp( size.width/2 + bottomXOffSet, size.height/2 + bottomYOffSet)];
[self addChild: bottomMenu z:10];
For non-retina displays the whole label is clickable, but for retina displays a really small section of the label is clickable.
How does one fix this?
Totally forgot I added code for setFontSize on CCMenuItemLabel. Here it is:
-(void)setFontsize:(float)fontsize
{
Fontsize_ = fontsize * CC_CONTENT_SCALE_FACTOR();
self.scale = (Fontsize_ / OriginalFontsize_);
}

Trying to make a menu with buttons; program crashes

The title is pretty self explanatory. I am currently trying to make a cocos2d menu and it compiles flawlessly however it crashes right before fully launching and my log reads this...
+[NSInvocation invocationWithMethodSignature:]: method signature argument cannot be nil'
I don't know what this means here is my code though
// Standard method to create a button
CCMenuItem *Earth = [CCMenuItemImage itemFromNormalImage:#"1.png"
selectedImage:#"1.png"
target:self
selector:#selector(loadLevel:)];
Earth.position = ccp(160, 0);
CCMenuItem *Mars = [CCMenuItemImage itemFromNormalImage:#"2.png"
selectedImage:#"2.png"
target:self
selector:#selector(loadLevel:)];
Mars.position = ccp(160, 240);
CCMenuItem *Moon = [CCMenuItemImage itemFromNormalImage:#"3.png"
selectedImage:#"3.png"
target:self
selector:#selector(loadLevel:)];
Moon.position = ccp(160, 480);
CCMenu *myMenu = [CCMenu menuWithItems:Earth, Mars,Moon, nil];
[myMenu setPosition:ccp(160,240)];
[self addChild:myMenu z:0];
After adding this my program began to crash. Thanks for any help guys you are the best.
you need to implement loadLevel like this
-(void) loadLevel:(NSObject*) sender
{
}

how to pop a layer in cocos2d

please look the following picture
i want to do this function,when i click some button,it pop out a layer
my code is
-(id)init{
if (self = [super init]) {
CCMenuItem *successbtn = [CCMenuItemImage itemFromNormalImage:#"success.png"
selectedImage:#"success.png"
target:self
selector:#selector(successgame:)];
CCMenu *ccMenu = [CCMenu menuWithItems:successbtn, nil];
ccMenu.position=ccp(950,700);
[self addChild:ccMenu z:1 tag:2];
}
return self;
}
-(void)successgame:(id)sender{
//how can i write here?
}
so how can i write?
There is two possibilities. Either just add the button when you really want to show it and remove it from the Scenegraph as soon as it is not needed anymore.
Alternatively just make it invisible with the visible-property.