ios - 当我们将 nil 指向 sprite 节点时会发生什么?

标签 ios objective-c sprite-kit

@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 来看,它有助于释放内存还是有意义? 我想做的是,如果已经选择了高尔夫球,现在如果玩家将其切换为装饰球。我想从现场移除高尔夫球,这是正确的方法吗?

最佳答案

我假设 DecorativeBallGolfBall 都是 SKSpriteNode 的子类并且您正在使用 ARC。

由于 ARC,golfBall = nil; 确实释放了 sprite 但这还不够,至少还有一个对它的引用:您首先需要通过调用 从层次结构中删除它>[golfBall removeFromParent];。当然,decorativeBall 也是如此。

关于ios - 当我们将 nil 指向 sprite 节点时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26860671/

相关文章:

ios - 收到推送通知时推送 View Controller iOS

c++ - 在 iOS 上使用 C++ 代码,创建静态库或与 Objective C 混合使用?

swift - 动态地将组件添加到实体

ios - 按下完成按钮后使 AVPlayerViewController 继续播放

sprite-kit - 大量使用 SKActions 是否存在明显的性能问题?

ios - Spritekit 上双重生成(和重叠)敌人

ios - leftbarbuttonitem脱离框架

ios - 续订 Push 证书并保持当前 App Store 应用正常运行

objective-c - 两次方法调用之间的时间间隔

ios - 在 iOS 9 中自定义 ContactPicker UI