【问题标题】:Square collision detection problem (iPhone)方形碰撞检测问题(iPhone)
【发布时间】:2010-05-22 04:17:05
【问题描述】:

我知道我可能已经发布了三个与此相关的问题然后删除了它们,但这只是因为我在得到答案之前解决了它们。但是,这个我无法解决,我不相信与其他人相比它很难。所以,事不宜迟,这是我的问题:

所以我使用的是 Cocos2d,主要问题之一是它们没有按钮。为了弥补按钮的缺失,我试图检测触摸结束时是否与正方形(按钮)发生碰撞。这是我的代码:

- (void)ccTouchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:touch.view];
    NSLog(@"%f", 240-location.y);
    if (isReady == YES)
    {
        if (((240-location.y) <= (240-StartButton.position.x - 100) || -(240-location.y) >= (240-StartButton.position.x) + 100) && ((160-location.x) <= (160-StartButton.position.y) - 25 || (160-location.x) >= (160-StartButton.position.y) + 25))
        {
            NSLog(@"Coll:%f", 240-StartButton.position.x);
            CCScene *scene = [PlayScene node];
            [[CCDirector sharedDirector] replaceScene:[CCZoomFlipAngularTransition transitionWithDuration:2.0f scene:scene orientation:kOrientationRightOver]];
        }
    }
}

你知道我做错了什么吗?

【问题讨论】:

  • 不确定它是否是故意的,但看起来你的一些变量在接近尾声时被切换了。您正在将 x 与 y 进行比较。
  • @Ed Marty:是的,这是故意的……
  • 你还没有解释出了什么问题。不匹配吗?不应该匹配的时候匹配吗?您根本没有参加活动吗?另外,您是否设置了断点并单步执行,以查看变量取什么值?
  • 好的,问题是当 BOOL isReady == YES 时,只要我在 iPhone 模拟器中放开鼠标,程序就会自动停止并转到 PlayScene 场景。如您所见,我正在使用 NSLog 进行调试。

标签: iphone button cocos2d-iphone collision-detection


【解决方案1】:

你为什么不这样做

 if (isReady == YES)
{
    if (CGRectContainsPoint([StartButton boundingBox],location))
    {

        CCScene *scene = [PlayScene node];
        [[CCDirector sharedDirector] replaceScene:[CCZoomFlipAngularTransition transitionWithDuration:2.0f scene:scene orientation:kOrientationRightOver]];
    }
}

[StartButton boundingBox] 返回节点的 CGRect 和 CGRectContainsPoint 检查 CGPoint 位置是否在按钮内。

【讨论】:

  • 谢谢,我尝试了你的代码,只需要做一个小的改变,即创建一个名为 inverseLocation 的变量,它会翻转位置坐标,因为在玩游戏时 iphone 会旋转并使用它地点。
猜你喜欢
  • 2017-05-30
  • 1970-01-01
  • 2014-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多