Skip to main content
Participant
Apr 19, 2022 7:10 AM UTC
Answered

Hide object/image one by one

Hey, I am new to adobe animate.. how to hide object/image one by one by clicking the same button multiple times?

    This topic is closed to new replies. Start a new post to keep the conversation going.
    JoãoCésar17023019
    JoãoCésar17023019Correct answer
    Community Expert
    Apr 19, 2022 2:31 PM UTC

    Two approaches would be using array access (bracket notation) or storing the pencils in a vector or array.

     

    Array access (bracket notation):

    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    
    var counter:int = 10;
    
    minusbtn.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void
    {
        MovieClip(root)["pencil" + counter--].visible = false;
    		
        if (counter == 0)
            e.currentTarget.removeEventListener(e.type, arguments.callee);
    });

     

    Vector / Array:

    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    
    var pencils:Vector.<DisplayObject> = new <DisplayObject>
    [
    	pencil1,
    	pencil2,
    	pencil3,
    	pencil4,
    	pencil5,
    	pencil6,
    	pencil7,
    	pencil8,
    	pencil9,
    	pencil10
    ];
    
    minusbtn.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void
    {
        pencils.pop().visible = false;
    		
        if (pencils.length == 0)
            e.currentTarget.removeEventListener(e.type, arguments.callee);
    });

     

    Regards,

    JC

    5 replies

    JoãoCésar17023019
    Community Expert
    Community Expert
    Apr 19, 2022 10:10 AM UTC

    Hi.

     

    HTML5 Canvas

     

    var root = this;
    var button = root.yourButton;
    var container = root.yourContainer; // symbol that contains all objects that need to be hidden
    var counter = container.children.length;
    
    button.on("click", function(e)
    {
    	container.children[--counter].visible = false;
    		
    	if (counter === 0)
    		e.remove();
    });

     

     

    AS3

     

    import flash.display.SimpleButton;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    
    var button:SimpleButton = yourButton;
    var container:MovieClip = yourContainer; // symbol that contains all objects that need to be hidden
    var counter:int = container.numChildren;
    
    button.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void
    {
        container.getChildAt(--counter).visible = false;
    		
        if (counter == 0)
            e.currentTarget.removeEventListener(e.type, arguments.callee);
    });

     

     

    I hope it helps.

     

    Regards,

    JC

    Translate
    NewOne01Author
    Participant
    Apr 19, 2022 2:10 PM UTC

    I have button with instance name "minusbtn" and ten movieclip I named "pencil1"...until "pencil10"..how do I apply to AS3 code you give?

    Translate
    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    Apr 19, 2022 2:31 PM UTC

    Two approaches would be using array access (bracket notation) or storing the pencils in a vector or array.

     

    Array access (bracket notation):

    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    
    var counter:int = 10;
    
    minusbtn.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void
    {
        MovieClip(root)["pencil" + counter--].visible = false;
    		
        if (counter == 0)
            e.currentTarget.removeEventListener(e.type, arguments.callee);
    });

     

    Vector / Array:

    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    
    var pencils:Vector.<DisplayObject> = new <DisplayObject>
    [
    	pencil1,
    	pencil2,
    	pencil3,
    	pencil4,
    	pencil5,
    	pencil6,
    	pencil7,
    	pencil8,
    	pencil9,
    	pencil10
    ];
    
    minusbtn.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void
    {
        pencils.pop().visible = false;
    		
        if (pencils.length == 0)
            e.currentTarget.removeEventListener(e.type, arguments.callee);
    });

     

    Regards,

    JC

    Translate