Getting values of an object into a event

I trying to make a script which has a lot of "ButtonObject"s, which contain p5 buttons and some data. I want to use this data inside of a event (like "mousePressed") which is registered to a p5 button of the respective "ButtonObject".

The problem is, that when the button is pressed the console does output 'undefined' instead of 'Text'.

Is there a simple way to do this or is it impossible?

sketch.js:

var button;

function setup() {
    noCanvas();
    button = new ButtonObject();
}

button.js:

function ButtonObject() {
    this.but = createButton('Button');
    this.txt = 'Text';

    this.but.mousePressed( function ( ) {
        console.log(this.txt);
    });

}

index.html:

<html>
    <head>
        <meta charset="UTF-8">
        <script language="javascript" type="text/javascript" src="libraries/p5.js"></script>
        <script language="javascript" src="libraries/p5.dom.js"></script>
        <script language="javascript" type="text/javascript" src="sketch.js"></script>
        <script language="javascript" type="text/javascript" src="button.js"></script>
        <style> body {padding: 0; margin: 0;} </style>
    </head>
    <body>
    </body>
</html>

Thanks in advance!

Sign In or Register to comment.