【发布时间】:2013-11-11 16:36:53
【问题描述】:
我目前正在 iOS 上使用 Sprite Kit 来确定它是否适合制作相对简单的 2D 游戏。
由于我的 ActionScript 背景,我很乐意在代码方面使用 Sprite Kit
但有些事情我就是想不通。以纹理图集作为资源的动画节点非常耗费内存。我已经将地图集导入到我的项目中(纹理大小约为 35MB)。将纹理预加载到 RAM 中似乎没问题,但在我运行实际动画的那一刻,堆大小呈指数增长(从大约 80MB 到 780MB)
这是我的代码:
self.noahFrames = [[NSMutableArray alloc] init];
SKTextureAtlas *noahAtlas = [SKTextureAtlas atlasNamed:@"noahAnimati"];
int imgCount = noahAtlas.textureNames.count;
for (int i=1; i <= imgCount; i++) {
NSString *textureName = [NSString stringWithFormat:@"NoahMainMenuAnimation_%d", i];
SKTexture *temp = [noahAtlas textureNamed:textureName];
[self.noahFrames addObject:temp];
}
SKSpriteNode *noahNode = [self createSpriteWithName:@"noah" imagePath:@"Noah_main_menu_hd" positionXPath:@"MainMenu.Noah.x" positionYPath:@"MainMenu.Noah.y" scalePath:@"MainMenu.Noah.scale"];
[self addChild:noahNode];
//up to this point everything goes fine
[noahNode runAction:[SKAction repeatActionForever:
[SKAction animateWithTextures:self.noahFrames
timePerFrame:0.1f
resize:YES
restore:YES]] withKey:@"animatedNoah"];
所以我想我的实际问题是为什么应用程序在调用 SKAction 动画后会变得如此严重的内存?我一定遗漏了一些相当明显的东西......
【问题讨论】:
-
那么当您单步越过 runAction 行时,内存使用量会增加吗?还是您可能在每一帧一遍又一遍地运行此操作,可能针对多个节点?
-
没错。不,我不会一遍又一遍地调用这个动作。它只调用一次且用于一个节点。
-
我在 iPad 2 上遇到了同样的问题。我今天在应用商店发布的基于 spritekit 的应用程序在显示带有 spritekit 视图的视图控制器时立即崩溃。这些 spritekit 视图没有动画,只有简单的单色 spritenodes(甚至没有任何图像,只是简单的单色节点)。我在同一页面上确实有 7 个 spritekit 视图,但 iPad 2 无法处理。
-
另一种减少纹理内存的方法可以在这个答案stackoverflow.com/a/38679128/763355中找到
标签: ios memory-management sprite-kit