Jump to content

accessing variables from other source files?


DbD
 Share

Recommended Posts

Hi I was wandering how to use a variable called game in a differenct java file than it was declared in. I know in basic java I could use something like

public static game = *code here*

but that doesn't work with Phaser for some reason. Declaring it as

var game = *code here*

 

how can i access it in a different file?

Link to comment
Share on other sites

You are mixing Javascript with Java.

if you are new to javascript you might want to take look at Javascript Scope

 

var game = *code here* should be accessible in different files if you are defining it outside a function, if you still can't access it then take a look at loading order of the files, and check console for errors.

Link to comment
Share on other sites

Have it be a function from a different file, like this:

 

MyExternalFunctions.js

function doSomethingElse() {  console.log("Doing something from another file!");}

Main.js

function doSomething() {  console.log("Doing something from the same file.");}function create() {  doSomething();  doSomethingElse();}

Then so long as both of these files are loaded, both functions will be called inside create. The best path from here is to try to limit what functions have access to by passing them the things they need to work on as parameters. This keeps code clean and modular.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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