Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to find the name of a movieclip

Contributor ,
Mar 25, 2017 Mar 25, 2017

Hi All,

I'm new to Animate CC and I'm using it to build an HTML5<canvas> end product. (So, I think I'm looking for code that works with EasleJS).

I want to find the name of the movie clip clicked on by the user. Here's the relevant part of my main timeline script:

var stage = this;

stage.MovieClipInstance.addEventListener("click", findNameOfMovieclipClickedOn.bind(this)); //event listener

function findNameOfMovieclipClickedOn () {

document.getElementById("testOutput").innerHTML = "you clicked on:: "+ theName;

}

"this.name" just returns "null"

Also:

  • Is there another forum where I could ask questions specific to using Animate CC for HTML5 <canvas> projects...or is this the best place for that?
  • As I build this project, should I just be looking for syntax/code that pertains to EasleJS?

Thanks much!

2.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Mar 26, 2017 Mar 26, 2017

The click comes with an event. If you trap the event you can find out what was clicked. Like:

this.b1.name = "b1";

this.b2.name = "b2";

this.b1.addEventListener("click", wasclicked);

this.b2.addEventListener("click", wasclicked);

function wasclicked(e) {

  alert(e.currentTarget.name);

}

Translate
LEGEND ,
Mar 25, 2017 Mar 25, 2017

MovieClips don't have a name property in CreateJS. You can work around that by giving it a name property. Like:

alert(this.box.name); //shows 'null'

alert(this["box"].name); //shows 'null'

this["box"].name = "box";

alert(this["box"].name);  //now shows 'box'

alert(this.box.name);  //now shows 'box'

After that your listeners should be able to get the name property you set.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 26, 2017 Mar 26, 2017

Thanks Colin,

I'm now able to name the MovieClipInstance using:

stage.MovieClipInstance.name = "myInstanceName";

...and then I can see that assigned name by doing:

document.getElementById("testOutput").innerHTML = "InstanceName: "+ stage.MovieClipInstance.name ;

but I want to get that name from within the function where I process the click (if I don't know which instance was clicked on). But maybe I'm not going about this correctly. Here's my intent:

I have 12 buttons that will be clicked on. Each button will show a different dashboard when it's clicked on.

I was thinking that, Instead of creating 11 different functions to show the dashboard associated with each button, I'd just make one function and, inside that function, determine which button was clicked on and find the correct dashboard to show.

Am I going about this the wrong way?

Thanks

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 26, 2017 Mar 26, 2017

The click comes with an event. If you trap the event you can find out what was clicked. Like:

this.b1.name = "b1";

this.b2.name = "b2";

this.b1.addEventListener("click", wasclicked);

this.b2.addEventListener("click", wasclicked);

function wasclicked(e) {

  alert(e.currentTarget.name);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 26, 2017 Mar 26, 2017

BTW, in my example I had two movieclips on stage where I had used Properties to set the instance names to b1 and b2.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 27, 2017 Mar 27, 2017
LATEST

That's exactly what I need.

Thanks much, Colin!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 25, 2017 Mar 25, 2017

First, your click handler code is completely wrong. Canvas mode draws everything on a single HTML canvas element. It does not create child HTML elements. You should just be referencing this in the event handler.

Second, Re: HTML Canvas instance names - Good tricks to getting them?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 26, 2017 Mar 26, 2017

Thanks ClayUUID,

you wrote:

First, your click handler code is completely wrong.

hmm. I don't understand. The event listener..

var stage = this;

stage.MovieClipInstance.addEventListener("click", findNameOfMovieclipClickedOn.bind(this)); //event listener

..seems to work because it calls the findNameOfMovieclipClickedOn function. I can see the test output text sent to the browser by that function.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 26, 2017 Mar 26, 2017

paul_james123  wrote

Thanks ClayUUID,

you wrote:

First, your click handler code is completely wrong.

hmm. I don't understand. The event listener..

I said the click handler, not the event listener. The "document.getElementById("testOutput").innerHTML" stuff, that is all completely inapplicable to Canvas objects.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 27, 2017 Mar 27, 2017

ClayUUID wrote:

I said the click handler, not the event listener. The "document.getElementById("testOutput").innerHTML" stuff, that is all completely inapplicable to Canvas objects.

Ok. Thanks. That's just for testing purposes. It outputs text (the name of the movie clip) to the html wrapper that contains the canvas.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines