2014-04-01 3 views
2

나는 무작위적인 장애물이 화면의 상단에서 떨어지면서 플레이어가 그것을 피할 수있는 게임을 만들고 있습니다. 화면 아래에있는 노드 (장애물)를 삭제하는 방법을 구현하는 데 문제가 있습니다. 장애물을 산란하는 방식이 단순 해 보일 수도 있지만 혼란 스럽습니다.스프라이트 키트의 화면 아래에있는 노드를 삭제하는 방법은 무엇입니까?

- (void)spawnNewObstacles 
{ 
    //spawn obstacles 
    SKSpriteNode *obstacle = [SKSpriteNode spriteNodeWithImageNamed:@"black.png"]; 

    int position = arc4random() % 320 + 1; 
    obstacle.position = CGPointMake(position, 800); 
    obstacle.size = CGSizeMake(200, 20); 

    obstacle.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:obstacle.frame.size]; 
    obstacle.physicsBody.dynamic = YES; 

    [self addChild:obstacle]; 
    [obstacles addObject:obstacle]; //obstacles is an NSMutableArray 

    [obstacle.physicsBody applyImpulse:CGVectorMake(0, -80.0f)]; 

    [self performSelector:@selector(spawnNewObstacles) withObject:nil afterDelay:0.25]; 
} 

그리고 여기가 화면 아래로 떨어지는 것과 삭제하는 (그러나 실패)하고있어 방법은 다음과 같습니다 :

-(void)update:(CFTimeInterval)currentTime 
{ 
    for(int i=0;i<obstacles.count;i++) 
    { 
     NSLog(@"Removed an Obstacle"); //this log isn't showing so i concluded that it wasn't working 
     SKSpriteNode *obstacle = [obstacles objectAtIndex:i]; 
     if(obstacle.position.y<0) 
     { 
      [obstacle removeFromParent]; 
     } 
    } 
} 

방법 U 사람이 난을 제안 여기에 내가 장애를 생성하는 방법이다 이 작업을 수행?

p.s. 중단 점을 설정

obstacles = [NSMutableArray array]; 

obstacles 있는지 확인 : PLZ ... 코드로의 소리에서

답변

1

을 설명, 난 당신과 같은 라인으로 obstacles 배열을 초기화하지 않은 같은데요 변수가 nil이 아닙니다.

그 외의 코드는 괜찮아 보입니다. 당신이 빠른 열거를 사용하는 것을 선호한다 제외 - 그것은 안전 (발생하지 않을 수 범위를 벗어 지수)의, 빠르고 쉽게 읽을 :이있는 경우

for (SKSpriteNode* obstacle in obstacles) 
{ 
    NSLog(@"Removed an Obstacle"); 
    if (obstacle.position.y < 0) 
    { 
     [obstacle removeFromParent]; 
    } 
} 
나는 또한 부모에서 노드를 제거하고 궁금
+0

이 충분하다 또는 메모리를 절약하는 더 좋은 방법 – vink007

+0

그만이면 충분합니다. – LearnCocos2D

관련 문제