views:

1070

answers:

2

Hey guys, i ready many many questions about that point, but i really didn't get the right one yet.

So.. this is the problem..

This is my first project with cocos2d, so sorry for that.

I have one scene called Gameplay inside that i have one Layer with the name Grid and inside of this grid, have many many Block (Layer too).

I need check when you touch one Block, i do this before with Interface Builder, so when i call touchesBegin i have the exact touch in one view. But in cocos2D, i understand you have to check the position of the objects, and not hit test then right?!

So my touchesBegin is like this:

- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    //location = [[Director sharedDirector] convertCoordinate: location];

    for(int i = 0; i < [arrAllBlocks count]; i++)
    {
     for (int j = 0; j < [[arrAllBlocks objectAtIndex:i] count]; j++) 
     {
      Block *tempBlock = [[arrAllBlocks objectAtIndex:i] objectAtIndex:j];
      CGRect mySurface = (CGRectMake(tempBlock.position.x, tempBlock.position.y, tempBlock.contentSize.width,tempBlock.contentSize.height));
      if(CGRectContainsPoint(mySurface, location)) 
      {
       NSLog(@"Hit Positions %fl:%fl",location.x,location.y);
       NSLog(@"Object Positions %fl:%fl",tempBlock.position.x,tempBlock.position.y);
       NSLog(@"Object Color:%@ hited", tempBlock.strName);
      }
     }

    }
}

The first Problem is: This looks upsidedown! When i click in one of this blocks at the first line! I get the block at last line!! I Really dont get that! And the hit not seens perfect to me! And when i convertCoordinate this going even worst!

Someone can help me?! And sorry for bad english =/

A: 

Remember that the coordinate system in Cocos2D is upside down from that of Cocoa. (Really, Cocoa is upside down to me, but it's just a convention).

I forget this all the time myself!

Chris Garrett
+1  A: 

There are two things to remember...

  • Cocos2D coordinate system's origin is bottom left corner.
  • By default, the anchor points of display elements are their centers.

So this is how I solved this problem:

Set the anchor points of the elements to be bottom left as well.

[block setAnchorPoint:ccp(0, 0)];

Use convertCoordinate in ccTouchesBegan.

-(BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];
    CGPoint pt = [touch locationInView:[touch view]];
    CGPoint ptConv = [[Director sharedDirector] convertCoordinate:pt];
}

Calculate the rectangle of the element and compare...

CGSize size = [block contentSize];
CGPoint point = [block position];
rect = CGRectMake(point.x, point.y, size.width, size.height); 

if (CGRectContainsPoint(rect, ptConv)) 
{
    // touched
}

It looks like the only step you are missing is to change the anchor point.

If you prefer not to change the anchor point, you need to change the way rectangle is calculated; adding and subtracting the half of width and height.

Hope it helps.

peacewise