mm2*_*m24 2 iphone sprite uiview cocos2d-iphone uitouch
我正在使用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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是坐标错了.locationInView以不同方式对其进行翻译.我正在使用landscapeleft视图(kCCDeviceOrientationLandscapeLeft).
在函数上添加一个断点并查看myTouch变量,然后我看到它有一个名为locationInWindow的成员变量,它反映了实际的触摸位置(这就是我想要的).
我试图访问locationInWindow,但没有getter方法.我怎么能这样做?
致以真诚的感谢和诚挚的问候
Window是UIWindow一个UIView子类.此外UITouch还有一个window属性.所以你可以尝试:
CGPoint touchPosition = [myTouch locationInView:myTouch.window];
Run Code Online (Sandbox Code Playgroud)
视图的变换数字计算在内; 所以你可能也想尝试self.superview参数.
| 归档时间: |
|
| 查看次数: |
3706 次 |
| 最近记录: |