GameZero.jl中Actor()方法无法加载指定图像的问题及解决
GameZero.jl Actor() 找不到图像文件的解决办法
在使用GameZero.jl时,调用Actor()方法指定图像绝对路径或相对路径都会触发错误,比如尝试以下代码:
ship = Actor("C:/path/to/images/image.png"; pos=(100, 100), angle=90)
或
ship = Actor("./images/image.png"; pos=(100, 100), angle=90)
会抛出如下错误:
ERROR: UndefRefError: access to undefined reference Stacktrace: [1] getproperty @ .\Base.jl:37 [inlined] [2] getindex @ .\refvalue.jl:56 [inlined] [3] file_path(name::String, subdir::Symbol) @ GameZero C:...\.julia\packages\GameZero\q74y7\src\resources.jl:46 [4] image_surface(image::String) @ GameZero C:...\.julia\packages\GameZero\q74y7\src\resources.jl:37 [5] Actor(image::String; kv::Base.Pairs{Symbol, Any, Tuple{Symbol, Symbol}, NamedTuple{(:pos, :angle), Tuple{Tuple{Int64, Int64}, Int64}}}) @ GameZero C:...\.julia\packages\GameZero\q74y7\src\actor.jl:18 [6] top-level scope @ c:...\Documents\practice_gamezero\main.jl:59
即使是运行官方示例代码并替换为占位图像,也无法正常定义Actor。
解决方案
- 按规范组织目录结构:将图像文件放在项目的
images子目录下(GameZero会自动识别该目录为资源目录),调用Actor()时直接使用图像文件名,无需添加任何路径:ship = Actor("image.png"; pos=(100, 100), angle=90) - 使用正确的启动命令:必须通过
rungame(path/to/main.jl)命令启动项目,不能直接运行main.jl文件。
内容的提问来源于stack exchange,提问作者Jonathan F.
相关产品推荐
相关产品推荐

