在cocos2d中检测触摸*屏幕上的任何地方*?

发布于 2024-07-29 03:39:52 字数 1061 浏览 4 评论 0原文

真的很抱歉,我意识到有人询问了有关 cocos2d 触摸检测的几个问题(包括 这个答案对我有很大帮助),但我就是无法让它们中的任何一个工作。 我会评论我链接的答案,而不是问我自己的问题,但我没有足够的代表来发表评论。

我想做的就是在用户点击屏幕上的任意位置时立即停止动画。

到目前为止,这是我的代码:

- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"Touches Began");
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[Director sharedDirector] convertCoordinate: location];

    CGRect mySurface = (CGRectMake(100, 100, 320, 480));
    if(CGRectContainsPoint(mySurface, location)) {
        NSLog(@"Event Handled");
        return kEventHandled;
        [[Director sharedDirector] stopAnimation];
       }
     return kEventIgnored;
     NSLog(@"Event Ignored");

}

我在一个层中尝试了 BOOLvoidccTouchesBegantouchesBegan文件和 cocosNode 文件,以及许多其他东西。 什么都没发生。 日志中没有显示任何内容,动画继续以愉快的方式进行。 我究竟做错了什么?

I'm really sorry, I realize there have been several questions asked about cocos2d touch detection (including this answer which helped me a bunch), but I just can't get any of them to work. I would have commented on the answer I linked instead of asking my own question, but i don't have enough rep to leave comments.

All I want to do is stop animation as soon as a user taps anywhere on the screen.

Here's my code so far:

- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"Touches Began");
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[Director sharedDirector] convertCoordinate: location];

    CGRect mySurface = (CGRectMake(100, 100, 320, 480));
    if(CGRectContainsPoint(mySurface, location)) {
        NSLog(@"Event Handled");
        return kEventHandled;
        [[Director sharedDirector] stopAnimation];
       }
     return kEventIgnored;
     NSLog(@"Event Ignored");

}

I've tried both BOOL and void, ccTouchesBegan and touchesBegan, in a layer file and a cocosNode file, and many other things. Nothing happens. Nothing shows in the log, and the animation continues on its merry little way. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

舟遥客 2024-08-05 03:39:52

主要问题是您在 return kEventHandled; 之后而不是之前得到了 [[Director shareDirector] stopAnimation];return 一旦被调用就会退出该函数,因此它之后的任何内容都将永远不会到达。

我面前没有我的Mac来检查你的代码的其余部分,但它看起来很好,所以我猜这是主要问题。 如果您甚至没有看到 NSLog(@"Touches Began"); 那么您需要确保您是在扩展 CocosNode 中执行此操作>图层。

另一个有用的东西(一旦您看到触摸)是 NSStringFromCGPoint 函数,它允许您轻松显示和调试 CGPoint 中的值,这样您就可以做一些事情喜欢:

NSLog(@"This layer was touched at %@", NSStringFromCGPoint(location));

The main problem is that you've got the [[Director sharedDirector] stopAnimation]; after the return kEventHandled; rather than before it. return exits the function as soon as it's called, so anything after it will never get reached.

I don't have my mac in front of me to check the rest of your code, but it seems fine, so I'm guessing that's the main problem. If you're not even seeing the NSLog(@"Touches Began"); then you need to make sure that you're doing this in a CocosNode that extends Layer.

Another useful thing(once you're seeing the touches) is the NSStringFromCGPoint function, which allows you to easily display and debug the values in a CGPoint, so you could do something like:

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