为什么这不起作用?
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。