CGRectIntersectsRect不起作用

时间:2011-06-02 20:18:46

标签: iphone objective-c xcode cgrect

为什么这不起作用?

   CGRect enemyRect = CGRectMake(enemy.position.x, 
                                  enemy.position.y, 
                                 enemy.contentSize.width, 
                                  enemy.contentSize.height);
   CGRect AppleRect = CGRectMake(Apple.position.x, 
                                 Apple.position.y, 
                                 Apple.contentSize.width, 
                                 Apple.contentSize.height);
   if(CGRectIntersectsRect(enemyRect, AppleRect)) {
       NSLog(@"INTERESTsjkfjkdjkgjkfdjkgjk");
   }

编辑1:

[self setIsTouchEnabled:TRUE];
       Apple = [CCSprite spriteWithFile:@"Apple.jpg"];
       [Apple setPosition:ccp(240,160)];
       [self addChild:Apple];

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
   UITouch* touch = [touches anyObject];
   CGPoint location = [touch locationInView: [touch view]];
   location = [[CCDirector sharedDirector]convertToGL:location];
   [Apple runAction:[CCMoveTo actionWithDuration:0.3 position:location]];
   NSLog(@"Touches ended!");
}

-(void)addenemy {
   CGSize size = [[CCDirector sharedDirector] winSize];
   enemy = [CCSprite spriteWithFile:@"enemy.jpg"];
   enemy.position = ccp(0.0f, size.height + enemy.contentSize.height * 1.0f);
   [self addChild:enemy];
}
- (void)startEnemiesFalling {
   CGSize size = [[CCDirector sharedDirector] winSize];
   enemy.opacity = 255;
   enemy.scale = 1.0f;
   float startingX = RAND_FLOAT * size.width;
   float startingY = size.height + enemy.contentSize.height * 0.5f;
   enemy.position = ccp(startingX, startingY);
   float endingX = RAND_FLOAT * size.width;
   CGPoint endingPosition = ccp(endingX, 0.0f);
   CCMoveTo *moveenemy = [CCMoveTo actionWithDuration:2.0f position:endingPosition];
   CCCallFuncN *moveenemydone = [CCCallFuncN actionWithTarget:self selector:@selector(enemymovedone:)];
   CCSequence *sequence = [CCSequence actions:moveenemy, moveenemydone, nil];
   [enemy runAction:sequence];
}

编辑2:

CGRect enemyRect = CGRectMake(0, 
                                      0, 
                                     25, 
                                      25);
       CGRect AppleRect = CGRectMake(0, 
                                     0, 
                                     25, 
                                     25);
       if(CGRectIntersectsRect(enemyRect, AppleRect)) {
           UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"You Died" message:@"YOU DIED MICROSOFT KILLED YOU." delegate:self cancelButtonTitle:@"" otherButtonTitles:nil] autorelease];
           [alert show];
       }

图像为25pxX25px。

2 个答案:

答案 0 :(得分:1)

我假设您需要调试rects的坐标计算。 CGRectIntersectsRect是防争功能,可以完美地解决计算中的问题。只是为了测试,您可以在代码中硬编码,例如:

    CGRect enemyRect = CGRectMake(0, 
                              0, 
                              100, 
                              100);
CGRect AppleRect = CGRectMake(10, 
                              10, 
                              80, 
                              80);
if(CGRectIntersectsRect(enemyRect, AppleRect)) {
    NSLog(@"INTERESTsjkfjkdjkgjkfdjkgjk");
}

您将看到它按预期工作。请再次查看指标计算代码。

答案 1 :(得分:0)

在检查它们是否相交的函数中,尝试添加:

NSLog(@"enemy rect: (%d,%d - %d,%d) - apple rect: (%d,%d - %d,%d)",enemy.position.x, enemy.position.y, enemy.contentSize.width, enemy.contentSize.height, Apple.position.x, Apple.position.y, Apple.contentSize.width, Apple.contentSize.height);

并发布日志。

相关问题