如何在缩放状态下检测触摸动作是否触及精灵?

发布于 2024-09-08 03:11:17 字数 433 浏览 0 评论 0原文

首先,我将包含精灵的图层放大。 现在我需要感知精灵上的触摸。 我尝试如下,但无法达到目标-

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何其悲哀 2024-09-15 03:11:17

首先,您需要确保从 UITouch 正确获取位置。

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

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

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

First, you need to make sure you get the location from the UITouch correctly.

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

Second, you need to test your touch against the sprite's bounding box.

if (CGRectContainsPoint([sprite boundingBox], location)) {
    // The sprite is being touched.
}
伴我心暖 2024-09-15 03:11:17

弗兰克·米切尔是正确的。另一种方法是将您的监听代码添加到精灵本身,以便 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.

软糖 2024-09-15 03:11:17

最后我找到了解决方案:),这里是代码

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

CGPoint tapPosition = [self convertToNodeSpace:convertedLocation];

,其中“self”是我之前指定的精灵持有者层。该层正在监听触摸事件。

At last I have found the solution :), here is the code

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

CGPoint tapPosition = [self convertToNodeSpace:convertedLocation];

where 'self' is the sprite holder layer as I have specified before. This layer is listening the touch event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文