我正在尝试从嵌套对象('action')中访问实例变量。我能找到的唯一解决方法是使用局部var('_')来表示父对象。
Class Mover
...
Constructor () =>
_ = this
@mode = "wave"
@action= {
wave: ->
_.x = _.ox
_.y = _.oy = Math.cos(window.G.time * _.speed + _.c + _.vary) * _.amp - _.amp * .5
return
jump: ->
_.y = _.oy = Math.min(0,Math.cos(window.G.time*_.speed + _.c * _.vary)) * _.amp - _.amp * .5
_.x = _.ox
return
@loop =>
@action[@mode]()
答案 0 :(得分:4)
尝试在动作对象中使用胖箭头(=>),如下所示:
wave: =>
@x = @ox
@y = @oy = Math.cos(window.G.time * @speed + @c + @vary) * @amp - @amp * .5
return