草庐IT

swift - 为 SKSpriteNode 设置名称?

coder 2023-09-14 原文

我正在使用以下代码为游戏中的玩家生成随机图像。

var playerTexture = [SKTexture]()

        playerTexture.append(SKTexture(imageNamed: “red”))
        playerTexture.append(SKTexture(imageNamed: “blue”))
        playerTexture.append(SKTexture(imageNamed: “green”))


        let rand = Int(arc4random_uniform(UInt32(playerTexture.count)))
        let chosenColor = playerTexture[rand] as SKTexture
        let playerSkin = chosenColor

上面的代码生成了一个名为 playerSkin 的随机 SKTexture(红色、蓝色或绿色)。

    player = SKSpriteNode(texture: playerSkin)
    player.size = CGSize(width: 50, height: 50)
    player.position = location
    self.addChild(player)
    player.name = chosenColor.description

然后代码的下一部分将玩家创建为 SKSpriteNode 并为其分配随机的红色、蓝色或绿色纹理。但最主要的是代码使用以下方法将 player.name 分配给随机选择的图像:

player.name = chosenColor.description

然后我使用打印检查播放器的名称。

 print(player.name)

但是,当我检查播放器的名称时,它显示为以下内容之一,具体取决于选择的随机图像:

可选(“\'红色\'(120 x 120)”)

可选(“\'蓝色\'(120 x 120)”)

可选(“\'绿色\'(120 x 120)”)

我的图像被称为“红色”、“蓝色”和“绿色”,它们位于 Xcode 的 Assets.xcassets 文件夹中。

我的图像是 120 x 120,但为什么会出现这些随机的长名称?如何仅将“red”、“blue”或“green”设置为播放器的名称?

我需要名称完全是“红色”、“蓝色”或“绿色”,因为我想稍后将 player.name 用于不同的功能。

最佳答案

可能有更好的方法来做到这一点,但这是我想到的第一件事 - 将 playerTexture 变量设为包含纹理和您要为其指定的名称的元组数组:

 var playerTexture = [(SKTexture, String)]()

  playerTexture.append((SKTexture(imageNamed: “red”), "red"))
  playerTexture.append((SKTexture(imageNamed: “blue”), "blue"))
  playerTexture.append((SKTexture(imageNamed: “green”), "green"))

  let rand = Int(arc4random_uniform(UInt32(playerTexture.count)))
  let chosenColor = playerTexture[rand].0 as SKTexture
  let colorName = playerTexture[rand].1
  let playerSkin = chosenColor

  player = SKSpriteNode(texture: playerSkin)
  player.size = CGSize(width: 50, height: 50)
  player.position = location
  self.addChild(player)
  player.name = colorName

关于swift - 为 SKSpriteNode 设置名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38440902/

有关swift - 为 SKSpriteNode 设置名称?的更多相关文章

  1. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  2. ruby-openid:执行发现时未设置@socket - 2

    我在使用omniauth/openid时遇到了一些麻烦。在尝试进行身份验证时,我在日志中发现了这一点:OpenID::FetchingError:Errorfetchinghttps://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username:undefinedmethod`io'fornil:NilClass重要的是undefinedmethodio'fornil:NilClass来自openid/fetchers.rb,在下面的代码片段中:moduleNetclass

  3. ruby-on-rails - 如何使用 instance_variable_set 正确设置实例变量? - 2

    我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击

  4. ruby-on-rails - date_field_tag,如何设置默认日期? [ rails 上的 ruby ] - 2

    我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问

  5. ruby-on-rails - 在 Rails 开发环境中为 .ogv 文件设置 Mime 类型 - 2

    我正在玩HTML5视频并且在ERB中有以下片段:mp4视频从在我的开发环境中运行的服务器很好地流式传输到chrome。然而firefox显示带有海报图像的视频播放器,但带有一个大X。问题似乎是mongrel不确定ogv扩展的mime类型,并且只返回text/plain,如curl所示:$curl-Ihttp://0.0.0.0:3000/pr6.ogvHTTP/1.1200OKConnection:closeDate:Mon,19Apr201012:33:50GMTLast-Modified:Sun,18Apr201012:46:07GMTContent-Type:text/plain

  6. ruby-on-rails - 有没有办法为 CarrierWave/Fog 设置上传进度指示器? - 2

    我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r

  7. ruby - 在 Ruby 中按名称传递函数 - 2

    如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只

  8. ruby-on-rails - 应用程序的名称是否可以作为变量使用? - 2

    当我创建一个Rails应用程序时,控制台:railsnewfoo我的代码可以使用字符串“foo”吗?puts"Yourapp'snameis"+app_name_bar 最佳答案 Rails.application.class将为您提供应用程序的全名(例如YourAppName::Application)。从那里您可以使用Rails.application.class.parent获取模块名称。 关于ruby-on-rails-应用程序的名称是否可以作为变量使用?,我们在StackOve

  9. ruby-on-rails - 如何从过时的 TZInfo 标识符中获取 Rails TimeZone 名称? - 2

    已经有一个问题回答了如何将“America/Los_Angeles”转换为“PacificTime(US&Canada)”。但是我想将“美国/太平洋”和其他过时的时区转换为RailsTimeZone。我无法在图书馆中找到任何可以帮助我完成此任务的东西。 最佳答案 来自RailsActiveSupport::TimeZonedocs:TheversionofTZInfobundledwithActiveSupportonlyincludesthedefinitionsnecessarytosupportthezonesdefinedb

  10. objective-c - 在设置 Cocoa Pods 和安装 Ruby 更新时出错 - 2

    我正在尝试为我的iOS应用程序设置cocoapods但是当我执行命令时:sudogemupdate--system我收到错误消息:当前已安装最新版本。中止。当我进入cocoapods的下一步时:sudogeminstallcocoapods我在MacOS10.8.5上遇到错误:ERROR:Errorinstallingcocoapods:cocoapods-trunkrequiresRubyversion>=2.0.0.我在MacOS10.9.4上尝试了同样的操作,但出现错误:ERROR:Couldnotfindavalidgem'cocoapods'(>=0),hereiswhy:U

随机推荐