The basic process is:
2. Load the library. It's similar to using ajax in that the file can take time to load, and it must be loaded before you can use it.
"var js = document.createElement('script');
js.src = 'p2.min.js';
window.libraryLoaded = false;
js.onreadystatechange = js.onload = function()
3. Once the library is loaded you can pretty much use it like the documentation of the library. Keep in mind that without the sdk we're limited what we can access, so we can only communicate numbers and strings back and forth.
On a side note it is possible to access more but it is considered very hacky and will break if you export minified. Still it could be useful in some cases to try out an idea for a plugin.
Here's an example to implement a physics library so that we can do something that's not possible to do with the physics behavior or even my chipmunk behavior: [b]Draw the path an object will follow, including it's bounces off obstacles.[/b] Basically it requires doing multiple simulation steps at a time, which would require significant changes to the existing behaviors to do.
So first we pick a library. I picked this one which has pretty good documentation:
And here's the example capx:
[url=https://www.dropbox.com/s/j76abx1fu5l91mb/js_p2_physics.capx?dl=1]https://www.dropbox.com/s/j76abx1fu5l91 ... .capx?dl=1[/url]
It's pretty basic and only does what we need and doesn't do much else.
One quick note in the capx you'll see js like this: "window.foo = value;" instead of "var foo=value;". The reason is "window.foo" makes a global variable and "var" can't be accessed in another execJs action. Both can be accessed with "foo" elsewhere.
I won't cover what I did exactly, you can look at the capx for that, but I basically just followed the examples for p2.js.
Part of it is we need to pass object locations to js. To do that just use "&" to make the text with values:
"window.c2pos = ["& ball.X &","& ball.Y &"]"
Instead of typing the values directly:
"window.c2pos = [100,50]"
And that's basically it to pass values to js. The other way, js to C2, can be done with the Browser.execJS() expression.
As stated before without the sdk you're limited what you can access. For something like physics one big one is you can't access an object's collision polygon. For stuff that should draw stuff in your game you have to use the sdk, because the runtime chooses when to draw.