How can I add custom Javascript to an HTML5 game

designpeg

  • *
  • Posts: 689
Hi, I know I can do this by adding the script to the html5 template at Stencyl/plaf/haxe/lib/assets/templates/html5/templates/index
but is there a better way of adding as script? Ideally i'd keep the script in the games extras folder.

SadiQ

  • Posts: 1780
Why not inside an extension?
Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.

designpeg

  • *
  • Posts: 689
Hi, I'm building an extension, but I can't work out how to add a javascript file to the template.

mdotedot

  • Posts: 1560
You should be able to read the data from the extras folder. There is an external data extension that might do the trick. And otherwise there would be other options.

You read the file into a variable (Game Attribute) and pass this to this code block (or Extension)
js.Lib.eval(getGameAttribute("YourAttribute"));

You can look at my Mobile HTML5 extension which has this block for futher reading/testing.


Hanging out in the Chat:  http://www.stencyl.com/chat/

Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.

designpeg

  • *
  • Posts: 689
Cool, I'll try that.  My javascript works when I manually add it to the template file - I can call functions from stencyl and vice versa, I'm just thinking that if I get the extension working and want to share it, I'd need to have everything contained in the extension, and not need these hacky requirements.