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

Bring Object to Front using Actionscript

New Here ,
Jun 17, 2024 Jun 17, 2024

UnknownTiara_0-1718682699767.pngexpand image

So I'm making a Burger Builder game, where the user can build their own burger by dragging and dropping the burger parts, essentially stacking them. However, I have a problem with the layers because if the user wants to have the patty as the first layer in the stack, and they want lettuce as the second layer, but then the lettuce is stacked behind the patty (if you get what I mean).

 

When I use the "Bring Object to Front" code snippet, when I go to the next scene, it looks like this, like the objects I clicked are showing up in the next scene, and layers that are hidden shows up.

UnknownTiara_2-1718683191869.pngexpand image

UnknownTiara_1-1718683069400.pngexpand image

 

Is there a way to fix this? Because the code snippet makes it that everything in the scene is clickable, so how do I make it that only certain objects (the burger parts)  are clickable to bring to the front and leave everyting else be?

 

I'm very new to this,  so if you can, I'd like to be explained like a 5 years old "monkey see monkey do".

TOPICS
ActionScript , Code , How to
562
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

Community Expert , Jun 18, 2024 Jun 18, 2024

did you call move_to_top()?

eg, using bun click

 

 

bun.addEventListener(MouseEvent.CLICK,, onBunClick);

 

function onBunClick(e:MouseEvent):void{

move_to_top(MovieClip(e.currentTarget));

}

 

function move_to_top(mc:MovieClip):void{
mc.parent.addChild(mc);

}

Translate
Community Beginner ,
Jun 17, 2024 Jun 17, 2024

you can use setChildIndex to set the depths of the movieclips

 

function onLettuceClick(event:MouseEvent):void {
// Bring the lettuce the front
setChildIndex(lettuce , numChildren - 1);
}

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
New Here ,
Jun 18, 2024 Jun 18, 2024

UnknownTiara_0-1718696012695.pngexpand image

like this? because it didn't work for me

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
Community Expert ,
Jun 18, 2024 Jun 18, 2024

nurba's code is incorrect.  and it's easier to use addChild:

 

function move_to_top(mc:MovieClip):void{
mc.parent.addChild(mc);

}

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
Community Beginner ,
Jun 18, 2024 Jun 18, 2024

we did not see the whole code, maybe UnknownTiara  is using already added movieclips?

if movieclips already there, then setChildIndex should work fine,

on your code addChild adds movieclips, new movieclip automatically will be at the top

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
New Here ,
Jun 18, 2024 Jun 18, 2024

all the burger parts are modified to movie clips! 

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
New Here ,
Jun 18, 2024 Jun 18, 2024

ooh so just copy this code word for word or what do i change?

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
Community Beginner ,
Jun 18, 2024 Jun 18, 2024

can you share your code?

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
Community Expert ,
Jun 18, 2024 Jun 18, 2024

did you call move_to_top()?

eg, using bun click

 

 

bun.addEventListener(MouseEvent.CLICK,, onBunClick);

 

function onBunClick(e:MouseEvent):void{

move_to_top(MovieClip(e.currentTarget));

}

 

function move_to_top(mc:MovieClip):void{
mc.parent.addChild(mc);

}

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
New Here ,
Jun 19, 2024 Jun 19, 2024

It worked! Thank you so much, you're seriously a life saver ❤️

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
Community Expert ,
Jun 19, 2024 Jun 19, 2024
LATEST

you're welcome.

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
Community Expert ,
Jun 18, 2024 Jun 18, 2024

Hi.

 

Are the burger parts inside of the same parent? And are there any other instances in this same parent?

 

I think the codes suggested here work but you need to make sure youl're adding the click listener to the correct container or children and that these parts live in the same parent so that they don't go beyond the UI for example or don't appear in another screen of your game.

 

Regards,

JC

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