Jump to content

Can't load images by any means.


Discworld
 Share

Recommended Posts

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

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.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...