我正在使用 ARC 和 Cocos2d 2.0 作为静态库(它不使用 ARC 并且被编译为单独的目标)。我翻译了一个旧项目(没有 ARC),我想知道以这种方式声明属性是否有一些潜在的保留周期问题:
@interface PlayerData : NSObject <NSCoding> {
}
//Is ok to save this as if the game gets paused you may want to save this.
@property (readwrite, nonatomic) int numberOfHits;
@property (readwrite, nonatomic) bool everBeenHitInCurrentLevel;
@property (readwrite, nonatomic) int hitsForOneHearth;
我注意到我的各种场景会随着时间的推移建立起记忆。此外,我在 CCLayer 方法(MyScene : CCLayer)的 release 方法中添加了一个 CCLOG 调用,它永远不会被调用。这就是我创建场景的方式(我使用“ [CCDirector sharedDirector] replaceScene ”方法替换它
+ (id) sceneWithLevelName:(LevelName)name
{
CCScene *scene = [CCScene node];
ShooterScene * shooterLayer = [[self alloc] initWithId:name];
[scene addChild:shooterLayer];
return scene;
}
编辑:当我意识到我愚蠢地没有包含一个带有对象的示例并且只使用原始数据类型时,我将在我的场景中粘贴一些元素片段:CharacterSelection、ShooterScene 和 PlanetSelectionMenu:
//ShooterScene
@interface ShooterScene : CCLayer {
HudLayer * hudLayer;
....
}
@property(readwrite, nonatomic) CCSpriteBatchNode* backgroundAndEnemiesBatchNode;
@property(readwrite, nonatomic) ShipEntity* playerShip;
... etc..
Please note that I do not declare member variables for properties like playerShip and backgroundAndEnemiesBatchNode beause, as far as I can understand, should suffice the property declaration (but please correct me if I am wrong or if the approach may cause issues).
//CharacterSelectionScene
@interface CharacterSelectionScene : CCLayer {
int currentlySelectedCharacterSpriteTag;
CCSprite * lights;
CCLayer * spritesLayer;
...
}
//PlanetSelectionMenu
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface PlanetSelectionMenu : CCLayer {
CCLayer * backgroundLayer; // Added background images here
CCLayer * alwaysPresentItems;
}
+ (id) scene;
@end
请注意,每次我从PlanetSelectionMenu转到CharacterSelectionScene - 反之亦然 - 记忆都会增加。但是在这种情况下,我没有使用任何属性,而是“只是”将对象(CCSprites)添加到图层和批处理节点。
编辑 2:这是我在通过 Menu->CharacterSelection->PlanetSelectionScene 等运行时在分配中看到的内容。似乎 LiveBytes 平均为 4MB,因为当时我只看到一个场景,所以我假设没有保留周期。那为什么我会收到那些讨厌的 LOW 内存信息呢?