Cocos2d: CCMenu Buttons for Retina Devices Have small touchable area - cocos2d-iphone

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_);
}

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

CCMenuItemSprite's alternative in Cocos2d v3

In Cocos2d 2.0 I used below code to use single image for normal and selected image with colour change on selection.
CCSprite *twitter_1 = [CCSprite spriteWithSpriteFrameName:FRAME_MM_TWR_1];
CCSprite *twitter_2 = [CCSprite spriteWithSpriteFrameName:FRAME_MM_TWR_2];
twitter_2.color = ccc3(128,128,128);
CCMenuItemSprite *twitterBtn = [CCMenuItemSprite itemWithNormalSprite:twitter_1
selectedSprite:twitter_2
target:self
selector:#selector(twitterBtnPress:) ];
In Cocos2d v3, I can use CCButton as alternative, but how to change selected frame colour?
CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
CCButton * twitterBtn = [CCButton buttonWithTitle:#""
spriteFrame:[cache spriteFrameByName:FRAME_MM_TWR_1]
highlightedSpriteFrame:[cache spriteFrameByName:FRAME_MM_TWR_1]
disabledSpriteFrame:nil];
twitterBtn = CCPositionTypeNormalized;
twitterBtn.position = ccp(0.5f, 0.5f);
[twitterBtn setTarget:self selector:#selector(playBtnPress:)];
[self addChild: twitterBtn];
Now in Cocos2d v3, how to use CCSprite for button and change colour?
You can use the method:
- (void) setBackgroundColor:(CCColor*)color forState:(CCControlState)state
of CCButton to set a different background color for the different states.

CCParticle define a region?

I am trying to define a region to my snow, when its now in the rect of the screen, but my game using twice the height of he screen and so, i want it to start from 2*screenHeigh.
How to change that property ?
CCTexture2D *tex=[[CCTextureCache sharedTextureCache] addImage:#"snow.png"];
CCParticleSnow *emitter = [CCParticleSnow node];
[emitter setTexture:tex ];
emitter.scale=0.7;
emitter.speed = 120 + arc4random() %20;
[self addChild: emitter z:-1];

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
{
}

CCLabelBMFont for a menu item?

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