当我运行我的代码时,它给了我这个错误:
类型错误:枢轴未定义
这是错误所指的相关类:
class Hero
constructor: (@color, @direction, @x, @y) ->
pivots: []
addPivot: (pivot) -> @pivots.push(pivot)
onPivot: (pivot) -> pivot.x == @x && pivot.y == @y
applyPivots: () ->
indexToRemove = -1
for i in [0..@pivots.length - 1]
pivot = @pivots[i]
if (@onPivot(pivot))
@direction = pivot.direction
indexToRemove = i
@pivots.splice(indexToRemove, 1)
为什么会这样?这是它生成的javascript:
Hero = (function() {
function Hero(color, direction, x, y) {
this.color = color;
this.direction = direction;
this.x = x;
this.y = y;
}
Hero.prototype.pivots = [];
Hero.prototype.addPivot = function(pivot) {
return this.pivots.push(pivot);
};
Hero.prototype.onPivot = function(pivot) {
return pivot.x === this.x && pivot.y === this.y;
};
Hero.prototype.applyPivots = function() {
var i, indexToRemove, pivot, _i, _ref;
indexToRemove = -1;
for (i = _i = 0, _ref = this.pivots.length - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) {
pivot = this.pivots[i];
if (this.onPivot(pivot)) {
this.direction = pivot.direction;
indexToRemove = i;
}
}
return this.pivots.splice(indexToRemove, 1);
};
return Hero;
})();
错误在说:
return pivot.x === this.x && pivot.y === this.y;