Cocos2d iphone 如何在缩放状态下检测触摸动作是否接触精灵?

Cocos2d iphone 如何在缩放状态下检测触摸动作是否接触精灵?,cocos2d-iphone,iphone-sdk-3.2,Cocos2d Iphone,Iphone Sdk 3.2,首先,我将包含精灵的图层放大。 现在我需要感觉触摸到一个精灵。 我试过以下方法,但达不到目标- CGRect tRect= [[aSprite displayedFrame] rect]; if(CGRectContainsPoint(tRect, touchedPosition)) { NSLog(@"touched:>> touch at (%f,%f)",touchedPosition.x,touchedPosition.y); // Do someth

首先,我将包含精灵的图层放大。 现在我需要感觉触摸到一个精灵。 我试过以下方法,但达不到目标-

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

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

CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
第二,你需要测试你的触摸对精灵的边界框

if (CGRectContainsPoint([sprite boundingBox], location)) {
    // The sprite is being touched.
}

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

CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
第二,你需要测试你的触摸对精灵的边界框

if (CGRectContainsPoint([sprite boundingBox], location)) {
    // The sprite is being touched.
}

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

Frank Mitchell是正确的。另一种方法是将您的监听代码添加到sprite本身,以便Cocos为您完成工作。它将只发送sprite CCTouchesBegined事件,如果它确实被触摸。

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

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

CGPoint tapPosition = [self convertToNodeSpace:convertedLocation];

其中“self”是我前面指定的精灵持有者层。这一层正在监听触摸事件。

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

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

CGPoint tapPosition = [self convertToNodeSpace:convertedLocation];

其中“self”是我前面指定的精灵持有者层。这一层正在监听触摸事件。

我刚刚开始iPhone编程..我刚刚开始iPhone编程。。