2

I'm trying to access instance variables from within a nested object ('action'). The only workaround I could find was using a local var ('_') to represent the parent object.

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]()

1 Answer 1

4

Try using a fat arrow (=>) inside your action object like so:

wave: =>
    @x = @ox  
    @y = @oy = Math.cos(window.G.time * @speed + @c + @vary) * @amp - @amp * .5
    return
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.