[英]How can I detect touch in cocos2d?
我正在使用cocos2d为iPhone开发一款2D游戏。
我在游戏中使用了很多小精灵(图像)。 我想触摸两个相似类型的精灵(图像),然后两个精灵(图像)将被隐藏。
如何检测特定精灵(图像)中的触摸?
更好的方法是实际使用sprite本身的边界框(这是一个CGRect)。 在这个示例代码中,我将所有精灵放在NSMutableArray中,并且我简单地检查精灵触摸是否在边界框中。 确保在init中打开触摸检测。 如果你注意到我也通过返回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;
}
按照乔纳斯的指示,再添加一点......
- (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 / ya以适应Cocos中的“居中定位”
在包含您的精灵的图层中,您需要说:
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.
}
@david,你的代码有一些关于cocos 0.7.3和2.2.1的拼写错误,特别是CGRectMake而不是CGMakeRect和[touch location]现在是[touch 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;
}
}
这是一个很好的教程,解释了基本的触摸系统http://ganbarugames.com/2010/12/detecting-touch-events-in-cocos2d-iphone/
首先,写
self.isTouchEnabled = YES;
那么,你需要实现ccTouchesEnded,ccTouchesBegan等功能
根据我的理解,你希望能够“匹配”两个可以在屏幕上不同坐标上的精灵。
这样做的方法.. :(我确定还有很多其他方法)
考虑有2个全局变量。
因此,每当触摸触摸精灵时,您都会使用多次提到的CGRectContainsPoint函数来查找已触摸的精灵。 然后,您可以将该精灵的“标记”保存在其中一个全局变量中。
您在第二次触摸时执行相同操作,然后比较2个全局变量。
如果你有问题,你应该能够弄清楚其余的但是要评论。
这是它对我有用的方法......其中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在CocosLand中工作,因为上面有2行调用:
[[Director sharedDirector] convertCoordinate:]
Cocos2D对象将使用OpenGL坐标系,其中0,0是左下角,UIKit坐标(如触摸发生的位置)左上角有0,0。 convertCoordinate:正在为您从UIKit翻转到OpenGL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.