@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
从上面的代码decorBall = nil和golfBall = nil,它是否有助于释放内存或有任何意义? 我想要做的是,如果已经选择了高尔夫球,现在如果球员已将其切换为装饰球。我想从场景中移除高尔夫球,这是一种正确的方法吗?
答案 0 :(得分:1)
我假设DecorativeBall
和GolfBall
都是SKSpriteNode
的子类,并且您正在使用ARC。
由于ARC,golfBall = nil;
确实会释放精灵,但这还不够,至少还有一个其他参考:首先需要通过调用[golfBall removeFromParent];
将其从层次结构中删除。当然,decorativeBall
也是如此。