I am VERY new to Playcanvas and I’m just trying to set up something where if I click a button it prints an alert message in the browser (nothing more :))
However, I want all the code for the button to be handled in a script (main) that is not attached to the button.
I think I can probably do it if I attach a new script to the button to talk with my main script but is this even necessary? Is there some way I can detect the mousedown event on the button only from the main script?
Basically I don’t want to have millions of scripts for each UI element in my app. Hope you understand.
Okay great thanks! Just to clarify though, there is no way I can do this without attaching a new script component to each button? ie, I cant access the mousedown through each button’s element component or something?
I have a button object in the scene called Button_Test and I just have a script attached to another entity that looks like this…
var UiBehaviour = pc.createScript('uiBehaviour');
// initialize code called once per entity
UiBehaviour.prototype.initialize = function() {
};
// update code called every frame
UiBehaviour.prototype.update = function(dt) {
};
//events
var button_test = this.app.root.findByName('Button_Test');
button_test.element.on('mouseup', function()
{
console.log("Hello this is a test debug log. We have begun!");
}, this);
Right, so your code is executing way before the Playcanvas app has been initialized and your entities are available, because it’s running outside of any script method.
Also this.app will always be undefined there.
Try using the following:
var UiBehaviour = pc.createScript('uiBehaviour');
// initialize code called once per entity
UiBehaviour.prototype.initialize = function() {
//events
var button_test = this.app.root.findByName('Button_Test');
button_test.element.on('mouseup', function()
{
console.log("Hello this is a test debug log. We have begun!");
}, this);
};
// update code called every frame
UiBehaviour.prototype.update = function(dt) {
};