Check for a child

Hello, I have a function:

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);
            }
        }

Basically I want to check if e.target contains a child element called check. If so, I want e.target to remove the child, otherwise I want to add a child. But the method I tried does not seem to work, although I think this is the way to go. Any suggestions?

+3
source share
4 answers

When you declare your Check object, ActionScript creates the reference code for that particular object.

, Check @c0ecc29. if , @c0ecc29 target. , Check @c0ecc29 target.

, clickHandler, Check, . Check @c0ecc29, .

, (DataGrid, Group ..).

EDIT: , - . , Check target . , Check , .

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;
}

Check target, , :

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);
  }
}
+2

'check', getChildByName(). . flash.display.DisplayObject.name

, getChildIndex()

0

check - , .

, ( DisplayObjectContainer).

.

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 (possibly toggleCheck) on the target and have this display object responsible for visualizing the validation (and deleting).

0
source

This worked perfectly for me in my situation:

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

This may or may not be exactly what you are looking for.

0
source

Source: https://habr.com/ru/post/1783473/


All Articles