如果我想在另一个类中分配一个类并且我想轻松引用它,但有时这个类不需要被分配,因此不需要被释放。这是怎么做到的?我可以在 dealloc 中放置一个条件,所以它不必被释放吗?
更详细地说,我使用的是 Cocos2D。我有可能需要也可能不需要分配的玩家能力等级。在我的初始化中:
// 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 需要保留引用,以便它可以随地图滚动。但是如果我不分配它,我怎么不解除分配?