我使用此代码
为我的cocos2d项目创建了ButtonCCMenuItem *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];
现在我需要将我的精灵跳跃动作与此按钮链接..我该怎么做...?
答案 0 :(得分:1)
你可以这样做,
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];
答案 1 :(得分:1)
现在你必须实现你在代码中引用的按钮按下处理程序方法 选择器:@selector(starButtonTapped:)
- (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];
}
}
对于这个跳跃示例,您必须先在 init 中执行此操作:
-(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;
}