当我的 ccLayer 具有 loadNibNamed 时,如何识别 ccTouches 事件

发布于 2024-11-24 14:45:25 字数 592 浏览 2 评论 0原文

我的问题是我没有找到通过 UIScrollView “刺穿”的解决方案,以便 ccLayer 可以识别 ccTouch 事件

   self.isTouchEnabled = YES;
    [[NSBundle mainBundle] loadNibNamed:@"myLayer" owner:self options:nil];

...

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

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
        CGPoint location = [self convertToWorldSpace:[self convertTouchToNodeSpace:touch]];

有什么想法如何创建委托或其他解决方案来绕过 UI 并与 cc 对话?

My problem is that I didn't find a solution to "pierce" through a UIScrollView so the ccLayer could recognize the ccTouch events

   self.isTouchEnabled = YES;
    [[NSBundle mainBundle] loadNibNamed:@"myLayer" owner:self options:nil];

...

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

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
        CGPoint location = [self convertToWorldSpace:[self convertTouchToNodeSpace:touch]];

Any ideas how to create a delegate or another solution to bypass the UI and talk with the cc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你又不是我 2024-12-01 14:45:25

今天早上我在使用 Cocos2D v1.0.0 时遇到了这个问题。我的解决方案是在该层的 init 方法中包含 CCTouchDispatcher 方法调用,然后 UIView 内的该层将识别触摸。

-(id) init
{
  if ((self = [super init]) != nil) {
    // do stuff
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
  }
  return self;
}

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
  CGPoint location = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]]];
  NSLog(@"TouchBegan at x:%0.2f, y:%0.2f", location.x, location.y);

  return YES;
}

另一种解决方案是使用 ccTouchesBegan 方法:

-(id) init
{
  if ((self = [super init]) != nil) {
    // do stuff
    self.isTouchEnabled = YES;
  }
  return self;
}


-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{   
  for (UITouch *thisTouch in touches) {
    CGPoint location = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:[thisTouch locationInView:[thisTouch view]]]];
    NSLog(@"TouchesBegan at x:%0.2f, y:%0.2f", location.x, location.y); 
  }
}

请注意,这两种触摸方法具有不同的方法来让您的应用程序知道它应该响应触摸。您无法混合搭配您想要响应触摸的方式以及您想要观察的触摸。

I had this problem this morning with Cocos2D v1.0.0. My solution was to include the CCTouchDispatcher method call inside my init method for the layer, and then that layer, inside a UIView, would recognize touches.

-(id) init
{
  if ((self = [super init]) != nil) {
    // do stuff
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
  }
  return self;
}

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
  CGPoint location = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]]];
  NSLog(@"TouchBegan at x:%0.2f, y:%0.2f", location.x, location.y);

  return YES;
}

An alternative solution would be to use the ccTouchesBegan method:

-(id) init
{
  if ((self = [super init]) != nil) {
    // do stuff
    self.isTouchEnabled = YES;
  }
  return self;
}


-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{   
  for (UITouch *thisTouch in touches) {
    CGPoint location = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:[thisTouch locationInView:[thisTouch view]]]];
    NSLog(@"TouchesBegan at x:%0.2f, y:%0.2f", location.x, location.y); 
  }
}

Note that the two touch methods have different methods to let your application know it should respond to touches. You can't mix and match how you want to respond to touches, and which touches you want to observe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文