【发布时间】:2013-11-12 21:01:44
【问题描述】:
有人可以向我解释为什么我会得到:
Uncaught TypeError: Cannot read property 'canvas' of undefined game.js:48
Uncaught TypeError: Cannot call method 'resize' of undefined game.js:4
似乎由于某种原因 this.stage 超出了 start 函数的范围。
//index.js
var game;
function init()
{
game = new Game(document.getElementById('canvas'));
}
function resize()
{
game.resize();
}
_
//game.js
function Game(canvas)
{
this.canvas = canvas;
this.stage = null;
this.loader = null;
this.scaleCanvas();
this.initCreateJS();
this.loadAssets();
}
Game.prototype =
{
resize: function()
{
this.scaleCanvas(this.canvas);
this.level.resize(this.canvas.width, this.canvas.height);
this.stage.update();
},
scaleCanvas: function()
{
this.canvas.width = window.innerWidth;
this.canvas.height = window.innerHeight;
},
initCreateJS: function()
{
this.stage = new createjs.Stage(this.canvas);
createjs.Ticker.setFPS(30);
this.stage.enableMouseOver(10);
if(createjs.Touch.isSupported())
{
createjs.Touch.enable(this.stage);
}
},
loadAssets: function()
{
manifest = [
{src:"base.png", id:"base"},
{src:"logo.png", id:"logo"}
];
this.loader = new Loader(manifest, this.start);
},
start: function()
{
this.level = new Level(4,4,this.stage.canvas.width,game.stage.canvas.height);
this.level.randomLevel();
this.level.print();
game.stage.addChild(this.level);
game.stage.update();
}
};
【问题讨论】:
-
你是如何实例化你的对象的?
-
客户端代码是什么(即
new Game())? -
start方法在哪里调用?
-
好像这是在函数的范围内,试试Game.canvas,而不是this.canvas等等。
-
此外,我不太确定我是否理解为什么在
start的结尾行中使用game.stage而不是this.stage。
标签: javascript createjs