对象的属性未定义,尽管已指定值
Attribute of an object is undefined despite being assigned value
所以我正在构建一个小游戏,其中一个角色就是这个Golem对象:
function Golem(){
var self = this;
this.width = 470;
this.height = 360;
this.drawX = canvasEntities.width/3;
this.speed = 30;
this.isLeftKey = false;
this.isRightKey = false;
this.isSpacebar = false;
this.spritesheet = spritesheetgolemleft;
this.animate = function(){
requestAnimFrame(self.animate);
//this console log returns undefined
console.log(self.spritesheet)
this.spritesheet.update();
self.spritesheet.draw(self.drawX,canvasEntities.height/3);
}
}
spritesheetgolemleft变量已经在最顶部(全局)定义:
var spritesheetgolemleft = new SpriteSheet('images/golem_walkleft.png',470,360,3,6);
这是SpriteSheet类:
function SpriteSheet(path, frameWidth, frameHeight, frameSpeed, endFrame){
var image = new Image();
var framesPerRow,
currentFrame = 0,
counter = 0;
//# of frames after image loads
var self = this;
image.onload = function(){
framesPerRow = Math.floor(image.width/frameWidth);
};
image.src = path;
this.update = function(){
if(counter == (frameSpeed - 1))
currentFrame = (currentFrame + 1) % endFrame;
counter = (counter + 1) % frameSpeed;
}
this.draw = function(x,y){
var row = Math.floor(currentFrame / framesPerRow);
var col = Math.floor(currentFrame % framesPerRow);
//draw image into the Entities canvas
ctxEntities.drawImage(
image,
col * frameWidth, row*frameHeight,
frameWidth, frameHeight,
x,y,
frameWidth, frameHeight);
};
};
我得到的错误发生在Golem()对象的倒数第二行:
this.spritesheet.update();
它给了我一个TypeError,无法读取未定义的属性"update"。我认为这是范围的某种问题,在顶部添加了self-this hack,但它仍然不起作用。我做错了什么?提前谢谢。
事实证明,在创建精灵表之前,我正在实例化一个对象。我的错误:/
相关文章:
- 主干中的模型属性未定义
- JavaScript 2d 数组导致“无法读取未定义的属性'未定义'”
- JSON对象属性未定义
- Waypoint的循环,退出循环后属性未定义
- Javascript:错误“无法设置属性”..'未定义”
- 翡翠 - 显示对象属性 - 未定义
- 属性未定义,但控制台.log可以看到它
- JavaScript 对象的属性未定义
- Javascript 对象属性未定义(但属性已设置)
- DOM 元素属性未定义 - jquery / ruby on rails.
- Nodejs JSON 属性未定义
- 错误:窗口打开器的属性未定义
- angular Js:Array.lenght属性未定义
- 正在尝试访问JavaScript对象的属性=未定义
- Marionette属性未定义,请从API获取
- AngularJS作用域属性未定义
- 对象的属性未定义,尽管已指定值
- 为什么我一直收到一个错误,说对象属性未定义
- 对象属性未定义,但我看到它不是
- .map()img src属性未定义