我正在尝试将我用 raphael.js 制作的这个简单项目转换为使用backbone.js,但我一直遇到同样的错误,我不知道为什么。在我的模型中的函数中,我尝试使用“this”访问变量,但我仍然收到此错误“无法读取未定义的属性“长度””,例如一个数组。我的模型看起来像这样:
GameModel = Backbone.Model.extend({
defaults: {
spawnId: '',
gameloopId: '',
checkId: '',
goingUp: false,
counter: 0,
recentscores: [],
enemies: '',
speed: -4,
score: 0,
health: 100,
paper: '',
t: '',
h: '',
c: '',
circle: ''
},
initialize: function(){
this.enemies = [];
this.paper = new Raphael($('#canvas'), 0, 0);
this.circle = this.paper.circle(100, 50, 30);
this.t = this.paper.text(1000, 50, "Score: " + this.score);
this.h = this.paper.text(50, 50, "Health: " + this.health);
this.c = this.paper.image("background.png", 0, 0, 2400, 800);
this.paper.setSize(1200,800);
this.circle.attr({fill: '#9cf', stroke: '#ddd', 'stroke-width': 5});
this.t.attr({ "font-size": 20, "font-family": "Arial, Helvetica, sans-serif" });
this.h.attr({ "font-size": 20, "font-family": "Arial, Helvetica, sans-serif" });
},