Search code examples
javascriptcreatejs

Javascript object gets set to undefined after function that creates it ends


I'm using create.js, but whenever the quequeSetup() ends queque gets set to undefined, as proven by the console.log()'s. Down below is the affected snippet.

var queue;

function init() {
    queueSetup();
};

function queueSetup() {
    var queue = new createjs.LoadQueue(false);

    console.log(queue);
    queue.installPlugin(createjs.Sound);
    console.log(queue);
    queue.addEventListener("complete", queueLoaded);
    queue.loadManifest([
        {id:"nbckg", src:"images/nbckg.png"},
        {id:"newgame", src:"images/newgame.png"}
    ]);

    console.log(queue);
}

function queueLoaded() { 
    //the following console.log logs undefined
    console.log(queue);

    compName = "NULLCOMPNAME "; 
    stage = new createjs.Stage("MyCanvas");

    createjs.Ticker.addEventListener("tick", tick);

    //LOAD MAINMANU

    // loadMainMenu();
    initUI();
}

Solution

  • I'm not familiar with createjs, but in queueSetup(), the var queue creates a new variable named queue scoped to the function. To reference the queue variable outside the function, leave out the var.