【问题标题】:Cocos2d call random static spriteCocos2d 调用随机静态精灵
【发布时间】:2014-04-25 18:05:46
【问题描述】:

我正在尝试从我的 plist 中调用一个随机静态精灵。在这种情况下,它是一个可收藏的球体,我有 4 张图像,我想从名为 orb_0.png 到 orb_3.png 中随机挑选

我可以调用单个文件图像,而且一切都是花花公子。

 - (CCSprite *)sprite {
 if(_sprite == nil) {
 _sprite = [CCSprite spriteWithSpriteFrameName:@"orb_0.png"];

但是,当我使用以下代码代替时,它要求图像但不显示它们;

int randomOrb = rand( ) % 4;
 
 _sprite = [ CCSprite spriteWithFile:[ NSString stringWithFormat:@"orb_%d.png", randomOrb]];

我在调试器中得到的信息说无法重复找到图像。

2014-04-25 18:58:16.322 MadScientist[2210:60b] cocos2d: CCFileUtils: 未找到警告文件:orb_0-hd.png

2014-04-25 18:58:16.323 MadScientist[2210:60b] cocos2d:CCTexture2D。 无法创建纹理。 UIImage 为零

2014-04-25 18:58:16.323 MadScientist[2210:60b] cocos2d:无法添加 图片:CCTextureCache中的orb_0.png

2014-04-25 18:58:16.323 MadScientist[2210:60b] cocos2d: CCFileUtils: 未找到警告文件:orb_3-hd.png

2014-04-25 18:58:16.323 MadScientist[2210:60b] cocos2d:CCTexture2D。 无法创建纹理。 UIImage 为零

2014-04-25 18:58:16.324 MadScientist[2210:60b] cocos2d:无法添加 图片:CCTextureCache中的orb_3.png

【问题讨论】:

  • 在您的第一个示例中,您使用的是 spriteWithSpriteFrameName,但在您的第二个示例中,您使用的是 spriteWithFile。这可能是问题吗?

标签: objective-c cocos2d-iphone sprite


【解决方案1】:

试试sprite = [CCSprite spriteWithImageNamed:[NSString stringWithFormat:@"orb%d.png", randomOrb]]; 那应该可以解决你的问题。

【讨论】:

  • _sprite = [CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:@"orb_%d.png", randomOrb]];我稍微调整了一下让它工作,谢谢 Akaino
  • 哦,好吧,不知道你已经习惯了 spriteFrame。一定是监督的。但我很高兴你现在让它运行!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-18
相关资源
最近更新 更多