1

self.isTouchEnabled = YES; 当然在init方法中。

-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
}

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

}

在上面的代码中 ccTouchesMoved 工作正常,但 ccTouchMoved 没有调用.. 有什么帮助吗?!

4

2 回答 2

3

Cocos2d 支持两种不同的触摸事件处理方式。它们由两种不同类型的委托定义(均在 CCTouchDelegateProtocol.h 中定义)。

标准触控代理: @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; 

这些事件与您在标准 CocoaTouch 应用程序中获得的事件类型相同。你会得到所有的事件,所有的接触;在多点触控环境中,由您决定您关心哪些触控。要在 CCLayer 子类中获取这些事件,您只需设置 isTouchEnabled = YES,如下所示:

self.isTouchEnabled = YES;

有针对性的触摸代表

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event; 
@optional 
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event; 
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event; 
- (void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event; 

请注意目标和标准触摸代理之间的两个重要区别:

  1. 这些方法只提供一次触摸,而不是一组——因此,方法名称以“ccTouch”而不是“ccTouches”开头。

  2. ccTouchBegan 方法是必需的,它返回一个布尔值。

因此 ccTouchBegan 将为每个可用的触摸单独调用,并且您返回 YES 以指示您关心的触摸。只有 ccTouchBegan 声明的触摸随后会传递到 Moved、Ended 和 Canceled 事件(所有这些都是可选的)。

要接收这些事件,您必须向全局调度程序注册为目标触摸委托。在 CCLayer 子类中,重写 registerWithTouchDispatcher,如下所示:

-(void) registerWithTouchDispatcher {
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0swallowsTouches:YES]; }

(这意味着在文件顶部导入“CCTouchDispatcher.h”)。

使用哪个?

除了更复杂的注册之外,目标触摸委托通常更易于使用,因为您不必自己拆分 NSSet,也不必在 Moved 中不断检查事件是否是您想要的事件/结束/取消的事件。但是,如果您想在一种方法中处理多个触摸(例如,因为您将它们组合成缩放或旋转输入),您可能希望使用标准触摸委托。 请注意,您只能使用其中一种。

于 2013-03-26T08:20:02.680 回答
0

有两种行为:一种用于标准触摸,一种用于多次触摸。您不必这样做addTargetedDelegate:::,您可以简单地将touchMode属性设置为您喜欢的值。CCLayer将为您处理注册。

- (void)onEnter
{
    [self setTouchMode: kCCTouchesAllAtOnce]; //resp kCCTouchesOneByOne
}

在幕后,更改touchMode将禁用然后重新启用触摸,启用触摸 ( enableTouch) 将为您注册正确的委托,方法是调用addStandardDelegateaddTargetedDelegate

于 2013-03-26T08:57:46.740 回答