我正在用Cocos2D开发一个迷你iPhone游戏。我想探测到雪碧的触感。为此,我决定不对CCSprite类进行子类,而是使用层类中的触摸事件:
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CCLOG(@"touch began...");
CCSprite *particularSprite = [self getChildByTag:artSprite];
CCNode *nodeClass = (CCNode*) particularSprite;
CGRect DesiredSprite = CGRectMake(nodeClass.positionInPixels.x, nodeClass.positionInPixels.y,particularSprite.contentSize.width , particularSprite.contentSize.height);
for (UITouch *myTouch in touches) {
CGPoint touchPosition = [myTouch locationInView: [myTouch view]];
if(CGRectContainsPoint(DesiredSprite ,touchPosition ))
{
CCLOG(@"Sprite touched");
}
}
}不幸的是坐标是错的。locationInView的翻译方式不同。我正在使用景观景观视图(kCCDeviceOrientationLandscapeLeft)。
在函数上添加一个断点并查看myTouch变量,然后我看到它有一个名为locationInWindow的成员变量,它反映了实际的触摸位置(这正是我想要的)。
我试着访问locationInWindow,但是它没有getter方法。我怎样才能做到呢?
非常感谢和最诚挚的问候
发布于 2012-02-11 16:39:42
窗口是一个UIWindow,它是UIView子类。此外,UITouch还有一个window属性。所以你可以试着:
CGPoint touchPosition = [myTouch locationInView:myTouch.window];视图将图形转换为计算;因此,您可能还需要尝试参数的self.superview。
https://stackoverflow.com/questions/9240988
复制相似问题