Sign in to follow this  
DbD

accessing variables from other source files?

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?

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.