javascriptscopemovieclipadobe-animate

Animate CC (canvas) how to access variable inside e.currentTarget?


If I have a movieClip on the root timeline with the instance name of box. Inside the MC I have a colored square shape and the following code:

myNum = 5;

EDIT: In desparate act to get any response I have also on the line below:

var myNum = 3;

just to cover all my bases, but still no output response of either value END OF EDIT.

On the root timeline I have :

this.box.addEventListener("click", clickHandler);

function clickHandler(e) {
    console.log(e.currentTarget.myNum);
}

this returns undefined. How do I access myNum?


Solution

  • To access a variable inside of an object, the variable has to be a property (key) on the object. myNum isn't a property of e.currentTarget and just declaring a variable doesn't give any random object the property... you have to set the property on the object, like this:

    this.box.myNum = 3;
    this.box.addEventListener('click', clickHander);
    ...
    

    Although as Jose CC's answer mentions, putting custom attributes on elements is not standard (though it will work) and the way it should be done is with a data- attribute.

    var box = document.getElementById('box');
    box.dataset.myNum = 3;
    box.addEventListener('click', clickHandler);
    
    function clickHandler(e) {
      console.log(e.currentTarget.dataset.myNum);
    }
    <div id="box">My box</div>