Check deep entity from same layer


(reopucino) #1

If I create 2 or more entities, with the same name class, and that entites was stack, can I pick some one of them??

here sample MainWorld.as

override public function begin()
{
	add(new DragEntity(120, 120));
	add(new DragEntity(120, 120));
	add(new SlotEntity(240, 240));
}

(NoelFB) #2

I’m not entirely sure what you mean, could you explain a little more? If you want to get the entities of a specific class from the world you can call getClass();

var list:Vector.<DragEntity> = new Vector.<DragEntity>();    
getClass(DragEntity, list);
// now you have all the drag entities in the world stored in the list vector

(reopucino) #3

Thanks @noel for reply. Oke maybe I’m to shy to much talk… because my native not english, and my english very bad

here the probleme…

I have class DragEntity here the code :

   private var holderX:int;
	private var holderY:int;
	private var drag:Boolean = false;
	public function DragEntity(posX:int, posY:int, gambar : Image) 
	{
		super(posX, posY);
		graphic = gambar
		setHitboxTo(graphic);
	}
	
	override public function update():void 
	{
		super.update();
		if (collidePoint(x, y, world.mouseX, world.mouseY) && Input.mousePressed)
		{
			holderX = x - world.mouseX;
			holderY = y - world.mouseY;
			drag = true;
			world.bringToFront(this);				
		}
		if (drag)
		{
			x = holderX+world.mouseX;
			y = holderY+world.mouseY;
		}
		if (Input.mouseReleased) 
		{
			drag = false;
			world.sendToBack(this);				
		}
	}

and world class

override public function begin()
{
    add(new DragEntity(120, 120, red));
    add(new DragEntity(120, 120, blue));
    add(new SlotEntity(240, 240));
}

I just want drag the Red, what should i do??? So, when I drag the drag entity, only red will moving.


(Jacob Albano) #4

When you say that they were stacked, do you mean that they were at the same position (like in your example)? While using getClass() is a good start, I’d say you have a design issue on your hands if you need to access one in particular without any extra information available.

if you really don’t want to (or can’t) use a variable to refer to one in particular, you can give one of them a name and access it with getInstance():

var e:Entity = new DragEntity(120, 120);
e.name = "this one";
add(e);
add(new DragEntity(120, 120));

//  later

var e:DragEntity = getInstance() as DragEntity;

(NoelFB) #5

Ah, I think I understand better. Yeah I think you basically need to make a static variable somewhere that says if something is being dragged or not.

For example, once something has started being dragged, you could do Global.dragging = true; and once you’re done dragging, do Global.dragging = false;

Then, you only begin dragging if Global.dragging == false.

Here’s your code changed with that:

override public function update():void 
{
    super.update();
    if (!Global.dragging && collidePoint(x, y, world.mouseX, world.mouseY) && Input.mousePressed)
    {
        holderX = x - world.mouseX;
        holderY = y - world.mouseY;
        drag = true;
        Global.dragging = true;
        world.bringToFront(this);               
    }
    if (drag)
    {
        x = holderX+world.mouseX;
        y = holderY+world.mouseY;
    }
    if (Input.mouseReleased) 
    {
        drag = false;
        Global.dragging = false;
        world.sendToBack(this);             
    }
}

In this case I use a Global class for a static variable like that, but you could realistically place that anywhere.