Jump to content

Using functions in other functions in the prototype


dragonbiuex
 Share

Recommended Posts

Hello, I'm trying to get a button in my game to use other functions, and I haven't quite figured out how, other than brute forcing it.

// Let's say I have everything preloaded and I'm in my game state filevar main = function(game) {    var button1;    var button2;};// and my prototype is something likemain.prototype = {    create: function() {        button1 = this.add.button(0, 0, 'button1', this.button1);        button2 = this.add.button(100, 100, 'button2', this.button2);    }    button1: function() {        // I might want to use otherfunc to do stuff while doing other stuff    }    button2: function() {        // this is where I want to use button1's function and do other things    }    otherfunc: function() {        // does more stuff    }}

Help is greatly appreciated.

Link to comment
Share on other sites

You need to give the button context.
Look at the Button docs, and the first thing you see is this:

new Button(game, x, y, key, callback, callbackContext, overFrame, outFrame, downFrame, upFrame)

This is what this.add.button is calling when you use it, and you can put more arguments in your call.

button(x, y, key, callback, callbackContext, overFrame, outFrame, downFrame, upFrame, group)

You have everything up to callbackContext. If you pass 'this' through for the context, you can call otherfunc.

main.prototype = {    create: function() {        button1 = this.add.button(0, 0, 'button1', button1, this);        button2 = this.add.button(100, 100, 'button2', button2, this);    }    button1: function() {        // I might want to use otherfunc to do stuff while doing other stuff        this.otherFunc();    }    button2: function() {        // this is where I want to use button1's function and do other things        this.button1();    }    otherfunc: function() {        // does more stuff    }}

Now, with that out of the way, you might want to reconsider your function names...

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...