Jump to content

Getting started: accessing local files


jisseb
 Share

Recommended Posts

Hello,

 

As a total beginner with Phaser, i've started with that:

http://phaser.io/getting-started-js.php

 

In the first lines, it's written i have to set up a web server for accessing files.

It's about security, if i understand well. The web browsers, according to the page above, won't allow local files access.

 

It's said:


 

Your game is going to need to load in resources. Images, audio files, JSON data, maybe other JavaScript files. In order to do this it needs to run unhindered by the browser security shackles. It needs http:// access to the game files. And for that we need a web server.

 

 

Ok, but in that case, the following code shouldn't work:

(this code was taken from the samples, it's the first one)

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create });function preload() {// The second parameter is the URL of the image (relative)game.load.image('einstein', 'assets/pics/ra_einstein.png');}function create() {game.add.sprite(0, 0, 'einstein');}

This works, the picture is loaded from my disk, not from the web.

 

I've checked this by looking the network activity with firefox debugger.

It tries to load phaser.js from cdn.rawgit.com, fails and then use a «local copy».

Then it gets a json file , and a css file from other websites (succeed).

The Einstein picture is not loaded from a web server, but from my disk.

(checked by just rename the ra_einstein.png file on my disk: when done, no picture is displayed).

 

So can you explain what is the problem with local files ? Of course, for sharing a phaser game with others, i would use a web server. But for experimenting and creating, what is the point ?

 

Thank you for reading this, i hope it's not a dumb question for my first post :)

Link to comment
Share on other sites

The above code doesn't work in most browsers. Try it and you'll get all kinds of security errors pop-up. Chrome, IE, even Firefox by default won't run it if it's access via file://

 

The issue isn't about reading a file locally, it's about reading a file via http vs. file:// - if you're browsing the web in a browser set to allow JavaScript to access any local file, anywhere on your hard drive, the security implications of that should be immediately obvious. You can set browsers to do this though, Chrome has a flag to allow local file access, Firefox also.

 

Once your game is actually finally deployed, it will always be served via http, so I'd just get used to testing via that also, because you never know what oddities may be thrown up that aren't exposed until you do so.

Link to comment
Share on other sites

Well… i don't understand why it works offilen in my Firefox.

User Agent is:

Mozilla/5.0 (X11; Linux x86_64; rv:33.0) Gecko/20100101 Firefox/33.0

 

I've checked security options in about:config, but they seem to be all set by default

(according to http://stackoverflow.com/questions/192080/firefox-links-to-local-or-network-pages-do-not-work )

 

I've tested also with Chromium and Opera, but they don't load local files by default on my machine.

So only Firefox behaves mysteriously.

 

So, i will set a nginx serving only on localhost, to avoid spreading pre-releases to the whole world :)

 

Thank you for answering.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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