English 中文(简体)
visible property of DisplayObject
原标题:

For example I have a hierarchy of movie clips. mc1 is a child of mc, and mc2 is a child of mc1. Turns out that when I set

    mc1.visible = false;

mc2.visible stays true.

Is that supposed to happen? Is there a shortcut for testing visibility of mc2?


Code to play with:

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 of testing mc2 presence on stage except iterating over parents?

最佳回答

If a parent s visible property is set to false, none of its children will be visible in the stage. But that doesn t mean that children s visible properties would be automatically set to false - they will continue to hold their original values.

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

Compile the following code and click on the text field to understand it better. The textfield will become invisible (as it s parent s visible is set to false), but its own visible property continues to be 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.
}

Update to answer clorz question on 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;
}
问题回答

Yes a child of a parent that is set visible=false will be hidden as well. It follows the simple hierarchy.

And you can always test for a visible status by

if(uiObject.visible) ...

Alternatively you can always set the alpha = 0, but in terms of memory management it s best to remove the object off the stage if you re dealing with a lot of objects.

More info in this article

Nope that is not supposed to happen. If you hide a parent MovieClip then the child will always be hidden as well. My guess is that either mc2 is not a child of mc1 or your refering to another clip somewhere else that is also called mc1.

Yes, that is supposed to happen. It s the hierarchy that is playing its role in this case. You set visible to false for mc1 which makes mc1 invisible and as mc2 is child of mc1, so it will also disappear. (in other words mc2 is visible or invisible inside mc1). So if visible is reset to true for mc1 then mc2 will show up too depending on its visible property.

Here s a recursive function I made that takes a child and iterates up through the hierarchy till it runs out of DisplayObjects. If it finds an invisible parent along the way, it returns false, but if all the parents are visible it returns true:

function allParentsVisible(obj:DisplayObject):Boolean{
    //trace("
--- 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);
}




相关问题
Disable button tooltip in AS3

I want to disable the tooltip on certain buttons. The tooltip manager seems to be an all or nothing solution. Is it possible to disable the tooltip for just one or two buttons?

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

Red5 Security Tutorial

I am looking for a step by step tutorial on securing Red5 from intrusion. This seems to be a question that comes up alot in a google search, but is never really answered in a way that makes sense to ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

visible property of DisplayObject

For example I have a hierarchy of movie clips. mc1 is a child of mc, and mc2 is a child of mc1. Turns out that when I set mc1.visible = false; mc2.visible stays true. Is that supposed to happen?...

热门标签