I’ve been playing with PlayCanvas for a few days now, but am trying to grasp the flow of how things work, coming from a Flash Actionscript 3 background.
I am trying to get app.fire and app.on to work in a test project of mine, but I just can’t seem to get it working. I’ve got a box on the screen called “Box” with a script attached to it called box.js. Then I’ve got another script called game.js. I’m trying to control the box from game.js, just so I can understand how events work between scripts. I have tried following the tutorials and a couple of posts I found in the forums regarding this, but no luck.
There are quite a few things going on here so I go through them as much as I can.
game.js script was not added to any entity in the scene so the code never runs.
As you put both the event callback and the event fire in the initialize() functions of the scripts, the order execution matters and you may have the event fire called before the callback has been initialized(). In this example, you should call the event fire in the postInitialize() function which gets called after initialize(). That way the callback is guaranteed to be setup before the event is fired.
This is terrific, thanks so much for the detailed reply and fixed project! I forked your version over so feel free to delete it on your end.
Before editing, I had asked what entity game.js was attached to because I couldn’t find it, but then I saw it was attached to root. Edited to remove that question.
Thanks again, this will really help me get around the nuances of PlayCanvas. There are a lot of gotchas I’m expecting to encounter, but will take it one step at a time. Hopefully one day I will be able to help people like you helped me, which I fully intend to do once I get the hang of things!
As another tip, if you want to find out what entities are using a particular asset, you can right click on the asset in the asset panel and go to ‘References’.
In A script :
var paras = "Hello, PlayCanvas !"
this.app.fire('event-name', arg);
In B script:
this.app.on('event-name', foo, this);
var foo = function(some-args) {
alert(some-args);
};