如何在Zoom State上检测到触摸动作是否在精灵上触摸?
-
02-10-2019 - |
题
首先,我将包含精灵的层缩放。现在,我需要感觉到精灵的触摸。我已经尝试了以下操作,但无法实现目标 -
CGRect tRect= [[aSprite displayedFrame] rect];
if(CGRectContainsPoint(tRect, touchedPosition))
{
NSLog(@"touched:>> touch at (%f,%f)",touchedPosition.x,touchedPosition.y);
// Do something, maybe return kEventHandled;
}
else{
NSLog(@"NOT touched: touch at (%f,%f)",touchedPosition.x,touchedPosition.y);
}
仅供参考:我已经使用了Cocos2D框架
解决方案 3
最后我找到了解决方案:),这是代码
CGPoint location = [touch locationInView: [touch view]];
CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
CGPoint tapPosition = [self convertToNodeSpace:convertedLocation];
“自我”是我以前指定的精灵支架层。这层正在听触摸事件。
其他提示
首先,您需要确保从 UITouch
正确。
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
其次,您需要测试对Sprite的边界框的触摸。
if (CGRectContainsPoint([sprite boundingBox], location)) {
// The sprite is being touched.
}
弗兰克·米切尔(Frank Mitchell)是正确的。另一种方法是将您的听力代码添加到精灵本身中,以便可可为您完成工作。如果实际触摸,它将仅发送Sprite Cctouchesbegan事件。
不隶属于 StackOverflow