如何访问 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");
}
}
}
不幸的是,坐标是错误的。 locationInView 对其进行了不同的翻译。我正在使用左横向视图(kCCDeviceOrientationLandscapeLeft)。
在函数上添加断点并查看 myTouch 变量,然后我看到它有一个名为 locationInWindow 的成员变量,它反映了实际的触摸位置(这就是我想要的)。
我尝试访问 locationInWindow 但没有 getter 方法。我怎样才能这样做呢?
非常感谢和最诚挚的问候
I am using Cocos2D to develop a mini iPhone game.. I wanted to detect the touch of a sprite. To do so I decided not to subclass the CCSprite class but instead using the touch events in the layer class:
-(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");
}
}
}
Unfortunately the coordinates are wrong. The locationInView translates it differently. I am using the landscapeleft view (kCCDeviceOrientationLandscapeLeft).
Adding a breakpoint on the function and looking at the myTouch variable, then I see that it has a member variable called locationInWindow which reflects the actual touch position (which is what I want).
I tried to access to the locationInWindow but there is no getter method for it. How can I do so?
Many thanks and Best Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Window 是一个
UIWindow
,它是UIView
子类。另外UITouch
还有一个window
属性。所以你可以尝试:视图的转换数字到计算中;因此,您可能还想尝试使用 self.superview 作为参数。
Window is a
UIWindow
which is aUIView
subclass. AdditionallyUITouch
has awindow
property. So you could try:The view's transform figures into the calculation; So you might also want to try
self.superview
for the parameter.