Visible DisplayObject Property

For example, I have a hierarchy of video clips. mc1 is a child of mc, and mc2 is a child of mc1. It turns out when I install

mc1.visible = false; 

mc2.visible remains true.

Is this supposed to happen? Is there a shortcut to check mc2 visibility?


Code to play:

 var mc = new Sprite(); mc.graphics.beginFill(0xFF0000); mc.graphics.moveTo(50,50); mc.graphics.lineTo(100,50); mc.graphics.lineTo(100,100); mc.graphics.lineTo(50,100); mc.graphics.endFill(); var mc1 = new Sprite(); mc1.graphics.beginFill(0x00ff00); mc1.graphics.moveTo(150,150); mc1.graphics.lineTo(200,150); mc1.graphics.lineTo(200,200); mc1.graphics.lineTo(150,200); mc1.graphics.endFill(); mc.addChild(mc1); var mc2= new Sprite(); mc2.graphics.beginFill(0x0000ff); mc2.graphics.moveTo(250,150); mc2.graphics.lineTo(200,150); mc2.graphics.lineTo(200,200); mc2.graphics.lineTo(250,200); mc2.graphics.endFill(); mc1.addChild(mc2); stage.addChild(mc); mc1.visible = false; function myOnEnterFrame(e){ trace(mc2.hitTestPoint(mouseX, mouseY)); } stage.addEventListener(Event.ENTER_FRAME, myOnEnterFrame); 

Results: mc2.visible will still be true. hitTest will still fire for mc2.

Is there any other way to test the presence of mc2 on stage, other than reprising the role of parents?

+4
source share
5 answers

If the parent visible property is set to false , none of its children will be visible on the scene. But this does not mean that child visible properties will be automatically set to false - they will continue to retain their original values.

In short, a DisplayObject with the visible property true should not be visible on the stage - it also depends on the visible value of its parents. But if the visible object is set to false , it will not be visible no matter what.

Compile the following code and click on the text box to better understand it. The text field will become invisible (since its parent view is set to false ), but its own visible property remains true

 private var sprite:Sprite; private var tf:TextField; public function init():void { sprite = new Sprite(); addChild(sprite); tf = new TextField(); tf.text = "sometext"; sprite.addChild(tf); sprite.addEventListener(MouseEvent.CLICK, onClick) } private function onClick(e:MouseEvent):void { sprite.visible = false; trace(tf.visible);//traces true - but tf is not displayed. } 

Refresh to answer the clorz question about how to check if an object is visible or not:

 function isVisible(t:DisplayObject):Boolean { if(t.stage == null) return false; var p:DisplayObjectContainer = t.parent; while(!(p is Stage)) { if(!p.visible) return false; p = p.parent; } return true; } 
+8
source

Yes, the child of the parent, which is set to visible = false, will also be hidden. This follows a simple hierarchy.

And you can always check the visible status on

 if(uiObject.visible) ... 

Alternatively, you can always set alpha = 0, but in terms of memory management, it is best to remove an object from the scene if you are dealing with a large number of objects.

Additional information in this article.

+1
source

No, this should not happen. If you hide the parent MovieClip, then the child will always be hidden. I assume that either mc2 is not a child of mc1, or refers to another clip somewhere else, also called mc1.

+1
source

Yes, this is bound to happen. This is a hierarchy that plays a role in this case. You set the visible value to false for mc1, which makes mc1 invisible and mc2 is a child of mc1, so it will also disappear. (in other words, mc2 is visible or invisible inside mc1). So if the visible reset is true for mc1, then mc2 will also be displayed depending on its visible property.

+1
source

Here's the recursive function I did that takes the child and iterates through the hierarchy until the display of the DisplayObjects objects ends. If it detects an invisible parent along the way, it returns false, but if all parents are visible, it returns true:

 function allParentsVisible(obj:DisplayObject):Boolean{ //trace("\r--- Test for visibility ---"); var counter:Number = 0; var safetyLimit:Number = 10; var parent:DisplayObject = obj; var allVisible:Boolean = true; doTest(); function doTest(){ parent = parent.parent; if(parent && counter < safetyLimit){ if(!parent.visible) allVisible = false; doTest(); }else{ return; } counter ++; } return(allVisible); } 
+1
source

All Articles