【发布时间】:2013-12-10 14:06:25
【问题描述】:
我无法检测到两个精灵之间的碰撞,其中一个正在移动。
我有一个“Enemy”类精灵,通过 CCMoveTo 在屏幕上移动,另一个“Hero”类精灵,由触摸控制,都添加到“MainGame”类的场景中
以下说明如何将两个精灵添加到场景中以及如何操作敌人:
MainGame.m
-(id) init{
if( (self=[super init]) ) {
_enemies = [[NSMutableArray alloc]init];
_enemyLink = [Enemy nodeWithTheGame:self];
_enemyLink.position = ccp(10, 10);
[self.enemies addObject:_enemyLink];
CCMoveTo *test = [CCMoveTo actionWithDuration:40 position:ccp(500, 250)];
[_enemyLink runAction:test];
_heroArray = [[NSMutableArray alloc]init];
_heroLink = [Hero nodeWithTheGame:self location:ccp(100,100)];
[_heroArray addObject:_heroLink];
[self scheduleUpdate];
}
}
-(void)update:(ccTime)delta{
if (CGRectIntersectsRect([self.enemyLink.enemySprite boundingBox], [self.heroLink.heroSprite boundingBox])) {
NSLog(@"rect intersects rect");
}
for (CCSprite *enemies in self.enemies) {
NSLog(@"enemy position: %f, %f",enemies.position.x,enemies.position.y);
}
}
我无法在动作期间和之后检测到这两个精灵之间的碰撞。但是,如果我将英雄移动到场景中的位置 (0,0),我的代码中的日志将触发,并且两者将进行攻击。
更新中的 for 循环表明 Enemy 精灵位置在动作期间不断移动。因此,为什么我不知道为什么没有检测到碰撞。
【问题讨论】:
标签: ios cocos2d-iphone