纹理图集中的纹理未加载或出现

时间:2015-10-09 04:30:53

标签: ios objective-c sprite-kit sktexture texture-atlas

由于一些奇怪的原因,纹理图集中的纹理没有加载。我不知道为什么。

以下是我通常声明/编码纹理的方法

     -(SKSpriteNode *)background {
SKSpriteNode *background;
NSArray *backgroundIpad;

    background = [SKSpriteNode spriteNodeWithImageNamed:@"dodgR - main background"];
    background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
    background.size = CGSizeMake(1136, 640);

    NSArray *backgroundIphone = @[[SKTexture textureWithImageNamed:@"dodgR - animation 1.jpg"],
                                  [SKTexture textureWithImageNamed:@"dodgR - animation 2.jpg"],
                                  [SKTexture textureWithImageNamed:@"dodgR - animation 3.jpg"],
                                  [SKTexture textureWithImageNamed:@"dodgR - animation 4.jpg"],
                                  [SKTexture textureWithImageNamed:@"dodgR - animation 5.jpg"],
                                  [SKTexture textureWithImageNamed:@"dodgR - animation 6.jpg"],
                                  [SKTexture textureWithImageNamed:@"dodgR - animation 7.jpg"],
                                  [SKTexture textureWithImageNamed:@"dodgR - animation 8.jpg"],
                                  [SKTexture textureWithImageNamed:@"dodgR - animation 9.jpg"],
                                  [SKTexture textureWithImageNamed:@"dodgR - animation 10.jpg"]];

    SKAction *backgroundIphoneAnimation = [SKAction animateWithTextures:backgroundIphone timePerFrame:0.05];

    SKAction *backgroundIphoneRepeat = [SKAction repeatActionForever:backgroundIphoneAnimation];

    [background runAction:backgroundIphoneRepeat];






background.name = @"background";
return background;

}

我的纹理图集的名称是sprites.atlas 任何帮助将不胜感激,谢谢

1 个答案:

答案 0 :(得分:2)

上面的代码只有在使用不在纹理图集中的图像时才有效,因为它实际上并没有使用纹理图集。

在不使用SKTextureAtlas的情况下,无法从Atlas(我所知道的)中取出图像。所以你不能直接抓住这些图像。

(SWIFT):

let atlas = SKTextureAtlas(named: "BackgroundImages") // atlas name
var backgroundFrames = [SKTexture]()

let imageCount = atlas.textureNames.count
for var i=1; i<= imageCount/2; i++ {
  let textureName = "dodgR - animation \(i)"
  backgroundFrames.append(atlas.textureNamed(textureName))
}

目标-C(未经测试)

SKTextureAtlas *atlas = [SKTextureAtlas named:@"BackgroundImages"]; // atlas name
NSMutableArray *backgroundFrames = [NSMutableArray new];

int imageCount = atlas.textureNames.count;

for (int i = 1; i <= imageCount/2; i++) {
   NSString *textureName = @"dodgR - animation \(i)";
   [bacgroundFrames addObject:[atlas textureNamed:textureName]];
}

BONUS:Xcode 7现在允许您在资产文件夹内创建精灵地图集。