0

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?

asked Mar 3, 2014 at 13:39

2 Answers 2

2

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?).

answered Mar 3, 2014 at 13:42
Sign up to request clarification or add additional context in comments.

1 Comment

the menu in tween is working but menu out not. I did what you said. But it is still not working.
1

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 ();
}
answered Mar 3, 2014 at 13:47

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.