如何在缩放状态下检测触摸动作是否触及精灵?
首先,我将包含精灵的图层放大。 现在我需要感知精灵上的触摸。 我尝试如下,但无法达到目标-
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 框架
First i have scaled the layer larger which contains the sprites.
Now I need to sense touch on a sprite.
I have tried as follows, but cant reach to goal-
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: I have used cocos2d framework
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,您需要确保从
UITouch
正确获取位置。其次,您需要测试您对精灵边界框的触摸。
First, you need to make sure you get the location from the
UITouch
correctly.Second, you need to test your touch against the sprite's bounding box.
弗兰克·米切尔是正确的。另一种方法是将您的监听代码添加到精灵本身,以便 Cocos 为您完成工作。如果实际被触摸,它只会发送精灵 ccTouchesBegan 事件。
Frank Mitchell is correct. Another approach would be to add your listening code to the sprite itself so that Cocos will do the work for you. It will only send the sprite ccTouchesBegan events if it is actually touched.
最后我找到了解决方案:),这里是代码
,其中“self”是我之前指定的精灵持有者层。该层正在监听触摸事件。
At last I have found the solution :), here is the code
where 'self' is the sprite holder layer as I have specified before. This layer is listening the touch event.