@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 来看,它有助于释放内存还是有意义? 我想做的是,如果已经选择了高尔夫球,现在如果玩家将其切换为装饰球。我想从现场移除高尔夫球,这是正确的方法吗?
最佳答案
我假设 DecorativeBall
和 GolfBall
都是 SKSpriteNode
的子类并且您正在使用 ARC。
由于 ARC,golfBall = nil;
确实释放了 sprite 但这还不够,至少还有一个对它的引用:您首先需要通过调用 从层次结构中删除它>[golfBall removeFromParent];
。当然,decorativeBall
也是如此。
关于ios - 当我们将 nil 指向 sprite 节点时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26860671/