0

Found this topic: AS3 setChildIndex to front I'm trying to accomplish the exact opposite.

"addChildAt" doesn't work for me when I'm setting the container behind everything else or something along the lines. And as for stars themselves, I can't send them back anymore than layer 0 for some odd reason (it'll give me error 2006, stating "The supplied index is out of bounds"). Here's the code:

starsSpawn function:

var starContainer:MovieClip = new MovieClip();
addChildAt(starContainer, 20);

starContainer code:

function starsSpawn()
{
    for(var i:int= 0; i < 30; i++)
    {
        var newStar = new starCode();
        var scaleXY = Math.random()*(2)+0.1;
        newStar.width = scaleXY;
        newStar.height = scaleXY;
        var positionX:Number = Math.random()*(stage.stageWidth + (1* newStar.width));
        var randomY:Number = Math.random()*(stage.stageHeight - newStar.height);
        newStar.x = positionX;
        newStar.y = randomY;
        starContainer.addChild(newStar);
    }
}

Essentially how it works is that a container is set up and the for loop creates 30 stars, each with the outlined code.

Community
  • 1
  • 1
Cray
  • 43
  • 8
  • *"can't send them back anymore than layer 0 for some odd reason"* That's not "some odd reason", that's how AddChildAt works. Reading the documentation on AddChildAt should clear some things up. – Brian Dec 12 '16 at 17:36
  • Documentation from where? Searching it on google doesn't give me anything for addChildAt, just for addChild and that's not very helpful for me in this scenario. – Cray Dec 12 '16 at 18:49
  • Odd, a search for "as3 addChildAt" gave me [this](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html#addChildAt()) as the first result. What were you searching for? – Brian Dec 12 '16 at 18:55

1 Answers1

0

When you addChildAt(myChildMC, 0); it gets added at index 0 and everything else gets bumped up. Z order indexes in AS3 are contiguous, meaning every space from zero to the highest z order must be filled. If I remove whatever is on layer 4, layer 5 will slide down to fill, and so on

And I don't think you can add something to index 20 unless all the other indexes have children in them. That may be what is causing your particular error. To add something to the top level just do addChild(myChild).

edit

Since it sounds like you have the concept of how z-order works in reverse in your mind, you probably are wanting to have the stars "in front" of everything else i.e. Always on top i.e. Always visible. To do that just do addChild(starsContainer) after all the other containers are added or Sprites or MovieClips. If you add something to the stage at runtime, just add the stars container again (this won't create a second container, it literally just changes the z-order if there is already a container by that name... this is an often misunderstood point).

Neal Davis
  • 2,010
  • 2
  • 12
  • 25
  • That doesn't answer my question though; how do I get the stars behind everything else in the game? Setting individual stars to 0 won't do it and I can't set it to any other number than 0, so where do I go from here, how do I get it behind everything? Take everything else and put it in front of stars? That would be a bloody nightmare, there's GOT to be an easier way and there's got to be an easy explenation for why this specific type of instance doesn't work like others do. I have enemies set to layer 3, player ship projectile container to 1 and weapon container at 2. – Cray Dec 11 '16 at 19:53
  • And the aforementioned is it; there is nothing else I am using "AddChildAt" and defining a layer or a Z position for, if you will. Changing the values so that I could place stars at the very back (set them to layer 3) gives me the same previous error. So how the bloody hell do I get the stars to be behind everything and I mean everything? – Cray Dec 11 '16 at 19:55
  • Just do `addChildAt(starContainer, 0);` – Neal Davis Dec 11 '16 at 19:55
  • @XirmiX layer 3 is not the very back. 0 is the very back. Maybe I misunderstood but you say "*changing values so that I could place the stars at the very back (set them to layer 3)*"... – Neal Davis Dec 11 '16 at 19:57
  • Ugh, it's really confusing which way is the back layering and which way is the front layering when you get used to something like GML in Game Maker Studio... Anyway, I tried to set it to 0 and it only stays behind a few things, there are still things in front of stars for some reason. Any idea what's with this? – Cray Dec 11 '16 at 22:20
  • 1
    What? "*it only stays behind a few things, there are still things in front of stars for some reason*"... this confuses me. This makes no sense. It's like me saying "I put this pancake on the bottom of the stack but there are still pancakes above it". Of course there are still things in front of your stars, the zero level is the very back. EVERYTHING will be in front of the stars if the stars are at zero. Do you want the stars to be on the top of the stack of pancakes so nothing is covering the stars? Because that is the exact opposite of what you have been asking for. – Neal Davis Dec 11 '16 at 23:10
  • If you want the stars to have the highest z order (be in top of and in front of everything) this will make the stars cover what is under or behind. In hat case do the opposite of what we did. Instead of adding to the bottom of the stack, just do `addChild(starContainer);`. That's it. Done. – Neal Davis Dec 11 '16 at 23:33
  • But if you add anything after that it will cover the stars. So either add the starsContainer last. If you ever add anything else, either add it below the stars using addChildAt, or simply re add the starsContainer again. – Neal Davis Dec 11 '16 at 23:35
  • To explain z order another way the higher the z order, the closer to the viewer/user it is. Zero order is farther away into the computer screen. So if you want the stars covering up stuff behind it/ beneath it/ further away, then just do addChild and it will be on top of everything. You seem to just have "in front of" and "behind" backwards. – Neal Davis Dec 12 '16 at 01:41
  • @XirmiX Why doesn't AddChildAt(0) work? What happens when you do that? – Brian Dec 12 '16 at 17:34
  • That puts it underneath all the other pancakes. All your other elements will be covering the stars. Is that what you want? @Brian it seems to me like he is using the verbiage of "in front of" and "behind" backwards or interchangeably. – Neal Davis Dec 12 '16 at 17:37
  • @NealDavis LOL at pancakes. The fault lies not in our stars, but in our pancakes. What I was looking for was clarification from the OP on why addChildAt(0) didn't solve their problem. The question makes it sound like they want these star movieclips to go behind everything else, which addChildAt(0) does. Something is missing here. – Brian Dec 12 '16 at 17:43
  • 1
    @Brian, right but then he also says that when he does that "there are still things in front of the stars" Doh! If the stars go to the back EVERYTHING will be in front of the stars. That's why I think he's confused about the usage of these terms. He thinks he wants to send them back but he really wants to make them come to the front, I think. Which is why a simple addChild() should work, he just needs to make sure that it is the last thing added, and if he adds something else after the stars, add the stars again. I could be wrong about what he wants but this is my guess. – Neal Davis Dec 12 '16 at 17:55
  • @Brian by the way addChildAt requires two parameters. You probably know that but the way you wrote it could confuse someone. – Neal Davis Dec 12 '16 at 18:21
  • @Neal good catch; I was being sloppy. I should have written `addChildAt(pancake, 0);` – Brian Dec 12 '16 at 18:44
  • Yes, weird, I'm thinking one thing, but I write the other. Sorry for the confusion. I want everything to be in front of stars, the stars being behind everything. "addChildAt()" doesn't move them behind everything though. The only things that they stay behind are the main menu stuff and options menu, everything else is behind the stars for some weird reason. Possibly because the stars are added at the very beginning. So, since the current method doesn't send the stars behind everything (addChildAt) is there anything else I can do or any other structure I can put to have stars behind everything? – Cray Dec 12 '16 at 18:45
  • Ok. Let's just be abundantly clear... you want the stars to get covered up and obscured by all other things so the user can't see them if something else occupies the same x/y coordinates. Is that correct? – Neal Davis Dec 12 '16 at 19:16
  • Give us one specific example of something that is still behind the stars after doing `addChildAt(starContainer, 0)` – Neal Davis Dec 12 '16 at 19:18
  • 1
    I suspect that `starContainer` is getting added to another container, not directly to the stage. If that's the case, it could be at index 0 (within its container), but still have things behind it if there are objects on the stage behind the `starContainer` parent object. Can you verify if that's the case XirmiX? – Cadin Dec 13 '16 at 00:18