简体   繁体   English

如何检测cocos2d中的触摸?

[英]How can I detect touch in cocos2d?

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) ? 如何检测特定精灵(图像)中的触摸?

A better way to do this is to actually use the bounding box on the sprite itself (which is a CGRect). 更好的方法是实际使用sprite本身的边界框(这是一个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. 在这个示例代码中,我将所有精灵放在NSMutableArray中,并且我简单地检查精灵触摸是否在边界框中。 Make sure you turn on touch detection in the init. 确保在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) 如果你注意到我也通过返回YES(如果我使用触摸)或否(如果我没有)接受/拒绝图层上的触摸

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

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/ya little to account for the 'centered positioning' in Cocos 您可能需要调整x / ya以适应Cocos中的“居中定位”

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.
}

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

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. 因此,每当触摸触摸精灵时,您都会使用多次提到的CGRectContainsPoint函数来查找已触摸的精灵。 then, you can save the 'tag' of that sprite in one of the global variables. 然后,您可以将该精灵的“标记”保存在其中一个全局变量中。

You do the same for the second touch, and then you compare the 2 global variables. 您在第二次触摸时执行相同操作,然后比较2个全局变量。

you should be able to figure out the rest but comment if you have problems. 如果你有问题,你应该能够弄清楚其余的但是要评论。

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

@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. Cocos2D对象将使用OpenGL坐标系,其中0,0是左下角,UIKit坐标(如触摸发生的位置)左上角有0,0。 convertCoordinate: is making the flip from UIKit to OpenGL for you. convertCoordinate:正在为您从UIKit翻转到OpenGL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM