vincent
vincent

Reputation: 197

checking if child exists

Hello i have a function as following:

private function seatClickHandler(e:MouseEvent):void{

            var check:Check = new Check();
            if(e.target.contains(check)){
                e.target.removeChild(seat);
            }else{
                e.target.addChild(check);
            }
        }

basicly i want to check if e.target contains a child called check. If it does i want e.target to remove the child, else i want to add the child. But the method i tried doesnt seem to work although i think this is the way to go. Any suggestions?

Upvotes: 3

Views: 19002

Answers (4)

brenjt
brenjt

Reputation: 16297

This worked perfectly fine for me in my situation:

if(possibleChild.parent == holder)
    holder.removeChild(possibleChild)

It may or may not be exactly what you're looking for.

Upvotes: 0

Jason Towne
Jason Towne

Reputation: 8050

When you declare your Check object, Actionscript creates a reference code for that specific object.

So the first time your code is run, your Check object could be given a reference of @c0ecc29. Your if statement checks to see if @c0ecc29 is a child component of target. It won't be, so the Check object with reference @c0ecc29 is added to target.

The second time the clickHandler is called, a new instance of the Check object is created which will have a new reference id. Your target has the original Check object with the @c0ecc29 reference so it won't get removed.

The correct way to get this working depends on what target is (DataGrid, Group, etc.).

EDIT: Based on your comments, I would try something like this. It checks to see if the Check object is a child of target and adds it if needed. Then when the Check object is clicked, it will toggle its visibility.

public var check:Check = new Check();

private function seatClickHandler(e:MouseEvent):void
{
  if(!e.target.contains(check))
  {
    check.addEventListener(MouseEvent.CLICK, check_handleClick);
    e.target.addChild(check);
  }
}

protected function check_handleClick(event:MouseEvent):void
{
  check.visible = !check.visible;
}

If you need to actually remove the Check object from target instead of just changing its visibility, you could try this:

public var check:Check = new Check();

private function seatClickHandler(e:MouseEvent):void
{
  if(!e.target.contains(check))
  {
    e.target.addChild(check);
  }
  else
  {
    e.target.removeChild(check);
  }
}

Upvotes: 2

Chris Bos
Chris Bos

Reputation: 335

check is a new object in the scope of that function, so it will not be a child of the event target.

What you want to do is declare check as a global variable (And also cast target as DisplayObjectContainer).

e.g.

private function seatClickHandler(e:MouseEvent):void{
                if((e.target as DisplayObjectContainer).contains(check)){
                    (e.target as DisplayObjectContainer).removeChild(seat);
                }else{
                    (e.target as DisplayObjectContainer).addChild(check);
                }
    }

However I'm not sure if this is exactly what you want to do (There can only be one check). A better approach would be to have a function (maybe toggleCheck) on the target, and have that display object responsible for rendering the check (And removing it)

Upvotes: 0

John Giotta
John Giotta

Reputation: 16934

If the child is named 'check' then you should be able to use getChildByName(). See flash.display.DisplayObject.name

If you happen to have the child in memory, you can use getChildIndex()

Upvotes: 0

Related Questions