0

正如许多其他人在网上建议的那样,我正在尝试为图层启用触摸功能:

        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 override me”

我可以在线找到此错误的唯一原因是,如果您没有包含 ccTouchBegan 功能,但是我是,有其他人知道出现此错误的任何其他原因吗?

4

2 回答 2

1

子类 CCLayer 具有 hud 层,然后在其中实现这些方法。

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

于 2012-11-02T22:19:38.727 回答
0

您的函数没有实现适当的签名。尝试:

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

如果您想要多点触摸处理(您的签名),您应该添加StandardDelegate 而不是targetedTouchDelegate。

编辑:现在在objective-c中:

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

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

编辑 2:现在是协议的确切语法,直接来自 cocos 的代码。如您所见,没有使用 NSSet(您的签名)的 ccTouchBegan,而是使用 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
于 2012-11-02T23:43:35.917 回答