0

これが機能しないのはなぜですか?

   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 です。

4

2 に答える 2

1

rectの座標の計算をデバッグする必要があると思います。CGRectIntersectsRectは、計算の問題を完璧に機能させるバトルプルーフ関数です。テストのためだけに、次のような値をコードにハードコーディングできます。

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

期待どおりに機能することがわかります。もう一度、rectsメトリック計算コードを確認してください。

于 2011-06-02T20:36:53.620 に答える
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);

ログを投稿します。

于 2011-06-02T20:35:07.510 に答える