views:

143

answers:

1

Hi, I know I've probably posted three questions related to this then deleted them, but thats only because I solved them before I got an answer. But, this one I can not solve and I don't believe it is that hard compared to the others. So, with out further ado, here is my problem:

So I am using Cocos2d and one of the major problem is they don't have buttons. To compensate for there lack in buttons I am trying to detect if when a touch ended did it collide with a square (the button). Here is my code:

- (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]];
        }
    }
}

Do you know what I am doing wrong?

A: 

Why dont you just do

 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] returns the CGRect of the node and CGRectContainsPoint checks to see if the CGPoint location is inside of the button.

Ricky Button
Thanks, I tried your code out and only had to make the small change of making a variable called inverseLocation which flipped coordinates of location because of the fact the iphone is rotated while playing the game and use that in place of location.
thyrgle
No problem, glad to help.
Ricky Button