Iphone 如何在cocos2d中将操作添加到按钮
我使用以下代码创建了cocos2d项目的按钮Iphone 如何在cocos2d中将操作添加到按钮,iphone,ios,cocos2d-iphone,Iphone,Ios,Cocos2d Iphone,我使用以下代码创建了cocos2d项目的按钮 CCMenuItem *starMenuItem = [CCMenuItemImage itemFromNormalImage:@"ButtonStar.jpg" selectedImage:@"ButtonStarSel.jpg" target:self selector:@selector(starButtonTapped:)]; starMenuItem.position = ccp(60, 60); CCMenu *starMen
CCMenuItem *starMenuItem = [CCMenuItemImage
itemFromNormalImage:@"ButtonStar.jpg" selectedImage:@"ButtonStarSel.jpg"
target:self selector:@selector(starButtonTapped:)];
starMenuItem.position = ccp(60, 60);
CCMenu *starMenu = [CCMenu menuWithItems:starMenuItem, nil];
starMenu.position = CGPointZero;
[self addChild:starMenu];
现在我需要将我的精灵跳跃动作与此按钮链接。。我该怎么做?你可以这样做
CCLabelBMFont *startLabel = [CCLabelBMFont labelWithString:@"Start Game" fntFile:@"Arial.fnt"];
CCMenuItemLabel *startItem = [CCMenuItemLabel itemWithLabel:startLabel target:self selector:@selector(startGameTapped:)];
startItem.scale = 1;
CCMenu *menu = [CCMenu menuWithItems:startItem,nil];
[menu alignItemsVerticallyWithPadding:20];
menu.position = ccp( winSize.width/2, winSize.height/2);
[self addChild:menu];
现在您必须实现您在代码中称为的按钮按下处理程序方法 选择器:@选择器(starButtonTapped:) 对于这个跳跃示例,您必须在前面的init中执行此操作:
当您点击按钮时,您的回拨是否
startButtonTapped:
没有被呼叫?
- (void) starButtonTapped: (CCNode *) sender {
CCLOG(@"you are here: starButtonTapped");
// implement your action for button pressing, eg.:
if (!blLetsJump) {
blLetsJump = TRUE;
CCSprite *s = (CCSprite *)[self getChildByTag:100];
CCSequence *seq = [CCSequence actions:
[CCJumpBy actionWithDuration:1.0 position:CGPointZero height:150 jumps:1],
[CCCallBlock actionWithBlock:
^{
blLetsJump = FALSE;
}], nil];
[s runAction:seq];
}
}
-(id) init {
if( (self=[super init]) ) {
CCSprite *jumpy = [CCSprite spriteWithFile:@"yourjumpinghero.png"];
jumpy.tag = 100;
jumpy.position = ccp(160, 50);
[self addChild:jumpy];
// then add your button
}
return self;
}