2012-07-17 3 views
2

벽에 충돌했는지 확인할 수 있도록 Sprites를 추가하려는 NSMutable 배열이 있습니다. 나는 그렇게 할이 코드를 사용합니다. 'HELP'나는 구축하고 실행할 때nsmutablearray가 호출 될 때 객체를 추가하지 않음

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; 
    } 
} 

} 

그러나, 나는 '(널)'과의 콘솔 출력을 얻을 'updator'이전의 메소드는 touchesEnded에서 호출됩니다. 누군가 내가 잘못하고있는 것을 볼 수 있습니까? 감사합니다.

답변

1

글 머리 기호를 다른 배열에 추가하려는 이유가 무엇입니까? _layer.children 인 모든 배치 노드를 이미 가지고 있습니다.

배열 자체 (bulletsArray)가 0이 아닌 것은 확실합니까? 어디에서 초기화됩니까?

마지막으로 더 많은 성능을 제공하는 CCARRAY_FOREACH을 사용하여 루핑을 고려해야합니다.

3

배열을 초기화 하시겠습니까? nil 객체를 보유 할 수

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    bulletsArray = [NSMutableArray alloc] init]; 
} 
2

NSMutableArray 때문에 그게 유일한 방법에게 ...

이있는 viewDidLoad 방법이 시도 가능성이 가장 높은 이유처럼

[bulletsArray objectAtIndex:0] == nil 

가 평가할 수있는 조건을 보일 수있을 것입니다 truebulletsArraynil입니다. 배열이 제대로 할당되었는지 확인해야합니다. 이를 수행하는 전형적인 장소는 클래스의 지정된 초기화 프로그램입니다.

+0

** facepalm ** 감사 : / – Jeeter

관련 문제