Invoking a function in the javascript code not working properly

Hi,

I’m having problems calling a function that has been defined in the code. See below.

 properties: 
{
 label1 : cc.Label,
},

onLoad () {},

start () {

    var x = Multiply(4, 3);
    this.label1.string = x;

},

Multiply : function(a, b)
{
    return a * b;   
},

The function ‘Multiply’ is defined at the bottom and called in start(). However when the game is loaded it just gives a black screen (with no frame rate…etc so the game isn’t running). This isn’t a problem with this specifc function, its a problem with all functions I’ve tried to make. However if a button is pressed in the game to invoke a function then the function does work.

I’m assuming I’m not invoking functions in the code properly. It’s confusing though as I followed standard info for javascript on how to go about this. Does Cocos Creator work differently? Can anyone suggested whats wrong?

Thanks :smile:

Never mind I’ve managed to solve it.

For anyone else who sees this and is confused like I was you have to put ‘this.’ before any function name otherwise it won’t work. I guess that tells it to look in this script to find the function. So the code should be:

start(){

    var x = this.Multiply(4, 3);
    this.label1.string = x;             },

Sorry I know that probably really obvious to most people who see this but as someone starting out I didn’t realise that you need to use ‘this.’ on functions as well. :upside_down_face:

1 Like

Thanks this helped :slight_smile: