Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am developing a universal puzzle game, in which I have used separate image for iPhone4 and for iPhone3, iPhone4 image is double in size of iPhone3 image. My issue is for iPhone4, touch area of image for iPhone4 image is taking double size. Suppose I have an image size for iPhone4 of 20x40 and for iPhone3 of 10x20, the touch area for iPhone4 image is 40x80. I don't know how it's happening I worked out in my code but I didn't get any solution of this issue. Touching is working fine in iPhone3, touching within the sprite rectangle.

Can you help me to solve this issue? Or tell me what I am forgetting to code for iPhone4 touching so that it takes touch within sprite rectangle?

Below is my code: `

    -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

    {
     UITouch *touch = [touches anyObject];
     touchLocation = [touch locationInView: [touch view]];
     touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
     touchLocation = [self convertTouchToNodeSpace:touch];
     NSLog(@"touch location x = %f , y =%f",touchLocation.x,touchLocation.y);
     CCSprite * sp= [CCSprite spriteWithFile:@"1.png"];
    sp.position = ccp(touchLocation.x ,touchLocation.y);
    // [self addChild:sp z:100];
    CGRect myrec = [invisible[0] boundingBox];

    if (CGRectContainsPoint(myrec, touchLocation) ) {
    NSLog(@"oneeeeeeee");
    }


     [self selectSpriteForTouch:touchLocation];      
     // return TRUE;    
   }


     - (void)panForTranslation:(CGPoint)translation
    {    
     if (selSprite) {


    CGPoint newPos = ccpAdd(selSprite.position, translation);
    selSprite.position = newPos;

      } 
   }


     - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {   
    UITouch *touch = [touches anyObject];
    Location = [touch locationInView: [touch view]];
     Location = [[CCDirector sharedDirector] convertToGL:Location];
    Location = [self convertTouchToNodeSpace:touch];
    NSLog(@"touch moved location x = %f , y =%f",Location.x,Location.y);

      //touchLocation = [self convertTouchToNodeSpace:touch];

   CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
   oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
   oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];
   CGPoint translation = ccpSub(Location, oldTouchLocation); 

   if (CGRectContainsPoint(selspriterect,Location)) {
    [self panForTranslation:translation];
   }

     if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) 
    {
    NSLog(@"respond to selector");

    CGFloat scale = [[UIScreen mainScreen] scale];

    if (scale > 1.0) 
    {
        NSLog(@"iphone 4s");
        CGPoint mypoint = CGPointMake(80, 357);
        CGPoint mypoint1 = CGPointMake(237, 360);
        CGPoint mypoint2 = CGPointMake(79, 255);
        CGPoint mypoint3 = CGPointMake(237, 247);
        CGPoint mypoint4 = CGPointMake(78, 153);
        CGPoint mypoint5 = CGPointMake(239, 140);


        CCSprite *disablesprite=nil;
        switch (selSprite.tag) {
            case 0:
                if (CGRectContainsPoint( [dest[0] boundingBox] , mypoint ) ) {
                    NSLog(@"oneeeeee collided");
                    dest[0].position =  ccp(192,380);
                    // selSprite = disablesprite;
                }
                break;
            case 1:
                if (CGRectContainsPoint( [dest[1] boundingBox] , mypoint1 ) ) {
                    NSLog(@"two collided");
                    dest[1].position =  ccp(258,379);

                }
                break;
            case 2:
                if (CGRectContainsPoint( [dest[2] boundingBox] , mypoint2 ) ) {
                    NSLog(@"three collided");
                    dest[2].position =  ccp(173,281);
                }
                break;
            case 3:
                if (CGRectContainsPoint( [dest[3] boundingBox] , mypoint3 ) ) {
                    NSLog(@"four collided");
                    dest[3].position = ccp(258,281);

                }
                break;
            case 4:
                if (CGRectContainsPoint( [dest[4] boundingBox] , mypoint4 ) ) {
                    NSLog(@"five collided");
                    dest[4].position =   ccp(166,181);
                }
                break;
            case 5:
                if (CGRectContainsPoint( [dest[5] boundingBox] , mypoint5 ) ) {
                    NSLog(@"six collided");
                    dest[5].position =  ccp(258,176);

                }
                break;
            default:
                break;
        }

    }
    else {

    CGPoint mypoint = CGPointMake(106, 329);
    CGPoint mypoint1 = CGPointMake(208, 329);
    CGPoint mypoint2 = CGPointMake(97, 220);
    CGPoint mypoint3 = CGPointMake(217, 220);
    CGPoint mypoint4 = CGPointMake(92, 124);
    CGPoint mypoint5 = CGPointMake(212, 124);




    CCSprite *disablesprite=nil;
    switch (selSprite.tag) {
    case 0:
        if (CGRectContainsPoint( [dest[0] boundingBox] , mypoint ) ) {
            NSLog(@"oneeeeee collided");
            dest[0].position =  ccp(125,320);
            // selSprite = disablesprite;
        }
        break;
    case 1:
        if (CGRectContainsPoint( [dest[1] boundingBox] , mypoint1 ) ) {
            NSLog(@"two collided");
            dest[1].position =  ccp(200,312);

        }
        break;
    case 2:
        if (CGRectContainsPoint( [dest[2] boundingBox] , mypoint2 ) ) {
            NSLog(@"three collided");
            dest[2].position =  ccp(117,209);
        }
        break;
    case 3:
        if (CGRectContainsPoint( [dest[3] boundingBox] , mypoint3 ) ) {
            NSLog(@"four collided");
            dest[3].position =  ccp(200,208);

        }
        break;
    case 4:
        if (CGRectContainsPoint( [dest[4] boundingBox] , mypoint4 ) ) {
            NSLog(@"five collided");
            dest[4].position =  ccp(113,127);
        }
        break;
    case 5:
        if (CGRectContainsPoint( [dest[5] boundingBox] , mypoint5 ) ) {
            NSLog(@"six collided");
            dest[5].position =  ccp(207,126);

        }
        break;
    default:
        break;
  }
    }
      }

   } 


    -(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
      CGPoint mypoint = CGPointMake(106, 329);
      CGPoint mypoint1 = CGPointMake(208, 329);
      CGPoint mypoint2 = CGPointMake(97, 220);
       CGPoint mypoint3 = CGPointMake(217, 220);
     CGPoint mypoint4 = CGPointMake(92, 124);
     CGPoint mypoint5 = CGPointMake(212, 124);
     switch (selSprite.tag) {
      case 0:
        if (CGRectContainsPoint( [dest[0] boundingBox] , mypoint ) ) {
            NSLog(@"oneeeeee collided");
            if (corsprite == TRUE) {
                correctplace++;
                corsprite = FALSE;
            }
        }else {
            selSprite.position = ccp(110,50);
        }
        break;
    case 1:
        if (CGRectContainsPoint( [dest[1] boundingBox] , mypoint1 ) ) {
            NSLog(@"two collided");
            if (corsprite2 == TRUE) {
                correctplace++;
                corsprite2 = FALSE;
            }
        }else {
            selSprite.position = ccp(110, 65);
        }
        break;
    case 2:
        if (CGRectContainsPoint( [dest[2] boundingBox] , mypoint2 ) ) {
            NSLog(@"three collided");
            if (corsprite3 == TRUE) {
                correctplace++;
                corsprite3 = FALSE;
            }
        }else {
            selSprite.position =ccp(130, 10);
        }
        break;
    case 3:
        if (CGRectContainsPoint( [dest[3] boundingBox] , mypoint3 ) ) {
            NSLog(@"four collided");
            if (corsprite4 == TRUE) {
                correctplace++;
                corsprite4 = FALSE;
            }
        }else {
            selSprite.position = ccp(215, 10);
        }
        break;
    case 4:
        if (CGRectContainsPoint( [dest[4] boundingBox] , mypoint4 ) ) {
            NSLog(@"five collided");
            if (corsprite5 == TRUE) {
                correctplace++;
                corsprite5 = FALSE;
            }
        }else {
            selSprite.position = ccp(220,70);
        }
        break;
    case 5:
        if (CGRectContainsPoint( [dest[5] boundingBox] , mypoint5 ) ) {
            NSLog(@"six collided");
            if (corsprite6 == TRUE) {
                correctplace++;
                corsprite6 = FALSE;
            }           
        }else {
            selSprite.position = ccp(220,70);
        }
        break;
    default:
        break;
   }

`

