Add child alternative?


(Adam Edney) #1

Is there something in FP that I can add a child and also choose the layer? I’m using a GUI frame work which I need to use addchild() to make it render but the only problem is that I cannot choose the layer. Any ideas ?

Thanks, Adam


(Martí Angelats i Ribera) #2

You’ll have to make your own Entity. Have a look how Graphiclist works, it should help you.


(Adam Edney) #3

Thanks for the reply, im not too sure that what im looking for. Are entities a separate child or are they handled differently? Ideally I would like to change the entity child index if possible but I assume that would break something. Or alternatively change FP child index e.g. make FP 1, the background 0 and have any pop ups 2. Would that be feasible?


(Adam Edney) #4

Ps this frame work is a third party so it’s not feasible to turn it into an entity (I’m pretty sure )


(Martí Angelats i Ribera) #5

I don’t know your framework but if you want to use a parent that contains multiple use this:

package net.flashpunk
{
	net.flashpunk.Entity;

	public class Entitylist extend Entity
	{
		public function Entitylist()
		{
			super();
		}

		private var _entities:Vector.<Entity> = new Vector.<Entity>;
		public function add(e:Entity):void
		{
			if (_entities.indexOf(e) < 0)
			{
				if (_world)
				{
					e._world = _world;
					e.added();
				}
				_entities.push(e);
			}
		}
		public function remove(e:Entity):Boolean
		{
			if (_entities.indexOf(e) >= 0)
			{
				e.removed();
				_entities.splice(_entities.indexOf(e), 1);
				return true;
			}
			else retrun false;
		}

		override public function added():void
		{
			for each (e:Entity in _entities)
			{
				e._world = _world;
				e.added();
			}
		}
		override public function removed():void
		{
			for each (e:Entity in _entities) e.removed();
		}
		override public function update():void
		{
			for each (e:Entity in _entities) e.update();
		}
		override public function render():void
		{
			for each (e:Entity in _entities) e.render();
		}
	}
}

The file must be in the src/net/flashpunk/Entitylist.as.

Then you want to make a subclass wich sets the type and layer in it’s constructor (right after the super()). Only add the parent to the world (not any of the childs) and it should work.

This code does not support collides and hadn’t been tested. But you get the idea.


(Zachary Lewis) #6

You can still use addChild(), but you need to get a reference to the stage. You should be able to add display objects the traditional way.

FP.stage.addChild(myDisplayObject);

(Adam Edney) #7

But can I add the child so it’s behind the FP render? Because when I try it’s always on top.


(Martí Angelats i Ribera) #8

Use FP.stage.addChildAt(child, index). The highest index is the top one.

Knowing this you want to use:

FP.stage.addChildAt(myDisplayObject, 0);

This way you add it into the back of the stage.

Here you have the documentation about Stage.


(Adam Edney) #9

Thanks for the link but I cannot seem to find the render child.

This is my code,

FP.stage.addChildAt(bn_go,1);

FP.stage.addChildAt(bn_pass,2);

if I set the index to be 0 then it doesn’t add the child / doesn’t update/ doesn’t render.

The bn_pass and bn_go layer is higher than the FP, so it’s on top of everything

I tired this…

FP.stage.swapChildrenAt(1, 0); (it makes bn_go disappear and if I then do FP.stage.swapChildrenAt(0, 1); then it reappear).

if I do this…

trace(FP.stage.getChildAt(0)); returns [object Main]

I tried getting FP.stage.parent but than is null.

Any idea on how to get the renders child index so I can change the index


(Martí Angelats i Ribera) #10

try this in the Main.as

override public function init():void
{
	//create the buttons
	
	FP.stage.setChildIndex(this, 2);
	FP.stage.addChildAt(button_1, 0);
	FP.stage.addChildAt(button_2, 1);
}

setChildIndex changes the index of a child, in this case itself. Then we add the buttons.

PS: There can only be a single child per index.


(Adam Edney) #11

Thanks i’m making progress, I think FP child is covering the whole window, because if I get pass_button behind the FP stage index then I cannot see it or click it.

my code.

override public function init():void
{	
create_pass_button(); // index 0
create_go_button();  // index 2
FP.stage.setChildIndex(this, 1);
}

I can click and see go button but I cannot see / press pass button.

I’m not too sure if there is a workaround.


(Martí Angelats i Ribera) #12

Use the setChildIndex BEFORE adding the buttons to the stage (you can still create them, but you have to have the right index when using set child at. You are adding to 0 which is the index of the current main).

Actually i don’t recomend to use something like create_pass_button or create_go_button instead you want to use it directly or have everything in a single function.

Another thing to keep in mind is that in AS3 the functions are usually named without _ but starting the next word with capital letter (like setChildAt). FP uses this and mixing them may make the code less readable.


(Adam Edney) #13

Sorry I must be missing something, my code (I made it more readable :slight_smile: )

        override public function init():void
	{
         var empty : Button = new Button();
         FP.stage.addChildAt(empty ,0);
         FP.stage.setChildIndex(this, 1);
         FP.stage.addChildAt(bnGo,0); // I have also tired removing it before override it
        } 

I cannot just change the FP index because it’s out of bounds

OUTPUT : “The supplied index is out of bounds”.

Sorry if I’m becoming annoying.

Thanks for the help so far.


(Martí Angelats i Ribera) #14
override public function init():void
{
	//make the display objects
	var button:Button = new Button();
	
	//move the index of the Main
	FP.stage.setChildIndex(this, 1);
	
	//Add the new display objects
	FP.stage.addChildAt(button, 0);
}

First move the Main then add the display objects (in this case the buttons).

PS: You are trying to add the empty into the index 0, which is the index of Main. An expected error.


(Adam Edney) #15

I cannot run…

FP.stage.setChildIndex(this, 1);

without getting this exception error. :frowning:

e.g.

        override public function init():void
	{
               var button:Button = new Button();

	       FP.stage.setChildIndex(this, 1);

               FP.stage.addChildAt(button, 0);
	}

OUTPUT: [Fault] exception, information=RangeError: Error #2006: The supplied index is out of bounds.

Here are some images

When im swapping the indexs

without swapping the index (sorry for my bad hand writing :slight_smile: )


(Martí Angelats i Ribera) #16

Oh sorry my fault. Try this:

        override public function init():void
	{
               var button:Button = new Button();

               FP.stage.addChild(button);

	       FP.stage.setChildIndex(button, 0);

	}

BTW, I feel like it will be a lot easier if you make your own UI using FlashPunk. Here a tutorial (a bit old but still working exactly the same way)


PS: I’m still not sure why you want to use this.


(Adam Edney) #17

Yh Still didn’t work,

This is the GUI i downloaded http://sibirjak.com/osflash/projects/as-dataprovider-controls/ I made my own GUI but this one is far more complete. I will Redesign my own one tomorrow after work. Thanks for all the help on this topic, much appreciated.


(Martí Angelats i Ribera) #18

So, what exactly are you trying to do? Making a UI?


(Adam Edney) #19

Originally yes, but more and more into this I wanted to learn more about AS3 and all the different thing I have never used. I may just use https://github.com/copying/FPGUI for FP GUI.

Lol I just notice you made it :))


(Martí Angelats i Ribera) #20

LEL; you are using my project. It’s in hold right now and it’s not finished at all.