Skip to main content
Inspiring
October 16, 2023
Answered

Saving a variable.

  • October 16, 2023
  • 2 replies
  • 470 views

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 has been closed for replies.
    Correct answer JoãoCésar17023019

    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

    2 replies

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    October 16, 2023

    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

    Inspiring
    October 16, 2023
    It's perfect!!!
    A very big thank you to you.
    JoãoCésar17023019
    Community Expert
    Community Expert
    October 16, 2023

    You're welcome!

    kglad
    Community Expert
    Community Expert
    October 16, 2023

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

    Inspiring
    October 16, 2023
    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….