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

CC Animate HTML5 Canvas - Bring Button to Front on Click

New Here ,
Sep 25, 2019 Sep 25, 2019

Copy link to clipboard

Copied

I have two buttons on a canvas. They are slightly overlapping. When the user clicks on one button, I would like that button to be brought to the top (or in front) of the other button. 

 

So, a click on a button should always bring it to the top so the whole button is visible to the user. 

 

Thanks for any any help on this!

Views

1.9K

Translate

Translate

Report

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
Community Expert ,
Sep 26, 2019 Sep 26, 2019

Copy link to clipboard

Copied

Hi.

 

You can use the addChild or setChildIndex method from the createjs.Container class. In this case, you're going to call one of these two methods from the buttons parent.

 

Examples:

 

addChild approach (ES5 syntax):

this.bringToFront = function(e)
{
    e.currentTarget.parent.addChild(e.currentTarget);
};

this.button0.on("click", this.bringToFront);
this.button1.on("click", this.bringToFront);

 

addChild approach (ES6 syntax):

this.bringToFront = e => e.currentTarget.parent.addChild(e.currentTarget);
this.button0.on("click", this.bringToFront);
this.button1.on("click", this.bringToFront);

 

setChildIndex approach (ES5 syntax):

this.bringToFront = function(e)
{
    e.currentTarget.parent.setChildIndex(e.currentTarget, e.currentTarget.parent.numChildren - 1);
};

this.button0.on("click", this.bringToFront);
this.button1.on("click", this.bringToFront);

 

setChildIndex approach (ES6 syntax):

this.bringToFront = e => e.currentTarget.parent.setChildIndex(e.currentTarget, e.currentTarget.parent.numChildren - 1);
this.button0.on("click", this.bringToFront);
this.button1.on("click", this.bringToFront);

 

I hope this helps.

 

Regards,

JC

Votes

Translate

Translate

Report

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
New Here ,
Sep 27, 2019 Sep 27, 2019

Copy link to clipboard

Copied

Thank you so much!! I will try it out, and report back 🙂

Votes

Translate

Translate

Report

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
New Here ,
Oct 15, 2019 Oct 15, 2019

Copy link to clipboard

Copied

Just what I needed! I used the event "mousedown" instead of "click" to instantly move to front on a mouse click. Thanks again!

Votes

Translate

Translate

Report

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
Community Beginner ,
Nov 17, 2021 Nov 17, 2021

Copy link to clipboard

Copied

LATEST

Hello JC and ClayUUID (this is not bumping an old thread) 🙂

JC, your code is great but for a whole day I could not get it to work. I tried so many ways but all to no avail.

Just one line of code was missing as far as I was concerned:

stop() the parent movieclip first!

This came from ClayUUID on another thread.

(https://community.adobe.com/t5/animate-discussions/unable-to-setchildindex-of-placed-movieclip-in-ca...)

I have made an HTML5 canvas fla and all it has is a single movieclip ("base") and two buttons ("b1" and "b2").

I have placed the buttons inside the "base".

Here is my code (in frame 1 of the stage timeline) for those who stumble upon this page.

this.stop();
root = this;
root.bringToFront = function(e)
{
      e.currentTarget.parent.setChildIndex( e.currentTarget, e.currentTarget.parent.numChildren-1);
};
root.base.stop();
root.base.b1.on("click", this.bringToFront);
root.base.b2.on("click", this.bringToFront);

 

Not sure you even need to stop the root, just the parent movieclip.

Thanks JC and ClayUUID

Ron

Votes

Translate

Translate

Report

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