2

我使用此代码为我的 cocos2d 项目创建了我的 Button

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

现在我需要将我的精灵跳跃动作与这个按钮联系起来.. 我该怎么做..?

4

2 回答 2

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];
于 2013-05-07T08:22:12.647 回答
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;
}
于 2013-05-07T14:30:36.657 回答