Button 按下按钮时的操作Cocos2d 3.0

Button 按下按钮时的操作Cocos2d 3.0,button,cocos2d-iphone,Button,Cocos2d Iphone,当按下按钮时,而不是当按下按钮时,我如何触发右按钮点击:(id)发送方 这是按钮的代码 rightButton = [CCButton buttonWithTitle:@"" spriteFrame:[CCSpriteFrame frameWithImageNamed:@"right.png"]]; rightButton.position = ccp(self.contentSize.width/3.65, self.contentSize.height/10); [rightButton s

当按下按钮时,而不是当按下按钮时,我如何触发右按钮点击:(id)发送方

这是按钮的代码

rightButton = [CCButton buttonWithTitle:@"" spriteFrame:[CCSpriteFrame frameWithImageNamed:@"right.png"]];
rightButton.position = ccp(self.contentSize.width/3.65, self.contentSize.height/10);
[rightButton setTarget:self selector:@selector(onRightButtonClicked:)];
[self addChild:rightButton];
然后按下按钮后会发生什么

- (void)onRightButtonClicked:(id)sender
{
CCActionAnimate *animationAction = [CCActionAnimate actionWithAnimation:walkAnim];
CCActionRepeatForever *repeatingAnimation = [CCActionRepeatForever actionWithAction:animationAction];
[dino runAction:repeatingAnimation];
}

恐怕这个没有1-2行,如果您选中
CCButton.m
您可以看到动作是何时触发的

- (void) touchUpInside:(UITouch *)touch withEvent:(UIEvent *)event
{
    [super setHitAreaExpansion:_originalHitAreaExpansion];

    if (self.enabled)
    {
        [self triggerAction];
    }

    self.highlighted = NO;
}
真是肮脏的快速修复 如果您希望游戏中的所有按钮都具有这种行为,只需将
[自触发动作]
移动到
-(无效)触摸输入:(UITouch*)触摸事件:(UIEvent*)事件
CCButton

优雅的解决方案
创建
CCButton
的自定义子类,其中覆盖
touchUpInside
touchEntered
方法。但是由于这些不是公共的,您必须创建一个
CCButton\u Protected.h
头,在其中导入正常的
CCButton
头,并将要覆盖的私有方法签名放在其中。

我决定采用脏方法,因为我希望所有的按钮都这样做,谢谢!:不客气。如果您有时间,我建议您实施适当的解决方案,主要是因为当您想要更新Cocos时,它会给您带来问题。如果要继续使用“脏”解决方案,则必须重新实现这些更改。我还有一个问题,如何使操作仅在按下按钮时发生?覆盖触摸输入以触发action@Tibor乌德瓦里:回答得很好!