我正在尝试将我用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" });
},
答案 0 :(得分:0)
要做到这一点:
this.enemies = [];
也许你应该尝试使用
this.set("enemies", [])
和
this.circle = this.paper.circle(100, 50, 30);
到
this.set("circle", this.get("paper").circle(100, 50, 30))