方碰撞检测问题(iPhone)

时间:2010-05-22 04:17:06

标签: iphone button cocos2d-iphone collision-detection

我知道我可能已经发布了三个与此相关的问题然后删除了它们,但那只是因为我在得到答案之前解决了它们。但是,这个我无法解决的问题,而且我认为与其他人相比并不难。所以,进一步说,这是我的问题:

所以我使用的是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]];
        }
    }
}

你知道我做错了吗?

1 个答案:

答案 0 :(得分:0)

为什么不这样做

 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位置是否在按钮内。