【问题标题】:Problem with cocos2D for iPhone and touch detectioncocos2D for iPhone 和触摸检测的问题
【发布时间】:2010-09-27 12:38:31
【问题描述】:

我只是不明白。 我使用 cocos2d 在 iPhone/Pod 上开发一个小游戏。该框架很棒,但我在触摸检测方面失败了。我读到您只需要在子类 CocosNode 的类的实现中覆盖正确的函数(例如“touchesBegan”)。但它不起作用。我会做错什么?

功能:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{NSLog(@"tickle, hihi!");}

我完全搞错了吗?

【问题讨论】:

    标签: iphone cocos2d-iphone touch detection


    【解决方案1】:

    为了检测触摸,您需要从 UIResponder 子类化(UIView 也是如此)。我对 cocos2D 不熟悉,但是快速浏览一下文档就会发现 CocosNode 不是从 UIResponder 派生的。

    经过进一步调查,看起来 Cocos 人创建了一个派生自 CocosNode 的 Layer 类。该类实现了触摸事件处理程序。但是那些都是以cc为前缀的。

    见http://code.google.com/p/cocos2d-iphone/source/browse/trunk/cocos2d/Layer.h

    有关更多信息,另请参阅 menu.m 代码和以下博客文章:

    http://blog.sapusmedia.com/2008/12/cocos2d-propagating-touch-events.html

    【讨论】:

      【解决方案2】:

      Layer 是唯一的 cocos2d 类。

      诀窍是 Layer 的所有实例一个接一个地传递触摸事件,所以你的代码必须处理这个。

      我是这样做的:

      -(BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
      UITouch *touch = [touches anyObject];
      CGPoint location = [touch locationInView: [touch view]];
      CGPoint cLoc = [[Director sharedDirector] convertCoordinate: location];
      
      float labelX = self.position.x - HALF_WIDTH;
      float labelY = self.position.y - HALF_WIDTH;
      float labelXWidth = labelX + WIDTH;
      float labelYHeight = labelY + WIDTH;
      
      if( labelX < cLoc.x &&
          labelY < cLoc.y &&
          labelXWidth > cLoc.x &&
          labelYHeight > cLoc.y){
              NSLog(@"WE ARE TOUCHED AND I AM A %@", self.labelString);
              return kEventHandled;
          } else {
              return kEventIgnored;
          }
      

      }

      请注意,cocos2d 库有一个“ccTouchesEnded”实现,而不是 Apple 标准。它允许您返回一个 BOOL 指示您是否处理了事件。

      祝你好运!

      【讨论】:

      • 你可以让任何CCNode类接收触摸!使用,例如:[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0swallowsTouches:NO];
      • 类必须实现两个 TouchDelegate 协议之一
      【解决方案3】:

      maw,CGPoint 结构成员 x,y 是浮点数。使用 @"%f" 为 printf/NSLog 格式化浮点数。

      【讨论】:

        【解决方案4】:

        您是否已将此添加到您的图层 init 方法中?

            // isTouchEnabled is an property of Layer (the super class).
            // When it is YES, then the touches will be enabled
            self.isTouchEnabled = YES;
        
            // isAccelerometerEnabled is property of Layer (the super class).
            // When it is YES, then the accelerometer will be enabled
            self.isAccelerometerEnabled = YES;
        

        【讨论】:

          【解决方案5】:

          如果你使用 0.9 beta 的 cocos2D,它对 CocosNodes 有一个非常简单的触摸检测。这种新检测的真正美妙之处在于它可以很好地处理多点触摸跟踪。

          可以在这里找到一个例子

          http://code.google.com/p/cocos2d-iphone/source/browse/#svn/trunk/tests/TouchesTest

          【讨论】:

            【解决方案6】:
            - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
            {
            
                    //Add a new body/atlas sprite at the touched location
                    CGPoint tapPosition;
                    for( UITouch *touch in touches ) {
                        CGPoint location = [touch locationInView: [touch view]];
            
                        tapPosition = [self convertToNodeSpace:[[CCDirector sharedDirector] convertToGL:location]];     // get the tapped position
            
            
            
            
            
                }
            }
            

            认为这可以帮助你....

            【讨论】:

              【解决方案7】:

              -让你的场景符合协议CCTargetedTouchDelegate -将此行添加到您场景的init:

              [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
              

              -实现这些功能:

              - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
               {
                 return  YES;
               }
               -(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
                {
                  //here touch is ended
                }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-05-02
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多