ios - SKTextureAtlas:不止一个图集令人困惑

标签 ios objective-c sprite-kit sktextureatlas

我在使用多个 map 集时遇到问题。

例如,我有 main_menu.atlas 和 game.atlas 以及这些场景的图像。在主菜单场景出现之前,我为它准备了图集([SKTextureAtlas atlasNamed:@"main_menu"])并且一切正常。但是当我开始游戏并在游戏中准备游戏图集( [SKTextureAtlas atlasNamed:@"game"] )后,我只看到空节点(带红色交叉的矩形)。没有异常(exception)或警告 - 一切正常。

当我将所有游戏资源移至 main_menu.atlas 并删除 game.atlas 时,一切正常 - 我在游戏中看到 Sprite 。但我想分离图集以进行性能优化。

我使用自己的助手进行 SpriteKit 纹理管理。它加载 map 集并返回我需要的纹理。所以我有这些方法:


- (void) loadTexturesWithName:(NSString*)name {
    name = [[self currentDeviceString] stringByAppendingString:[NSString stringWithFormat:@"_%@", name]];
    SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:name];
    [self.dictAtlases setObject:atlas forKey:name];
}

- (SKTexture *) textureWithName:(NSString*)string {
    string = [string stringByAppendingString:@".png"];

    SKTexture *texture;
    SKTextureAtlas *atlas;
    for (NSString *key in self.dictAtlases) {
        atlas = [self.dictAtlases objectForKey:key];
        texture = [atlas textureNamed:string];
        if(texture) {
            return texture;
        }
    }
    return nil; // never returns "nil" in my cases
}

“清洁”无济于事。 我做错了什么? 提前致谢。

最佳答案

首先让我声明:你绝对可以使用 2+ 纹理图集:)

现在手头的问题:

您首先加载菜单图集(首先是字典),然后是游戏图集。当您抓取菜单纹理时,一切都很好。 当您外出获取游戏纹理时,您首先查看菜单图集(没有可用图像,因此图集返回此 doc 中定义的占位符纹理,而不是您期望的 nil。

这段代码应该可以正常工作

- (SKTexture *) textureWithName:(NSString*)string {
    string = [string stringByAppendingString:@".png"];

    SKTexture *texture;
    SKTextureAtlas *atlas;
    for (NSString *key in self.dictAtlases) {
        atlas = [self.dictAtlases objectForKey:key];
        if([[atlas textureNames] containsObject:string]){
            texture = [atlas textureNamed:string];
            return texture;
        }
    }
    return nil;
}

此外,不添加 .png 也能正常工作 :)

关于ios - SKTextureAtlas:不止一个图集令人困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21260571/

相关文章:

ios - SIGABRT 在 iOS 5 模拟器中不显示堆栈跟踪

ios - 未找到 PhoneGap 应用程序 : "ERROR: Start Page at ` www/index. html`”

objective-c - 处理ARC中的指针对指针所有权问题

ios - Cocos2d -> Swift SpriteKit - CCLabelAtlas charmap 功能

ios - 具有关联类型的 Functor 协议(protocol)的实现(Swift)

ios - 通过使用查询而不是重复观察单个事件来加快为我的社交网络应用获取帖子

iPhone MKMapView : Detecting Nearest Locations in Array from Current Location

ios - 从其他类调用 Controller 函数

swift - 在 SpriteKit (swift) 中生成多次相同节点的最佳实践

ios - Sprite Kit SKAction 组暂停应用程序