【发布时间】:2016-02-01 09:00:11
【问题描述】:
我对如何使用、定义、更改和取消设置 javascript 对象感到困惑。
我的 javascript 对象是:
var Ship = function(){
return {
canvas: $('#area')[0],
canvas_width: this.canvas.width,
canvas_height: this.canvas.height,
context: this.canvas.getContext("2d"),
ship_image: new Image(),
ship_width: null,
ship_height: null,
ship_x: 0,
ship_y: 0,
init: function() {
this.ship_image.onload = function() {
this.ship_width = this.width;
this.ship_height = this.height;
this.ship_x = (this.canvas_width / 2) - (this.ship_width / 2);
this.ship_y = this.canvas_height - this.height;
this.draw(
this.ship_x,
this.ship_y
);
}
this.ship_image.src = "ship.gif";
},
draw: function(x, y) {
this.context.drawImage(
this.ship_image,
x,
y
);
}
}
}();
当我像这样执行这段代码时;
$(function(){
Ship.init();
Controller.init();
});
我每次都会收到这个错误。
Uncaught TypeError: Cannot read property 'width' of undefined on line 4 (ship.js)
Uncaught TypeError: Ship 不是第 29 行的函数 (index.html / Ship.init())
我现在该怎么办?
【问题讨论】:
标签: javascript jquery canvas javascript-objects