简体   繁体   English

Cocos2d处理触摸多层

[英]Cocos2d handling touch multiple layers

I'm trying to enable touch for layers as many other people have suggested online: 我正在尝试为其他人在网上建议的图层启用触摸功能:

        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];

and my touchmethods are here: 我的触摸方法在这里:

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

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

however this continually throws the error: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Layer#ccTouchBegan override me' 但是,这不断引发错误:由于未捕获的异常“ NSInternalInconsistencyException”而终止应用程序,原因:“ Layer#ccTouchBegan覆盖我”

The only reason I could find for this error online is if you weren't including the ccTouchBegan function, however I am, does anyone else know any other reasons for this error to appear? 我可以在线找到此错误的唯一原因是,如果您不包括ccTouchBegan函数,但是我是,还有其他人知道导致此错误的其他原因吗?

Subclass CCLayer to have hud layer, then inside it implement these methods. 子类CCLayer具有hud层,然后在其中实现这些方法。

You add your hud layer as targeted delegate, then it must implement at least ccTouchBegan:withEvent: method. 将hud层添加为目标委托,然后它必须至少实现ccTouchBegan:withEvent:方法。 If you want your hud to be targeted delegate, make CCLayer subclass and implement there methods from targeted touch delegate protocol. 如果您希望将hud定位为目标委托,则使CCLayer成为子类,并从目标touch委托协议中实现那里的方法。

your function does not implement the appropriate signature. 您的函数未实现适当的签名。 Try: 尝试:

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

if you want multiple touch handling (your signature), you should addStandardDelegate instead of targetedTouchDelegate. 如果要进行多次触摸处理(签名),则应添加StandardDelegate而不是targetedTouchDelegate。

EDIT : and now in objective-c: 编辑:现在在objective-c中:

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

There are two protocols implemented by the touch dispatcher. 触摸分派器实现了两种协议。 You are currently registering as a targetTouchDelegate but implementing the delegate methods of the standardDelegate. 您当前正在注册为targetTouchDelegate,但正在实现standardDelegate的委托方法。 Use the line above to register if you want to keep your methods. 如果要保留方法,请使用上面的行进行注册。

Edit 2 : and now the exact syntax of the protocols, straight from cocos's code. 编辑2:现在直接从cocos的代码中获取协议的确切语法。 As you can see, no ccTouchBegan with NSSet (your signature) BUT INSTEAD ccTouchesBegan. 如您所见,没有带有NSSet(您的签名)的ccTouchBegan,但是没有ccTouchesBegan。 Whichever handling method you prefer (targeted of standard), you must conform to the protocols below. 无论您喜欢哪种处理方法(针对标准),都必须遵守以下协议。

@protocol CCTargetedTouchDelegate @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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM