Reputation: 678
I am trying to declare properties and functions to a specific js class. But when I try to call function inside a function (even if called after declared) it throws a exception
Uncaught TypeError: Object [object Object] has no method 'addButtonEventListeners
My code listed: SplashScene.js
var anim1, background;
var SplashScene;
function SplashScreen(SceneContainer)
{
this.name = "SplashScreen";
this.loadScreen = function()
{
background = new createjs.Shape();
background.graphics.beginBitmapFill(loader.getResult("gameLoopSplash")).drawRect(0,0,w,h);
SplashScene = new createjs.Container();
anim1 = new createjs.Sprite(buttonData, "playIdle");
anim1.framerate = 30;
anim1.x = 260;
anim1.y = 22;
SplashScene.alpha = 0;
SplashScene.addChild(background);
SplashScene.addChild(anim1);
}
this.addButtonEventListeners = function()
{
console.log("addButtonEventListeners SplashScreen" );
}
this.menuIn = function()
{
console.log("menuIn SplashScreen" );
stage.addChild(SplashScene);
var tween = createjs.Tween.get(SplashScene).to({y : 0, x : 0, alpha : 1}, 5000).call(this.menuInCompleted);
var splashTimer = window.setTimeout(function(){menuOut("MainScreen", false)},15000);
}
this.menuInCompleted = function()
{
console.log("menuInCompleted SplashScreen" );
this.addButtonEventListeners();
}
}
Can anybody tell me how can I do this?
Upvotes: 0
Views: 110
Reputation: 10342
Your problem is this
, which points to the current context (in runtime) and not to your object's one.
Just add a variable in SplashScreen like:
var self=this; //keeping the context of SplashScreen
And then call it as follows:
this.menuInCompleted = function()
{
console.log("menuInCompleted SplashScreen" );
self.addButtonEventListeners ();
}
Upvotes: 1
Reputation: 382102
The problem is that the context (this
) in the setTimeout
callback is window
, not your object.
You can change
var splashTimer = window.setTimeout(function(){menuOut("MainScreen", false)},15000);
to
var splashTimer = window.setTimeout(
(function(){menuOut("MainScreen", false)}).bind(this)
,15000);
and you also have to do the same where you bind menuIn
(to an event?).
Upvotes: 2