2014-12-21 3 views
1

초보자로서 도움이 필요합니다. 저는 4 개의 사각형 (sprite1)과 1 개의 카운터 (counterLabel, 제거 된 노드를 계산합니다)라는 다른 노드가 있습니다. 나는 그들을 만져서 4 개의 정사각형을 제거하고 싶다. 아래 코드를 사용하면 사각형을 제거 할 수 있으며 카운터도 제거 할 수 있습니다. 이상하게도 사각형 노드 (sprite1)를 독점적으로 처리하려고했기 때문입니다. 사각형 노드 (스프라이트 1)를 독점적으로 제거 할 수 있습니까?SpriteKit에서 특정 노드 제거

@implementation GameScene { 
    BOOL updateLabel; 
    SKLabelNode *counterLabel; 
} 

int x; 
int y; 
int counter; 

-(id)initWithSize:(CGSize)size {  
    if (self = [super initWithSize:size]){ 

    self.backgroundColor = [SKColor /*colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0*/ whiteColor]; 

    counter = 0; 

    updateLabel = false; 

    counterLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"]; 
    counterLabel.name = @"myCounterLabel"; 
    counterLabel.text = @"0"; 
    counterLabel.fontSize = 48; 
    counterLabel.fontColor = [SKColor greenColor]; 
    //counterLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter; 
    //counterLabel.verticalAlignmentMode = SKLabelVerticalAlignmentModeBottom; 
    counterLabel.position = CGPointMake(50,50); // change x,y to location you want 
    //counterLabel.zPosition = 900; 
    [self addChild: counterLabel]; 
    } 
    return self; 
} 


-(void) didMoveToView:(SKView *)view { 

    SKTexture *texture1 = [SKTexture textureWithImageNamed:@"square"]; 

    for (int i = 0; i < 4; i++) { 
    x = arc4random()%668; 
    y = arc4random()%924; 
    SKSpriteNode *sprite1 = [SKSpriteNode spriteNodeWithTexture:texture1]; 
    sprite1.position = CGPointMake(x, y); 
    sprite1.name = @"square"; 

    [self addChild:sprite1]; 
    } 
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 

    NSArray *nodes = [self nodesAtPoint: [touch locationInNode: self]]; 

    for (SKNode *sprite1 in nodes) { 

    [sprite1 removeFromParent]; 

    counter ++; 
    updateLabel = true; 
    } 
} 

-(void)update:(CFTimeInterval)currentTime { 

    if(updateLabel == true){ 
    counterLabel.text = [NSString stringWithFormat:@"%i",counter]; 
    updateLabel = false; 
    } 
} 

@end 

답변

1

당신은 당신이 할 수있는,이 경우

SKSpriteNode

의 재산 name을 사용해야합니다
for (SKNode *sprite1 in nodes) { 

    if(![sprite1.name isEqualToString:@"myCounterLabel"]) { 

    [sprite1 removeFromParent]; 

    } 

    counter ++; 
    updateLabel = true; 
} 

그래서 경우 SKNode 이름은 다음 counterLabel의 이름, removeFromParent 다릅니다.

+1

좋아요! 그게 다야! 감사! – George

+0

@ 조지 환영합니다! ;) – Ilario