Ios 当我们将nil指向sprite节点时会发生什么?
从上面的代码decorativeBall=nil和golfBall=nil来看,它有助于释放内存还是有意义?Ios 当我们将nil指向sprite节点时会发生什么?,ios,objective-c,sprite-kit,Ios,Objective C,Sprite Kit,从上面的代码decorativeBall=nil和golfBall=nil来看,它有助于释放内存还是有意义? 我想做的是,若有高尔夫球已经被选中,现在若球员已经把它换成装饰球。我想从场景中移除高尔夫球,这是正确的方法吗?我假设DecorativeBall和GolfBall都是SKSpriteNode的子类,并且您正在使用ARC 由于电弧,golfBall=nil确实释放了精灵,但这还不够,至少还有一个对它的引用:首先需要调用[golfBall removeFromParent]将其从层次结构中移
我想做的是,若有高尔夫球已经被选中,现在若球员已经把它换成装饰球。我想从场景中移除高尔夫球,这是正确的方法吗?我假设
DecorativeBall
和GolfBall
都是SKSpriteNode
的子类,并且您正在使用ARC
由于电弧,golfBall=nil
确实释放了精灵,但这还不够,至少还有一个对它的引用:首先需要调用[golfBall removeFromParent]将其从层次结构中移除代码>。当然,decorativeBall
也是如此
@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