我正在构建一款针对新iOS 7和Sprite Kit的iOS游戏,使用发射器节点和物理来增强游戏性.在开发应用程序时,我遇到了一个严重的问题:您创建了场景,节点和效果,但是当您完成并需要返回主屏幕时,如何释放这些资源分配的所有内存?
理想情况下,ARC应该释放所有内容,应用程序应该恢复到创建场景之前的内存消耗级别,但这不是发生的事情.
我添加了以下代码,作为视图的dealloc方法,它绘制场景并负责在关闭(删除)后删除所有内容:
- (void) dealloc
{
if (scene != nil)
{
[scene setPaused:YES];
[scene removeAllActions];
[scene removeAllChildren];
scene = nil;
[((SKView *)sceneView) presentScene:nil];
sceneView = nil;
}
}
Run Code Online (Sandbox Code Playgroud)
我非常感谢你对此事的任何帮助.
我的问题非常简单,根据苹果文档,您可以在呈现如下场景之前将纹理预加载到RAM中:
SKTextureAtlas * atlas = [SKTextureAtlas atlasNamed:@"effect_circle_explode"];
SKTextureAtlas * atlas2 = [SKTextureAtlas atlasNamed:@"box_explodes"];
SKTextureAtlas * atlas3 = [SKTextureAtlas atlasNamed:@"fence_new"];
SKTextureAtlas * atlas4 = [SKTextureAtlas atlasNamed:@"swipe"];
SKTextureAtlas * atlas5 = [SKTextureAtlas atlasNamed:@"coin"];
SKTextureAtlas * atlas6 = [SKTextureAtlas atlasNamed:@"two_times"];
SKTextureAtlas * atlas7 = [SKTextureAtlas atlasNamed:@"three_times"];
SKTextureAtlas * atlas8 = [SKTextureAtlas atlasNamed:@"gus"];
[SKTextureAtlas preloadTextureAtlases:@[atlas, atlas2, atlas3, atlas4, atlas5, atlas6, atlas7, atlas8] withCompletionHandler:^{
[moron_logo removeFromSuperview];
moron_logo = NULL;
stuff.hidden = NO;
store.hidden = NO;
scroll_view.userInteractionEnabled = YES;
[self present_game_view];
}];
Run Code Online (Sandbox Code Playgroud)
现在如果以后通过游戏玩法你会对同样的地图集进行预加载会产生任何负面影响:
-(void)load
{
SKTextureAtlas …Run Code Online (Sandbox Code Playgroud) 如何从SKSpriteNode?访问图像文件名?我一直试图谷歌这个,但似乎没有答案可用.这是我的代码中无用的部分:
current_ingredient?.name = String(describing: current_ingredient?.texture)
print("Filename: \(current_ingredient?.name)")
Run Code Online (Sandbox Code Playgroud)
print命令返回:
Filename: Optional("Optional(<SKTexture> \'ingredient14\' (110 x 148))")
Run Code Online (Sandbox Code Playgroud)
所以,问题是,我怎么才得到"ingredient14"?
这让我想到了.
游戏开始,我用-spriteNodeWithImageNamed:方法创建精灵.稍后为动画,我创建了一个SKTextureAtlas对象.有些人说它更快,因为-spriteNodeWithImageNamed:首先会在你的应用程序包中查找png,然后它会查看地图集.
但是我不清楚的是:如果我稍后创建一个SKTextureAtlas,它会知道已经加载的地图集图像还是会变得笨拙而只是再次加载图像?
如果我在同一地图集的多个节点中创建SKTextureAtlas对象.它会多次加载地图集吗?我必须确保我只为任何地图集使用一个SKTextureAtlas实例吗?