objective-c 如何在 cocos2d 中检测触摸?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/637743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:14:23  来源:igfitidea点击:

How can I detect touch in cocos2d?

iphoneobjective-ccocos2d-iphone

提问by Md Nasir Uddin

I am developing a 2d game for iPhone by using cocos2d.

我正在使用 cocos2d 为 iPhone 开发 2d 游戏。

I use many small sprite (image) in my game. I want to touch two similar types of sprite(image) and then both sprite(image) will be hidden.

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

How can I detect touch in a specific sprite(image) ?

如何检测特定精灵(图像)中的触摸?

回答by Terence

A better way to do this is to actually use the bounding box on the sprite itself (which is a CGRect). In this sample code, I put all my sprites in a NSMutableArray and I simple check if the sprite touch is in the bounding box. Make sure you turn on touch detection in the init. If you notice I also accept/reject touches on the layer by returning YES(if I use the touch) or NO(if I don't)

一个更好的方法是实际使用精灵本身的边界框(它是一个 CGRect)。在此示例代码中,我将所有精灵放在 NSMutableArray 中,并简单检查精灵触摸是否在边界框中。确保在 init.d 中打开触摸检测。如果您注意到我还通过返回 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;
}

回答by David Higgins

Following Jonas's instructions, and adding onto it a bit more ...

遵循乔纳斯的指示,并添加更多内容......

- (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;
   }
}

You may need to adjust the x/y a little to account for the 'centered positioning' in Cocos

您可能需要稍微调整 x/ya 以解决 Cocos 中的“居中定位”

回答by Jonas

In your layer that contains your sprite, you need to say:

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

self.isTouchEnabled = YES;

then you can use the same events that you would use in a UIView, but they're named a little differently:

然后您可以使用在 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.
}

回答by John

@david, your code has some typos for cocos 0.7.3 and 2.2.1, specifically CGRectMake instead of CGMakeRect and [touch location] is now [touch locationInView:touch.view].

@david,您的代码在 cocos 0.7.3 和 2.2.1 中有一些拼写错误,特别是 CGRectMake 而不是 CGMakeRect,并且 [touch location] 现在是 [touch locationInView:touch.view]。

here's what I did:

这是我所做的:

- (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;
    }
}

回答by agfa555

Here's how it worked for me... Where spriteSize is obviously the sprite's size... :P

这是它对我的工作方式......其中 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;
    }
}

回答by penguinsource

this is a good tutorial explaining the basic touch system http://ganbarugames.com/2010/12/detecting-touch-events-in-cocos2d-iphone/

这是一个很好的教程,解释了基本的触摸系统 http://ganbarugames.com/2010/12/detecting-touch-events-in-cocos2d-iphone/

first, write

首先,写

self.isTouchEnabled = YES;

then, you need to implement the functions ccTouchesEnded, ccTouchesBegan, etc

然后,你需要实现ccTouchesEnded、ccTouchesBegan等函数

from what I understood, you want to be able to 'match' two sprites that can be on different coordinates on the screen.

据我了解,您希望能够“匹配”屏幕上不同坐标上的两个精灵。

a method for doing this.. : (im sure theres many other methods)

这样做的方法.. :(我确定还有许多其他方法)

consider having 2 global variables.

考虑有 2 个全局变量。

so everytime a touch touches a sprite, you use the CGRectContainsPoint function that is mentioned several times to find which sprite has been touched. then, you can save the 'tag' of that sprite in one of the global variables.

因此,每次触摸触摸精灵时,您都可以使用多次提到的 CGRectContainsPoint 函数来查找触摸了哪个精灵。然后,您可以将该精灵的“标签”保存在全局变量之一中。

You do the same for the second touch, and then you compare the 2 global variables.

您对第二次触摸执行相同操作,然后比较两个全局变量。

you should be able to figure out the rest but comment if you have problems.

你应该能够弄清楚其余的,但如果你有问题,请发表评论。

回答by BigSprocket

@Genericrich: CGRectContainsPoint works in CocosLand because of the call 2 lines above:

@Genericrich:CGRectContainsPoint 在 CocosLand 中工作,因为上面的调用 2 行:

[[Director sharedDirector] convertCoordinate:]

The Cocos2D objects will be using the OpenGL coordinate system, where 0,0 is the lower left, and UIKit coordinates (like where the touch happened) have 0,0 is upper left. convertCoordinate: is making the flip from UIKit to OpenGL for you.

Cocos2D 对象将使用 OpenGL 坐标系,其中 0,0 是左下角,而 UIKit 坐标(例如触摸发生的位置)的 0,0 是左上角。convertCoordinate:正在为您从 UIKit 转换到 OpenGL。