我使用的cocos2d开发用于iPhone 2D游戏。

我在游戏中使用许多小精灵(图片)。我想抚摸两个相似类型的精灵(图),然后这两个精灵(图像)的将被隐藏。

如何可以检测触摸在特定的子画面(图像)?

有帮助吗?

解决方案

在你的层包含您的精灵,你需要说:

self.isTouchEnabled = YES;

然后就可以使用,你会在一个UIView使用相同的事件,但他们名为有点不同:

- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
   UITouch* touch = [touches anyObject];
  //in your touchesEnded event, you would want to see if you touched
  //down and then up inside the same place, and do your logic there.
}

其他提示

一个更好的方法来做到这一点是实际使用上的子画面本身的边界框(这是一个的CGRect)。在此示例代码,我把我的的NSMutableArray和我简单的检查,所有的精灵,如果精灵接触是在边界框。请确保您在init打开触摸检测。通过返回YES(如果使用触摸)或NO如果发现我还接受/拒绝层上的触摸(如果我不)

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

  for (CCSprite *station in _objectList)
  {
    if (CGRectContainsPoint(station.boundingBox, location))
    {
      DLog(@"Found sprite");
      return YES;
    }
  }

  return NO;
}

继纳斯的指示,以及添加到其上多一点...

- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
   UITouch* touch = [touches anyObject];
   CGPoint location = [[[Director sharedDirector] convertCoordinate: touch.location];
   CGRect particularSpriteRect = CGMakeRect(particularSprite.position.x, particularSprite.position.y, particularSprite.contentSize.width, particularSprite.contentSize.height);
   if(CGRectContainsPoint(particularSpriteRect, location)) {
     // particularSprite touched
     return kEventHandled;
   }
}

您可能需要调整的X / Y一点以考虑科科斯了“中心定位”

@大卫,您的代码具有用于茯苓0.7.3和2.2.1,具体的CGRectMake代替CGMakeRect和[触摸位置]一些错字现在[触摸locationInView:touch.view]。

下面是我所做的:

- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];

    CGPoint location = [[Director sharedDirector] convertCoordinate: [touch locationInView:touch.view]];
    CGRect myRect = CGRectMake(sprite.position.x, sprite.position.y, sprite.contentSize.width, sprite.contentSize.height);


    if(CGRectContainsPoint(myRect, location)) {
        // particularSprite touched
        return kEventHandled;
    }
}

@Genericrich:CGRectContainsPoint工作在CocosLand因为呼叫2行以上的:

[[Director sharedDirector] convertCoordinate:]

在Cocos2D中对象将被用OpenGL坐标系中,其中,0,0是左下,和UIKit坐标(像触摸发生的地点)具有0,0是左上角。 convertCoordinate:从UIKit中进行翻转到OpenGL为您服务。

下面是它的工作对我来说... 其中spriteSize明显是精灵的大小...:P

- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];

    CGPoint location = [[Director sharedDirector] convertCoordinate: [touch locationInView:touch.view]];
    CGRect myRect = CGRectMake(sprite.position.x-spriteSize/2, sprite.position.y-spriteSize/2, spriteSize, spriteSize);


    if(CGRectContainsPoint(myRect, location)) {
        // particularSprite touched
        return kEventHandled;
    }
}

这是一个很好的教程说明基本触摸系统 http://ganbarugames.com/2010/12/detecting-触摸事件合的cocos2d-iphone /

第一,写

self.isTouchEnabled = YES;

然后,需要实现的功能ccTouchesEnded,ccTouchesBegan等

这是我的理解,要能“匹配”两个精灵是可以在屏幕上的不同坐标。

用于执行此操作的方法..:(即时确定孤单许多其他方法)

考虑具有2个全局变量。

所以每次触摸倒是一个精灵,您使用多次提到发现这精灵已触及CGRectContainsPoint功能。然后,你可以保存该精灵的“标签”在全局变量之一。

您做第二触摸相同,那么你比较2个全局变量。

你应该能够找出休息,但如果你有问题提出意见。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top