2014-11-11 4 views
0
@interface Player() 
@property (nonatomic) SKTextureAtlas *atlas; 
@end 
@implementation Player{ 
    DecorativeBall *decorativeBall; 
    GolfBall *golfBall; 
} 
- (id) initWithBallName :(NSString *) name{ 
    if(self = [super init]){ 
     _atlas = [SKTextureAtlas atlasNamed:@"Balls"]; 
     [self removeAllChildren]; 
     if ([name isEqualToString:@"DecorativeBall"]) // if it is a decorative ball{ 
      if (debug){ 
       printf("\n\n It's a Decorative ball"); 
      } 
      if (!decorativeBall){ 
       decorativeBall = [[DecorativeBall alloc] initWithTexture:[_atlas textureNamed:@"DecorativeBall"]]; 
       golfBall = nil; 
      } 
      [self addChild:decorativeBall]; 
     } 
     if ([name isEqualToString:@"GolfBall"]) // if it is a golf ball{ 
      if (debug){ 
       printf("\n\n It's a Golf ball"); 
      } 
      if (!golfBall){ 
       golfBall = [[GolfBall alloc] initWithTexture:[_atlas textureNamed:@"GolfBall"]]; 
       decorativeBall = nil; 
      } 
      [self addChild:golfBall]; 
     } 
    } 
    return self; 
} 
@end 

위의 코드 decorativeBall = nil과 golfBall = nil에서 메모리를 확보하는 데 도움이되는지, 아니면 어떤 의미를 가지나요? 내가하려는 것은, 골프 공이 이미 선택되어 있고, 플레이어가 장식 공으로 바꾸었다면, 그렇게하는 것입니다. 현장에서 골프 공을 제거하고 싶습니다. 올바른 방법입니까?스프 라이트 노드에 nil을 지정하면 어떻게됩니까?

답변

1

나는 DecorativeBallGolfBall이 모두 서브 클래스가 SKSpriteNode이고 ARC를 사용한다고 가정합니다.

ARC로 인해 golfBall = nil;은 스프라이트를 해제하지만 충분하지 않습니다. 아직 다른 참조가 하나 이상 있습니다. 먼저 [golfBall removeFromParent];을 호출하여 계층 구조에서 제거해야합니다. 물론 decorativeBall에 대해서도 마찬가지입니다.

관련 문제