ios - userInteractionEnabled = NO 是什么意思,它是如何工作的?

标签 ios objective-c cocos2d-iphone

Cocos 2d-iphone 3.0.我正在使用此代码来检测是否触摸了单个 Sprite

-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
  
    CGPoint location = [touch locationInView: [touch view]];
    CGPoint convertedlocation = [[CCDirector sharedDirector] convertToGL: location];
    CGPoint convertedNodeSpacePoint = [self convertToNodeSpace:convertedlocation];
    
    if (CGRectContainsPoint([_sprite boundingBox],convertedNodeSpacePoint)) 
    {
        // Remove sprite 
    }
}

我里面有一些代码应该从父级中删除一些其他 Sprite 。从逻辑上讲,当 _sprite

第二次被触摸,应用程序会崩溃,因为其他 Sprite 已经被移除。

我试图使用 _sprite.userInteractionEnabled = NO; 使 _sprite 不可触摸,但这没有效果。

userInteractionEnabled 到底是什么意思,如何使用它来促进 Sprite 上的触摸检测。

在我的场景中处理 Sprite 触摸的最佳方式是什么?

最佳答案

无法禁用 ccsprite 上的交互。你应该做的是:

bool firstTimeClicked=false;

-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    if(firstTimeClicked==true)
    {
        return NO;
    }
    CGPoint location = [touch locationInView: [touch view]];
    CGPoint convertedlocation = [[CCDirector sharedDirector] convertToGL: location];
    CGPoint convertedNodeSpacePoint = [self convertToNodeSpace:convertedlocation];
    if (CGRectContainsPoint([_sprite boundingBox],convertedNodeSpacePoint)) {
        firstTimeClicked=true;
    }
}

关于ios - userInteractionEnabled = NO 是什么意思,它是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23054632/

相关文章:

ios - 无法使用 UIBarButtonItem 显示弹出 Controller

ios - 在 iPhone 上使用蓝牙 5

ios - 在辅助 View Controller 上显示来自网站的 pdf

基于 iPhone View 的应用程序没有核心数据?

ios - 为非 gailbreak 设备中已安装的应用下载和安装 .IPA 文件(开发应用商店)

ios - 无法创建 itunesconnect 沙盒测试器

ios - 如何调用委托(delegate)方法

ios - NSNotificationCenter 在应用程序购买期间发送释放的消息

ios - 滚动背景图像切成两半 - cocos2d

cocos2d-iphone - cocos2d for iphone 物理游戏 v3.0.0