现在我有精灵在屏幕上移动,当你触摸一个你正在“射击”它时。为了看看是否有任何“敌人”被“射击”,我正在使用CGRectContainPoint
,它可以正常工作,直到我希望它能够用更多的一击来移除敌人。这是一步:
- (void)update:(ccTime)dt {
_shot = bullet.position;
BOOL enemyHit = FALSE;
NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
for (CCSprite *target in _targets) {
CGRect targetRect = target.boundingBox;
if (CGRectContainsPoint(targetRect, _shot)) {
enemyHit = TRUE;
Enemy *enemy = (Enemy *)target;
enemy.hp--;
if (enemy.hp <= 0) {
[targetsToDelete addObject:target];
}
break;
}
}
for (CCSprite *target in targetsToDelete) {
[_targets removeObject:target];
[self removeChild:target cleanup:YES];
}
[targetsToDelete release];
}
我遇到的问题是它更新比重置触摸位置更快。因此,在一次触摸时,它将在3开始后将敌人的健康状况带到-2。如何解决这个问题?
编辑:我真的没有办法设置触摸所在的CGPoint,然后在更新一次迭代后将其重新关闭。
答案 0 :(得分:1)
每次用户启动或完成单次触摸时,而不是在更新循环中连续进行触摸交叉测试是有意义的,因为您希望每次触摸后任何触摸的精灵的生命周期减少一个。 / p>
您可以通过实施ccTouchesBegan或ccTouchesEnded(选择取决于您想要的点击行为)来实现:
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
_shot = [touch locationInView:[touch view]];
_shot = [[CCDirector sharedDirector] convertToGL:_shot];
// move your entire sprite intersection logic here
}