有条件分配/释放? [Objective-c 和 Cocos2D]

发布于 2024-11-17 19:05:34 字数 1079 浏览 4 评论 0原文

如果我想在另一个类中分配一个类并且我想轻松地引用它,但有时不需要分配该类,因此不需要释放该类,该怎么办?这是怎么做到的?我可以在 dealloc 中放置一个条件,这样就不必释放它吗?

更详细地说,我正在使用 Cocos2D。我有可能需要也可能不需要分配的玩家能力类别。在我的 init 中:

    // Abilities
    if(abilityRushH == 0){
        objects = [theMap objectGroupNamed:@"oj"];
        startPoint = [objects objectNamed:@"item_ability_rushH"];
        x = [[startPoint valueForKey:@"x"] intValue];
        y = [[startPoint valueForKey:@"y"] intValue];

        rushH = [[RushHorizontal alloc] init];

        [self addChild:rushH.rushHSpriteSheet];

        rushH.rushHSprite.position = ccp(x,y);              
    }

    if(abilityRushV == 0){
        objects = [theMap objectGroupNamed:@"oj"];
        startPoint = [objects objectNamed:@"item_ability_rushV"];
        x = [[startPoint valueForKey:@"x"] intValue];
        y = [[startPoint valueForKey:@"y"] intValue];

        rushV = [[RushVertical alloc] init];

        [self addChild:rushV.rushVSpriteSheet];

        rushV.rushVSprite.position = ccp(x,y);  
    }

Cocos2D 需要保留引用,以便它可以随地图滚动。但如果我不分配它,我如何才能不释放呢?

What if I want to alloc a class inside another and I want to reference it easily, but sometimes this class would not need to be alloc'd, therefore not dealloc'd. How is this done? Can I put a conditional inside dealloc so it doesn't have to be released?

In more detail, I'm using Cocos2D. I have player ability classes that may or may not need to be allocated. In my init:

    // Abilities
    if(abilityRushH == 0){
        objects = [theMap objectGroupNamed:@"oj"];
        startPoint = [objects objectNamed:@"item_ability_rushH"];
        x = [[startPoint valueForKey:@"x"] intValue];
        y = [[startPoint valueForKey:@"y"] intValue];

        rushH = [[RushHorizontal alloc] init];

        [self addChild:rushH.rushHSpriteSheet];

        rushH.rushHSprite.position = ccp(x,y);              
    }

    if(abilityRushV == 0){
        objects = [theMap objectGroupNamed:@"oj"];
        startPoint = [objects objectNamed:@"item_ability_rushV"];
        x = [[startPoint valueForKey:@"x"] intValue];
        y = [[startPoint valueForKey:@"y"] intValue];

        rushV = [[RushVertical alloc] init];

        [self addChild:rushV.rushVSpriteSheet];

        rushV.rushVSprite.position = ccp(x,y);  
    }

Cocos2D needs to keep the reference so it can scroll with the map. But if I'm not alloc'ing it, how do I NOT dealloc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咿呀咿呀哟 2024-11-24 19:05:34

由于您正在谈论在dealloc中释放它,因此将有一个实例变量。现在,当分配 Objective-C 类的任何实例时,其所有对象都为 nil,并且 c 类型被设置为 0(或等效值)。因此,如果您的类的对象未实例化,则无需付出任何额外的努力,因为实例变量在 dealloc 处将为 nil ,因此 release发送给它的 消息将不起作用。

Since you are talking of releasing it in dealloc, there will be an instance variable for this. Now when any instance of an Objective-C class is allocated, all its objects are nil and c types are set to 0 (or equivalent values). So you don't need to put any extra effort if the object of your class isn't instantiated as the instance variable will be nil at dealloc and so release message sent to it will have no effect.

孤君无依 2024-11-24 19:05:34

确保可选变量在不需要时为 nil,并在释放之前进行 nil 检查。

Make sure the optional variable is nil when its not needed, and do a nil check before dealloc'ing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文