1

首先,我将包含精灵的图层缩放得更大。现在我需要感知精灵的触摸。我已经尝试如下,但无法达到目标 -

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 框架

4

3 回答 3

1

首先,您需要确保从UITouch正确的位置获取位置。

CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];

其次,您需要针对精灵的边界框测试您的触摸。

if (CGRectContainsPoint([sprite boundingBox], location)) {
    // The sprite is being touched.
}
于 2010-07-08T19:45:51.050 回答
0

弗兰克米切尔是正确的。另一种方法是将您的监听代码添加到精灵本身,以便 Cocos 为您完成工作。如果它实际被触摸,它只会发送精灵 ccTouchesBegan 事件。

于 2010-07-11T07:06:09.317 回答
0

最后我找到了解决方案:),这是代码

CGPoint location = [touch locationInView: [touch view]];
CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];

CGPoint tapPosition = [self convertToNodeSpace:convertedLocation];

其中'self'是我之前指定的精灵保持层。该层正在监听触摸事件。

于 2010-07-13T05:59:02.543 回答