我有一个 NSMutable 数组,我想将 Sprites 添加到其中,这样我就可以检查它们是否撞到了墙上。我使用此代码这样做:
NSString *bulletName = [NSString stringWithFormat:@"tank%d_bullet.png", _type];
bullet = [CCSprite spriteWithSpriteFrameName:bulletName];
bullet.tag = _type;
bullet.position = ccpAdd(self.position, ccpMult(_shootVector, _turret.contentSize.height));
CCMoveBy * move = [CCMoveBy actionWithDuration:duration position:actualVector];
CCCallBlockN * call = [CCCallBlockN actionWithBlock:^(CCNode *node) {
[node removeFromParentAndCleanup:YES];
}];
if (!bulletIsGone) {
[self schedule:@selector(updator:) interval:0.01];
}
else {
[self unschedule:@selector(updator:)];
}
[bullet runAction:[CCSequence actions:move, call, nil]];
[_layer.batchNode addChild:bullet];
[bulletsArray addObject:bullet];
if ([bulletsArray objectAtIndex:0] == nil) {
NSLog(@"HELP");
}
NSLog(@"%@", [bulletsArray objectAtIndex:0]);
}
-(void)updator: (ccTime) dt{
for(CCSprite *bulletz in bulletsArray){
NSLog(@"this is the for loop");
CGRect rect1 = CGRectMake(bulletz.position.x - bulletz.contentSize.width/2, bulletz.position.y - bulletz.contentSize.height/2, 20, 20);
if ([_layer isWallAtRect:rect1]) {
NSLog(@"bulletHitWall");
[_layer.batchNode removeChild:bulletz cleanup:NO];
bulletIsGone = YES;
}
}
}
但是,当我构建和运行时,我得到了“(空)”和“帮助”的控制台输出。从 touchesEnded 调用 'updator' 之前的方法。有人可以看到我做错了什么吗?谢谢!