2010-07-04 30 views
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); 
} 

FYI:我已经使用cocos2d的框架

+0

我刚刚开始iPhone编程.. – Sadat 2010-07-04 08:08:49

回答

0

最后,我已经找到了解决办法:),这里是代码

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

CGPoint tapPosition = [self convertToNodeSpace:convertedLocation]; 

其中“自我”是精灵座一层我以前规定。该图层正在监听触摸事件。

1

首先,你需要确保你从UITouch正确的位置。

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

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

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

@Mitchell,我已经做到了。 – Sadat 2010-07-12 11:00:53

+0

@Mitchell,感谢您的回复 – Sadat 2010-07-13 06:00:38

0

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

+0

感谢@cc,我会尝试这种技术... – Sadat 2010-07-13 06:00:07