share|improve this question
    
You start by saying the image is double the size for iPhone4, then the issue is the touch area is also double the size? Also, you need to work on your accept rate. –  Byte56 Jul 19 '12 at 15:14
    
touching area is double of iphone4 image but touch area is same as iphone3 image. –  Anil gupta Jul 19 '12 at 15:32
    
It may help if you included a small amount of code, just the part where you're creating the images and where you're detecting the touch. –  Byte56 Jul 19 '12 at 15:45
    
Did you even read my answer before adding the code to your question? How/Where do you define your rectangle (I guess that would be invisible[0])? Also this code is a huge mess... you really don't need to do everything twice (once for iPhone 4 and once for older devices). –  bummzack Jul 20 '12 at 6:36
add comment

1 Answer 1

On iOS there are two different units of measurement. Points and Pixels. On older devices a point is equal to a pixel. On new devices, there are two pixels for each point, both vertically and horizontally (so totally 4 pixels per point). An iPhone 3 has 480x320 pixels and 480x320 points. Whereas an iPhone 4 has 960x640 pixels and 480x320 points! The important bit here is that the number of points remain the same for old and new devices.

So I'm assuming you're working with pixel sizes instead of point sizes here. When you define a rectangle to be 20x40 on a new device, then this is in points, not pixels. Which in turn results in a 40x80 pixel rectangle, which is of course double the size of your image. Your image of size 20x40 is actually 10x20 points in size (on an iPhone 4).

