How do I import a java Script Library via Browser Object?

0 favourites
  • 2 posts
From the Asset Store
Forget about default textbox restrictions, you can create sprites atop of the textbox
  • How do I import a javascript Library via Browser Object?

    For example: Band.js

    https://github.com/meenie/band.js

    A tutorial and example on how to do so would be highly appreciated. I am sure most of you help with these things because you enjoy to. That said, I'm willing to create you some music, or sprite art in return for your time.

    Thanks ahead of time XD

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • If you add the js file you want to use to your project's "files" folder then you can load it with:

    "$.getScript('band.js');"

    Or if you have the URL of the library you could use that too.

    Now, it won't be immediately usable. That's a request to download and run it. You can either wait a while and guess when it's loaded to use it, or if you add the fuction object to your project you can have it trigger a event sheet function when it's done loaded and ready to use.

    Say you call the function "loaded" then the js you run would be:

    "$.getScript('band.js', function(){c2_callFunction('loaded', []);});"

    Now, that works if the JavaScript library is one file, but some depend on other files to be loaded first so you'd need to add more events for that in the same way. You can search for posts by me about the browser object to get some examples. Also note this is made much simpler if you made a plugin, since you can just add a list of dependencies to editime.js and they just get loaded in order with no fuss.

    Next thing to note is you'll be getting used to looking at the browser's dev console a lot since any typos will lead to errors. If you did it in a plugin the web browser can be more helpful in pointing out what is amiss. Code run from the browser object gives more vague errors.

    Finally if you define any JavaScript vars inside an execJs action it won't exist next time you run it.

    For example if you run this

    "var myVariable=1337;"

    Then run this, you'll get an error because the var no longer exists.

    "myVariable+1"

    You can make a global var by using "window" like so:

    "window.myVariable=1337;"

    Then myVariable is global and you can use it like the following without issue:

    "myVariable+=1;"

    The logic behind this is "window" is the global namespace in the browser. I think it still is in nwjs but I'm not certain since I've seen libraries set themselves up differently under nwjs.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)