Skip to main content
Inspiring
Oct 16, 2023 4:31 PM UTC
Answered

Saving a variable.

Hello, newbie, I have a problem that I can't solve. I have 4 movieClips that I can move using the mouse: clip1, clip 2.... When clicking on a clip, I want to save a myVar variable which gives me a numerical value of the clip I click on and based on its position in the recip variable. For example, clicking on clip0, myVar = 0, clicking on clip2, the myVar = 2... Many thanks to anyone who can help me.

 

Var myVar ;
var recip = [this.clip0, this.clip1, this.clip2, this.clip3]
var objet = [];
for (var i = 0; i < recip.length; i++) {
	objet[i] = [recip[i].x, recip[i].y];
	recip[i].addEventListener("mousedown", startMouvRecip.bind(app))
	recip[i].addEventListener("pressmove", mouvRecip.bind(app));
	recip[i].addEventListener("pressup", stopmouvRecip.bind(app));
};

 

    This topic is closed to new replies. Start a new post to keep the conversation going.
    JoãoCésar17023019
    JoãoCésar17023019Correct answer
    Community Expert
    Oct 16, 2023 6:04 PM UTC

    Hi.

     

    This should work:

    var app = this;
    var myVar;
    var recip = [ this.clip0, this.clip1, this.clip2, this.clip3 ];
    // ...
    
    function onClickRecipe(e)
    {
    	myVar = parseInt(e.currentTarget.name.replace("clip", ""));
    	console.log(myVar);
    }
    
    for (var i = 0; i < recip.length; i++)
    {
    	// ...
    	recip[i].addEventListener("click", onClickRecipe.bind(app));
    }

     

    Regards,

    JC

    5 replies

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    Oct 16, 2023 6:04 PM UTC

    Hi.

     

    This should work:

    var app = this;
    var myVar;
    var recip = [ this.clip0, this.clip1, this.clip2, this.clip3 ];
    // ...
    
    function onClickRecipe(e)
    {
    	myVar = parseInt(e.currentTarget.name.replace("clip", ""));
    	console.log(myVar);
    }
    
    for (var i = 0; i < recip.length; i++)
    {
    	// ...
    	recip[i].addEventListener("click", onClickRecipe.bind(app));
    }

     

    Regards,

    JC

    Translate
    Inspiring
    Oct 16, 2023 6:15 PM UTC
    It's perfect!!!
    A very big thank you to you.
    Translate
    JoãoCésar17023019
    Community Expert
    Community Expert
    Oct 16, 2023 6:28 PM UTC

    You're welcome!

    Translate
    kglad
    Community Expert
    Community Expert
    Oct 16, 2023 4:41 PM UTC

    what problem are you having defining your variable (probably in your listener function)?

    Translate
    Inspiring
    Oct 16, 2023 5:01 PM UTC
    Actually I want to save in myVar the variable i when I click on a clip. For example i = 0 for clip0 clicked, i = 1 for clip1 clicked….
    Translate