1
我想在学习Backbone的同时构建一个简单的2D游戏,而且我在继承的工作方面遇到困难。为什么不是EntityModel中的默认值?从蜱()的输出始终是:Backbone中的继承模型上未设置默认属性
x: undefined
y: undefined
简化代码:
$(function(){
EntityModel = Backbone.Model.extend({
defaults: function(){
return {
x : 0,
y : 0
};
},
tick: function(){
console.log('x: ' + this.get('x'));
console.log('y: ' + this.get('y'));
}
});
PlayerModel = EntityModel.extend({
defaults: function() {
return {
name : 'John Doe',
health : 10
};
},
initialize: function(options){
console.log('New player ('+this.get('name')+') entered the game');
}
});
var player1 = new PlayerModel();
var gameloop = window.setInterval(function(){
player1.tick();
}, 40);
});
好极了!非常感谢你 :) – o01