Creating Buttons

This topic contains 22 replies, has 6 voices, and was last updated by  Joethemonkey101 3 years, 10 months ago.

Viewing 23 posts - 1 through 23 (of 23 total)
Author Posts
Author Posts
June 19, 2010 at 10:00 pm #222561

Joethemonkey101
@joethemonkey101

Hi guys. I was reading a page from this website someone sent to me. I realized there was an easier way to creating buttons than I thought. I tried replacing my way with the improved way, but I’m having a problem. Here’s a picture of my code. Please don’t mind the code not pertaining to “back”

When I build and run and go to that page, there is no button on that screen. The picture that is also on that screen doesn’t cover ccp(15, 15). I don’t know what’s wrong.

June 19, 2010 at 11:49 pm #287272

Kenny Ackerson
Participant
@pearapps

Just use UIKit

June 20, 2010 at 5:20 am #287273

Connor
Participant
@techy

Set menu position to 0 0

menu.position = ccp(0,0);

June 20, 2010 at 9:22 am #287274

Codemattic
Moderator
@codemattic

@Joethemonkey101 maybe use the code button or backticks and insert your code examples as text instead of as images.

June 20, 2010 at 9:48 pm #287275

Joethemonkey101
@joethemonkey101

@Codemattic I’ll try that

@Techy I’ll try that as well, thanks

June 20, 2010 at 9:51 pm #287276

Joethemonkey101
@joethemonkey101

I added menu.position = ccp (0, 0 ), and the button shows up, but clicking on it doesn’t do anything. Also, a duplication of my picture shows up.

That’s a pic of the left side of the iphone simulator

June 22, 2010 at 2:08 am #287277

Joethemonkey101
@joethemonkey101

Ok, here I changed it from a sprite to a CCLabel. I think this would look better, but when i’m in the simulator, it crashes at the page.

Also, it’s proving difficult keeping track between the actual sprite and the menu sprite. I think i might be mixing them up. I named the sprites actualSprite and menuSprite to help tell between them.

June 22, 2010 at 9:11 am #287278

T.Hieu
@thieu

@Joethemonkey101:

1. …but clicking on it doesn’t do anything

–> It’s because you don’t have a “HelloWorld” class. “HelloWorldScene” is the right one.

2. …a duplication of my picture shows up.

–> Because the menu item “info” uses the “help” sprite which inits with “Instruction.png” image so you have a another image there.

don’t need to add “help” sprite anymore when you use it as a menu item image.

3. …when i’m in the simulator, it crashes at the page.

–> wrong syntax. You can not add “menuarrow” to itself.

Take a look at the cocos2d example project: “MenuTest” class. Everything’s there.

June 22, 2010 at 9:30 am #287279

CJ
Moderator
@wiseganesha

Please post code as text and wrap it in backticks to make it display correctly in the forums.

June 22, 2010 at 1:11 pm #287280

Joethemonkey101
@joethemonkey101

@T.Hieu thanks so much for the answers! EDIT – when i change HelloWorld scene to HelloWorldScene scene, it says HelloWorldScene undeclared, even though i imported it at the header. Also, what do you mean by i have two pictures? My only picture is Instructions.png. Would it be easier to just add a CCLabel that replaced Instructions? Then I could add a background.

@CJ how do I do that?

June 22, 2010 at 1:28 pm #287281

CJ
Moderator
@wiseganesha

When you post you can see there is a button that says “code” it will insert backticks for you. Put your code between them.

June 22, 2010 at 1:28 pm #287282

Joethemonkey101
@joethemonkey101

CCSprite *sprite;

Thanks!

June 23, 2010 at 1:10 pm #287283

Joethemonkey101
@joethemonkey101

Ok so here’s the code

#import "Instructions.h"
#import "HelloWorldScene.h"

@implementation Instructions

CCSprite *help;
CCMenu *menu;
CCMenuItemSprite *info;
CCLabel *actualArrow;
CCMenuItemLabel *menuArrow;

-(id) init {

if( (self=[super initWithColor:ccc4(10, 100, 130, 130)]) )
{
actualArrow = [CCLabel labelWithString:@"Go Back" fontName:@"Marker Felt" fontSize:22];
menuArrow = [CCMenuItemLabel itemWithLabel:menuArrow target:self selector:@selector(menuArrow:)];
help = [CCSprite spriteWithFile: @"Instructions.png"];
info = [CCMenuItemSprite itemFromNormalSprite:help selectedSprite:help];
menu = [CCMenu menuWithItems:info, menuArrow, nil];
actualArrow.position = CGPointZero;
menu.position = ccp(0, 0);
actualArrow.position = ccp(23, 23);
help.position = ccp(240, 160);
[self addChild:menu];
[self addChild:help];
[self addChild:actualArrow];

}
return self;
}

