【问题标题】:activate ccTouchesBegan with a button in cocos2d and make it work just one time使用 cocos2d 中的按钮激活 ccTouchesBegan 并使其工作一次
【发布时间】:2012-01-10 17:54:51
【问题描述】:

我想用 ccTouchesBegan 做一些事情,但我想将该操作限制为一个按钮,这意味着按钮的操作应该触发 ccTouchesBegan 但只有一次。 ccTouchesBegan里面的代码完成后,界面应该会恢复正常,等待按钮被按下再次触发动作。

我已经让按钮在按下时触发 ccTouchesBegan,但问题是,一旦按下按钮,ccTouchesBegan 中的代码就会继续工作,并且从那时起,当模拟器上的触摸动作完成时,它会继续执行相同的操作。

这是我目前的代码。

这是我创建的标志方法,以便我知道按钮已被按下并且我可以控制 ccTouchesBegan 上的操作。

- (void) selector{
    click = true;
  }

这是调用方法选择器的按钮。

- (void) button{
CCMenuItemImage *touchesMovedButton = [CCMenuItemImage itemFromNormalImage:@"ActionButton-Normal.png" 
                                                         selectedImage:@"ActionButton-Selected.png"
                                                                target:self 
                                                              selector:@selector(selector)
                                                              ];      

        CCMenu *selectorButton = [CCMenu menuWithItems: touchesMovedButton, nil];

        selectorButton.position = ccp(64, 64);

        [self addChild: selectorButton];
}

这是 ccTouchesBegan 方法

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ 

UITouch *touch = [touches anyObject];

//this part doesn't let the interface start the ccTouchesBegan code until the button has been pressed.
if (click == false) {
            return;       
        }
        // the else code starts when the button has been pressed.
        else{
            CCLOG(@"you have touched the interface!!");
        }
}

这就是代码,在按下按钮之前界面不会执行任何操作,但在按下按钮后,每次我触摸界面时它都会继续打印 CCLOG。我只想让它做一次,然后当我再次按下按钮时,它应该再做一次。

有人知道怎么做吗?或者也许指出我的错误?

【问题讨论】:

    标签: iphone xcode cocos2d-iphone


    【解决方案1】:

    你只是有逻辑错误。在 ccTouchesBegan 中使用下面的代码它会根据您的需要完美运行...

    if (click == false) 
    {
        return;       
    }
    // the else code starts when the button has been pressed.
    else if(click == true)
    {
        CCLOG(@"you have touched the interface!!");
        click=false; // You need to make false if you want to make touch single time enabled
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-08
      • 2021-04-20
      • 1970-01-01
      • 2019-07-18
      • 1970-01-01
      • 2019-05-25
      • 2019-10-15
      • 2012-12-11
      相关资源
      最近更新 更多