Search code examples
ioscocos2d-iphonetouchcgrect

Cocos2D touches with CGRects?


I am trying to essentially split the screen into four different parts with four different rects like so:

    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.width;
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.height;

    CGRect jumpleftRect = CGRectMake(0, screenHeight/2, screenWidth/2, screenHeight/2); //1
    CGRect runleftRect = CGRectMake(0, 0, screenWidth/2, screenHeight/2); //2
    CGRect jumprightRect = CGRectMake(screenWidth/2, screenHeight/2, screenWidth/2, screenHeight/2); //3
    CGRect runrightRect = CGRectMake(screenWidth/2, 0, screenWidth/2, screenHeight/2); //4

And then I am cycling through a touchArray and checking if the touch location was within, lets say for example for this question, the first rect:

NSArray *touchArray = [NSMutableArray arrayWithArray:touchArray];
    for (UITouch *touchInArray in touchArray) {
        CGPoint toucharrayPosition = [touchInArray locationInNode:self];
        if (CGRectContainsPoint(jumpleftRect, toucharrayPosition)) { //1st rect }

However it seems like the rect is in the completely wrong spot. This never gets called even though my finger is clearly on the top left part of the screen. My anchorPoints are untouched and normal so I am not sure why this isn't working. Is there some specific reason why the touches aren't registering as in the rects even though in retrospect they should be?


Solution

  • Not sure if this was new behavior in iOS 8 but I just reversed my screenWidth and screenHeight to this:

    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    

    Now things are working very smoothly and nicely.