-(void) actualArrow: (id) sender {
CCDirector sharedDirector] replaceScene:[CCFlipAngularTransition transitionWithDuration:1.2f scene:[HelloWorldScene scene];
}

-(void) help: (id) sender {

}
@end

June 23, 2010 at 8:00 pm #287284

Joethemonkey101
@joethemonkey101

What does “Class CCMenuItemLabel does not implement the CCLabelProtocol protocol” mean?

June 24, 2010 at 11:05 pm #287285

Joethemonkey101
@joethemonkey101

When I go to that page in the simulator it just crashes.

June 24, 2010 at 11:45 pm #287286

Codemattic
Moderator
@codemattic

menuArrow = [CCMenuItemLabel itemWithLabel:menuArrow target:self selector:@selector(menuArrow:)];

wait, what?

@selector(menuArrow:) – do you have a method called menuArrow: in this class?

menuArrow =itemWithLabel:menuArrow – you are trying to initialize menuArrow with menuArrow?

June 25, 2010 at 12:54 am #287287

Joethemonkey101
@joethemonkey101

@Codemattic I’m not really sure about how to add this CCLabel. I just want a CCLabel and I’m not really sure how to initialize it. And no, I don’t have a method called menuArrow. How would I change my code to do that?

June 25, 2010 at 1:33 am #287288

Joethemonkey101
@joethemonkey101

I changed my code and here’s what I got, but when I click on the page, it still crashes.

#import "Instructions.h"
#import "HelloWorldScene.h"

@implementation Instructions

CCMenu *tut;
CCMenuItemImage *info;
CCMenuItemFont *back;

-(id) init {

if( (self=[super initWithColor:ccc4(10, 100, 130, 130)]) )
{
[CCMenuItemFont setFontName:@"American Typewriter"];
[CCMenuItemFont setFontSize:25];
back = [CCMenuItemFont itemFromString:@"Go Back" target:self selector:@selector(one:)];
info = [CCMenuItemImage itemFromNormalImage:@"Instructions.png" selectedImage:@"Instructions.png" target:self selector:@selector(two:)];
tut = [CCMenu menuWithItems:info, back, nil];
tut.position = ccp(0, 0);
back.position = ccp(23, 23);
info.position = ccp(240, 160);
[self addChild:tut];

}
return self;
}

-(void) back: (id) sender {
CCDirector sharedDirector] replaceScene:[CCFlipAngularTransition transitionWithDuration:1.2f scene:[HelloWorld scene];
}

-(void) help: (id) sender {

}
@end

June 25, 2010 at 2:22 am #287289

T.Hieu
@thieu

@Joethemonkey101: I changed my code and here’s what I got, but when I click on the page, it still crashes

back = [CCMenuItemFont itemFromString:@"Go Back" target:self selector:@selector(one:)];
info = [CCMenuItemImage itemFromNormalImage:@"Instructions.png" selectedImage:@"Instructions.png" target:self selector:@selector(two:)];

–> Where are your “one:” and “two:” methods. Change them to “back:” and “help:”

“it says HelloWorldScene undeclared”

–> really not sure what’s wrong. Can you tell me what classes do you have on this project?

June 25, 2010 at 3:08 am #287290

Codemattic
Moderator
@codemattic

@Joethemonkey101 when you have ... target:anObject selector:@selector(aMethodName:) ... what happens is when the menu is tapped, the method -aMethodName: of object anObject is called. Or another way of saying it is aMethodName: message is sent to anObject. So you have to make sure that anObject has a method:

-(void) aMethodName: (id) sender {
// the user tapped the menu
// do something here!
}

make sense?

June 25, 2010 at 1:25 pm #287291

Joethemonkey101
@joethemonkey101

@T.Hieu I have the classes Gameplay, HighScores, Instructions, Paused, HelloWorldScene, and AppDelegate

@Codemattic Yeah I understand, thanks.

June 25, 2010 at 1:28 pm #287292

Joethemonkey101
@joethemonkey101

I just installed 0.99.4 rc2. When I went to build and go, the simulator crashed on the main menu.

June 25, 2010 at 8:39 pm #287293

Joethemonkey101
@joethemonkey101

EDIT – I installed the latest xcode and everything is fine with the menu. I’m still having a problem with my Instructions page though.

Viewing 23 posts - 1 through 23 (of 23 total)

You must be logged in to reply to this topic.