Cocos2d处理触摸多层

时间:2012-11-02 21:57:28

标签: ios cocos2d-iphone touch

我正在尝试为其他人建议在线启用图层触控:

        hudLayer = [[[CCLayer alloc] init] autorelease];
        [self addChild:hudLayer z:3];

        gameLayer = [[[CCLayer alloc] init] autorelease];
        [self addChild:gameLayer z:1];
        gameLayer.isTouchEnabled = YES;

        rileyLayer = [[[CCLayer alloc] init]autorelease];
        [self addChild:rileyLayer z:2];

        pauseMenu = [[[CCLayer alloc] init] autorelease];
        [self addChild:pauseMenu z:4];

        [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:hudLayer priority:0 swallowsTouches:YES];

我的触摸方法在这里:

- (BOOL)ccTouchBegan:(NSSet *)touch withEvent:(UIEvent *)event {
    return  TRUE;
}

- (void)ccTouchEnded:(NSSet *)touch withEvent:(UIEvent *)event {
    if (!paused) {
        ratMove = 0;
    }
}

然而,这会不断引发错误:由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'Layer #ccTouchBegan覆盖我'

我可以在网上找到这个错误的唯一原因是,如果你不包括ccTouchBegan功能,不过我是,还有其他人知道出现此错误的其他原因吗?

2 个答案:

答案 0 :(得分:1)

子类CCLayer有hud层,然后在里面实现这些方法。

您将hud图层添加为目标委托,然后必须至少实现ccTouchBegan:withEvent:方法。如果您希望您的hud成为目标委托,请创建CCLayer子类并从目标触摸委托协议实现那些方法。

答案 1 :(得分:0)

您的功能未实现相应的签名。尝试:

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    // your stuff here
}

如果你想要多次触摸处理(你的签名),你应该添加标准删除而不是targetedTouchDelegate。

编辑:现在在objective-c:

[[CCDirector sharedDirector].touchDispatcher addStandardDelegate:self priority:0];

触摸调度程序实现了两个协议。您当前正在注册为targetTouchDelegate,但实现了standardDelegate的委托方法。如果您想保留您的方法,请使用上面的行进行注册。

编辑2:现在是协议的确切语法,直接来自cocos的代码。如您所见,没有ccTouchBegan与NSSet(您的签名)但是INSTEAD ccTouchesBegan。无论您喜欢哪种处理方法(标准的目标),您都必须遵守以下协议。

@protocol CCTargetedTouchDelegate

/** Return YES to claim the touch.
 @since v0.8
 */
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event;
@optional
// touch updates:
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event;
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event;
- (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event;
@end

/**
 CCStandardTouchDelegate.

 This type of delegate is the same one used by CocoaTouch. You will receive all the  events (Began,Moved,Ended,Cancelled).
 @since v0.8
*/
@protocol CCStandardTouchDelegate <NSObject>
@optional
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
@end