资源路径的问题
15951836388 opened this issue · 9 comments
不同的ide生成的运行文件对路径的处理貌似不同。goland识别不了路径(引擎demo也是这样)
所以建议 使用绝对路径(本项目的路径),减少不同ide带来的问题。还是我的设置不对?设置输出路径,或者不设置都是不行的
资源路径使用的是gomobile默认的处理方式,既 assets
目录时放在工程下面的。一般在工程目录下执行:go run main.go
即可运行工程。 用IDE要看当前工程的 workpace 目录配置,把它设置为当前的工程目录(IDE本质上也是执行 go run
命令)。
我用litide和 goland都不行。你用的什么ide?
我用Goland写大型工程,这个Demo我用命令: go run *.go
。
Last login: Thu Jun 14 15:45:04 on ttys002
yuanxuhuideMacBook-Pro:flappybird yuanxuhui$ go run *.go
2018/06/14 17:54:40 Game Start! Flappy Bird
2018/06/14 17:54:40 Load table: 8
2018/06/14 17:54:40 0 table - *game.ScriptTable
2018/06/14 17:54:40 1 table - *game.TagTable
2018/06/14 17:54:40 2 table - *gfx.SpriteTable
2018/06/14 17:54:40 3 table - *gfx.MeshTable
2018/06/14 17:54:40 4 table - *gfx.TransformTable
2018/06/14 17:54:40 5 table - *gfx.TextTable
2018/06/14 17:54:40 6 table - *effect.ParticleSystemTable
2018/06/14 17:54:40 7 table - *frame.FlipbookTable
3.2.1 Cocoa NSGL chdir menubar retina
2018/06/14 17:54:41 alloc shader id:(1, 3)
2018/06/14 17:54:41 bind attr: 1 => xyuv
2018/06/14 17:54:41 bind attr: 0 => rgba
2018/06/14 17:54:41 alloc uniform: (1, 1) => proj
2018/06/14 17:54:41 alloc uniform: (2, 0) => tex
2018/06/14 17:54:41 alloc shader id:(2, 6)
2018/06/14 17:54:41 bind attr: 1 => xyuv
2018/06/14 17:54:41 bind attr: 0 => rgba
2018/06/14 17:54:41 alloc uniform: (3, 5) => proj
2018/06/14 17:54:41 alloc uniform: (4, 0) => model
2018/06/14 17:54:41 alloc uniform: (5, 4) => tex
2018/06/14 17:54:41 alloc shader id:(3, 9)
2018/06/14 17:54:41 bind attr: 1 => xyuv
2018/06/14 17:54:41 bind attr: 0 => rgba
2018/06/14 17:54:41 alloc uniform: (6, 0) => projection
2018/06/14 17:54:41 alloc uniform: (7, 4) => tex
2018/06/14 17:54:41 alloc index-buffer: (1, 1)
2018/06/14 17:54:41 alloc vertex-buffer: (1, 2)
2018/06/14 17:54:41 font img bounds: (0,0)-(999,54)
2018/06/14 17:54:41 alloc texture id: (1, 1)
2018/06/14 17:54:41 dbg init w,h 0 0
2018/06/14 17:54:41 load file:images/bird.png
2018/06/14 17:54:41 texture "images/bird.png" not found: open images/bird.png: no such file or directory
open fonts/Marker Felt.ttf: no such file or directory
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x30 pc=0x4147f9a]
goroutine 1 [running, locked to thread]:
korok.io/korok/gui.SetFont(0x0, 0x0)
/Users/yuanxuhui/Documents/go/src/korok.io/korok/gui/gui.go:88 +0x7a
main.(*StartScene).OnEnter(0xc42007e180, 0xc42018c000)
/Users/yuanxuhui/Documents/go/src/github.com/ntop001/flappybird/main.go:36 +0x92
korok.io/korok/game.(*SceneManager).Setup(0xc42018c088, 0xc42018c000)
/Users/yuanxuhui/Documents/go/src/korok.io/korok/game/scene.go:58 +0xaa
korok.io/korok/game.(*Game).Create(0xc42018c000, 0x3c040000000)
/Users/yuanxuhui/Documents/go/src/korok.io/korok/game/game.go:167 +0x7b0
korok.io/korok/game.(*Game).OnCreate(0xc42018c000, 0x3c040000000)
/Users/yuanxuhui/Documents/go/src/korok.io/korok/game/game.go:68 +0x37
korok.io/korok/hid.CreateWindow(0xc420065ee0)
/Users/yuanxuhui/Documents/go/src/korok.io/korok/hid/window_glfw.go:116 +0x3d9
korok.io/korok.Run(0xc420065f40, 0x4227940, 0xc42007e180)
/Users/yuanxuhui/Documents/go/src/korok.io/korok/korok.go:69 +0x87b
main.main()
/Users/yuanxuhui/Documents/go/src/github.com/ntop001/flappybird/main.go:147 +0x97
exit status 2
yuanxuhuideMacBook-Pro:flappybird yuanxuhui$
我在不同机器上都试过工程是没有问题的,过程:
- git clone git@github.com:ntop001/flappybird.git
- cd flappybird
- go run *.go
之所以报资源找不到的问题,是因为:
// korok.io/asset/mtexture.go
imgFile, err := asset.Open(file)
if err != nil {
return bk.InvalidId, fmt.Errorf("texture %q not found: %v", file, err)
}
底层调用的是 asset.Open(file)
方法(这是 gomobile 的 asset 包),它会默认从当前的工程目录下查找文件,请确认,当前目录下是存在这些资源文件的。以上面为示例,我的工程目录是这样的:
flappybird git:(master) tree .
.
├── README.md
├── assets
│ ├── fonts
│ │ ├── Marker\ Felt.ttf
│ │ ├── futura-48.fnt
│ │ └── futura-48.png
│ ├── images
│ │ ├── bird.json
│ │ └── bird.png
│ └── sound
│ ├── collision.ogg
│ ├── drop.ogg
│ ├── point.ogg
│ ├── rise.ogg
│ └── swooshing.ogg
├── game.go
├── images
│ └── flappybird.final.gif
├── main.go
└── pipe.go
如果资源没有问题,你可以跑个小程序测试一下 asset.Open(file)
方法是否可以正常工作。是不是当前工作路径不对,这应该是系统配置的问题了。
你的 GoLand 配置的 "Working directory" 是 /User/yuanxuhui/Documents/go/
应该配置成工程的目录才对,修改后 GoLand 应该可以正常工作。另外一个问题是,资源路径应该不需要添加 assets
前缀的。
- 确认工程目录是
flappybird/assets/
而不是flappybird/assets/assets
- 代码是最新的吗?因为早前(大约是几个月前 )的代码确实是要加
assets
前缀的
问题解决了。是我的gomobile代码是旧的
x里的包不能直接更新。墙
我手动下载的。
有什么办法也能自动更新呢
解决就好。现在也没发现有哪些自动更新的方法,我一般用 git pull
更新的, 确实麻烦。