【问题标题】:why can't SpriteKit find my images?为什么 SpriteKit 找不到我的图像?
【发布时间】:2013-12-17 16:37:31
【问题描述】:

我正在关注 Ray Wenderlich 网站上的 SpriteKit 教程。我用 SpriteKit 模板创建了一个项目。问题是我的图像显示为一个大的白色矩形,里面有一个大的红色 X。日志中没有错误。我还在阅读 SpriteKit Programming Guide 和 Texture Atlas 帮助。我假设这是项目设置问题或文件夹结构问题。我对 Xcode 很陌生。这是我的项目在 Xcode 中的样子:

MyGameProj (the top level, with the little blue xcode icon)
-->MyGameProj (yellow folder)
 -->Assets (yellow folder)
   -->Atlases (yellow folder)
     -->dungeon.atlas (blue folder)
       dungeon_1.jpg
       dungeon_2.jpg
       dungeon_3.jpg
       hero_trans.gif
  various .h and .m files

起初,Assets 文件夹位于 MyGameProj 文件夹之外。都没有用

这是代码。这是在MySceneinitWithSize() 内:

SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:@"dungeon"];
SKTexture *texture = [atlas textureNamed:@"hero_trans.gif"];
self.player = [SKSpriteNode spriteNodeWithTexture:texture];
self.player.position = CGPointMake(100, 100);
[self addChild:self.player];

我已经进入构建设置并打开了图集生成。

【问题讨论】:

  • 在下面查看我的答案。但也只是想 - 为什么是 gif?如果您想要运动等外观,最好使用一系列动画图像
  • 我不知道为什么它是一个 gif;没有动画;只是我从某个地方得到的一个精灵

标签: xcode sprite-kit


【解决方案1】:

我认为您不能在Sprite-Kit 中使用带有纹理图集的 .gif 文件。我已经对您的代码进行了一些实验。将 .gif 文件放在图集之外并使用 spriteNodeWithImageNamed: 相反,有效。

测试 1:与您相同的代码,dungeon.atlas 中的 .gif:不起作用

SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:@"dungeon"];
SKTexture *texture = [atlas textureNamed:@"Seven_segment_display-animated.gif"];
self.player = [SKSpriteNode spriteNodeWithTexture:texture];
self.player.position = CGPointMake(100, 100);
[self addChild:self.player];

测试 2:使用来自 dungeon.atlas 的 .gif 的 spriteNodeWithImageNamed: 创建的节点:不起作用

self.player = [SKSpriteNode spriteNodeWithImageNamed:@"Seven_segment_display-animated.gif"];
self.player.position = CGPointMake(100, 100);
[self addChild:self.player];

测试 3:将 .gif 文件加载到项目本身(不是图集),例如资产文件夹:这可行

self.player = [SKSpriteNode spriteNodeWithImageNamed:@"Seven_segment_display-animated.gif"];
self.player.position = CGPointMake(100, 100);
[self addChild:self.player];

【讨论】:

  • @LearnCocos2D 它看起来但实际上并没有做任何事情,没有 GIF 动画
  • 我也是这么想的。我们不能在SKTextureAtlas 中使用 gif
  • 谢谢,就是这样;我将其更改为 .png 并且有效;如果这在 SpriteKit 文档中会很好
  • 我在您的代码中看不到测试 2 和测试 3 之间的任何区别
  • @OMGPOP - 在测试 2 中,图像在 atlas 文件夹中,在测试 3 中,它在它之外
猜你喜欢
  • 1970-01-01
  • 2020-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-12
  • 2018-03-05
  • 1970-01-01
相关资源
最近更新 更多