2014-03-03 7 views
0

속성 및 함수를 특정 js 클래스로 선언하려고합니다. 객체 [개체 개체]있는 방법이 없습니다 'addButtonEventListeners자바 스크립트 클래스 함수 내에서 함수 호출?

내 코드를 나열 : SplashScene.js

나는 (선언 후라고하더라도) 함수 내에서 함수를 호출 할 때 그러나 그것은 예외를

catch되지 않은 형식 오류가 발생합니다

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(); 
} 
} 

아무도 말해 줄 수 있습니까?

답변

2

setTimeout 콜백의 컨텍스트 (this)는 개체가 아니라 window입니다.

당신은

var splashTimer = window.setTimeout(function(){menuOut("MainScreen", false)},15000); 

var splashTimer = window.setTimeout(
    (function(){menuOut("MainScreen", false)}).bind(this) 
,15000); 

을 변경할 수 있습니다 당신은 menuIn을 결합 어디도 동일한 작업을 수행해야합니다 (이벤트에?).

+0

메뉴가 작동하지만, 메뉴 밖으로되지 않습니다. 네가 한 말대로했다. 하지만 여전히 작동하지 않습니다. – starrystar

1

문제는 this입니다.이 문제는 런타임에 현재 컨텍스트를 가리키며 객체의 컨텍스트가 아닙니다. 는 것처럼되는 SplashScreen에 변수를 추가 :

var self=this; //keeping the context of SplashScreen 

그리고 다음과 같이 호출 : 트윈에서

this.menuInCompleted = function() 
{ 
    console.log("menuInCompleted SplashScreen"); 
    self.addButtonEventListeners(); 
} 
+0

ıt worked :) 정말 고마워요 :) – starrystar

관련 문제