【问题标题】:How to change a sprite's image when a score changes当分数改变时如何改变精灵的图像
【发布时间】:2019-01-20 15:24:02
【问题描述】:

我一直在使用 Sprite Kit 在 Swift 中创建 Xcode 游戏。但是,当我尝试在高分超过 40 时更改角色的图像时,它不起作用。

这是代码:

if  highScore >= 40{
    player = SKSpriteNode(imageNamed: "start")
}

不太确定我哪里出错了,但任何建议都将不胜感激。

这里是源代码的链接:

https://github.com/skullamunger/Shootr-Master

【问题讨论】:

  • 仅供参考 - 您不是在创建“Xcode 游戏”。您正在创建 iOS 游戏或 macOS 游戏。

标签: swift sprite-kit


【解决方案1】:

您应该只更新播放器纹理:

if  highScore >= 40 {
    player.texture = SKTexture(imageNamed: "start")
}

这里你不是在创建一个新的精灵,你只是在更新它的纹理。

【讨论】:

  • 谢谢@YannickLoriot,但由于某种原因这仍然不起作用,游戏运行但它仍然不会改变角色。我会上传源代码,以防我更早的地方出错了
  • @skullamunger 在您的情况下,highScore 变量在此方法中始终等于 0,这就是精灵不更新其纹理的原因。看来你的最高分存储在UserDefaults().integer(forKey: "highscore")
  • 正如我所说,使用 if UserDefaults().integer(forKey: "highscore") >= 40 { 而不是 if highScore >= 40 {。但是你应该重构你的代码以提高效率。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多