Hi everyone,
I started learning p5.js a couple weeks ago and have been working on my first big project. I’m having a hard time troubleshooting an issue involving mousePressed()
used inside of an object. Here’s a simplified version of my code. This first version works fine. An image (which I’m using as a button) is created inside the ExampleObject
. When the image is clicked on, the testCallback()
function prints “Success” to the console. Great!
function setup() {
...
}
function draw() {
...
}
function testCallback() {
console.log("SUCCESS!");
}
function ExampleObject() {
this.button = createImg('test.png', "Play");
this.button.mousePressed(testCallback);
}
However, I want the callback function to be defined inside the object like this:
function setup() {
...
}
function draw() {
...
}
function ExampleObject() {
this.button = createImg('test.png', "Test");
this.button.mousePressed(this.internalCallback);
this.internalCallback = function() {
console.log("SUCCESS!");
}
}
This second version doesn’t work though. The console prints this error:
Uncaught TypeError: fxn is undefined
eventPrependedFxn http://127.0.0.1:5500/p5.js:52063
[p5.js:52063:15](http://127.0.0.1:5500/p5.js)
eventPrependedFxn http://127.0.0.1:5500/p5.js:52063
eventPrependedFxn self-hosted:844
Can someone please explain the mistake I’m making?
Thanks!
Jason