I don't know how you retrieve your rectangle sizes. If they are hard-coded, just always use the one for the low-res device. If you're measuring them at runtime, always use methods like contentSize instead of contentSizeInPixels.

Summary: Work with points instead of pixels, as points remain the same across iPhone 1 - 4.

share|improve this answer
    
I am using point as you can see my code, and here i am scaling my images so i have defined twice, one for iphone3 and second for iphone4. i checked my code i have not use pixel. –  Anil gupta Jul 23 '12 at 14:32
    
@Anilgupta I'm pretty sure the issue is because of using incorrect sizes. Without seeing the full source it's impossible to tell though. Did you name your HD assets with the suffix -hd.png? Also why do you have different code/coordinates for iPhone4 (see if (scale > 1.0) in your code)? –  bummzack Jul 23 '12 at 14:39
    
yes i have name the image with suffix -hd.png for iphone4 and with out suffix -hd.png for iphone3, can you give me your skype or any IM id so that i can share my screen with you , i am struggling with this issue, i will be very thankful to you. –  Anil gupta Jul 23 '12 at 15:43
    
you didnt reply me, please help me .. can you tell me, how i can get which device is using like iphone4 or iphone3 any other code to know which device is using so that i can change my code. –  Anil gupta Jul 27 '12 at 13:19
    
@Anilgupta I'm sorry, but I can't help you via IM. You could either take this to the chat or the cocos2d forums. And I say it again: You do not have to detect iPhone 4 or 3. They have the same resolution in points. As long as you use points, there's no need for different coordinates! Throw all your iPhone 4 specific code away and use only the iPhone 3 coordinates. –  bummzack Jul 27 '12 at 16:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.