0

我正在开发一款通用益智游戏,其中我为 iphone4 和 iphone3 使用了单独的图像,iphone4 图像的大小是 iphone3 图像的两倍。我的问题是 iphone4,iphone4 图像的触摸区域是双倍尺寸,假设我的 iphone4 图像尺寸是 20*40,iphone3 是 10*20,问题是 iphone4 图像的触摸区域是 40*80 i不知道它是如何发生的,我在我的代码中解决了这个问题,但我没有得到这个问题的任何解决方案,在 iphone3 中触摸工作正常,在精灵矩形中使用。

你能帮我解决这个问题还是告诉我我忘记为iphone4触摸编码什么,以便它在精灵(图像)矩形中触摸

预先感谢。这是我的代码:

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

`

4

2 回答 2

0

在您的应用委托中执行此操作

[director enableRetinaDisplay:YES];

并确保您的图像符合惯例。
例如对于 iPhone3 假设你有图像,image.png
那么你的视网膜图像必须是image-hd.png

于 2012-07-19T14:51:27.043 回答
0

好吧,一切都应该是重点,例如,我认为您不需要围绕 UIScreen 缩放属性的逻辑,并且您的 scale=1 逻辑应该在视网膜和非视网膜显示器上相同地工作。

快速尝试:将 scale > 1.0 更改为 scale > 3.0 (if 应该始终分支到 else 部分,您在 iPhone 3 上声明“有效”),只是为了看看这是否正确。如果是,请完全删除围绕比例的逻辑并进行清理:) ...在我的代码中,我没有一个合法的案例来确定比例是 1.0 还是其他。

于 2012-07-20T02:06:14.793 回答