Discworld Posted September 17, 2017 Share Posted September 17, 2017 Im attempting to make a game with Phaser and nodeJS. I was doing my character as a graphic (game.add.graphic) but couldn't figure out how to remove the graphic so I figured I'd make a sprite and load it. var Game = { preload: function(){ game.load.image("snakeBody", "../../assets/snakeBody.png"); game.load.image("apple", "../../assets/apple.png"); }, my folder structure is like so: -assets -static -scripts -game.js But it tells me that it can't find both files. I've also tried to add this line app.use(express.static('assets')); and then var Game = { preload: function(){ game.load.image("snakeBody", "localhost:3000/assets/snakeBody.png"); game.load.image("apple", "localhost:3000/assets/apple.png"); }, But same error occurs. EDIT: Solved it, tursn out I had to set the path as just "/snakeBody.png". I still dont understand why, but this worked. Link to comment Share on other sites More sharing options...
mattstyles Posted September 18, 2017 Share Posted September 18, 2017 On 17/09/2017 at 7:56 AM, Discworld said: I still dont understand why, but this worked. Your call to `express.static('assets')` sets `assets` as the base folder, which means that when you request `/snakeBody.png` it looks at the root, which is (as far as the static module knows) at `assets`. `../../assets/snakeBody.png` does not work because paths don't go relative to where your JS file is, they'll go relative from wherever you serve your html file from ordinarily, there are some rules (such as the `static('assets'`) which can change how your server routes requests. Discworld 1 Link to comment Share on other sites More sharing options...
Discworld Posted September 18, 2017 Author Share Posted September 18, 2017 Yea turns out the first time I tried it with express.static I had it as "snakeBody.png" without the "/" and that's why it hadnt worked. Thanks! mattstyles 1 Link to comment Share on other sites More sharing options...
Recommended Posts