Skip to main content
Participant
April 19, 2022
Answered

Hide object/image one by one

  • April 19, 2022
  • 1 reply
  • 432 views

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

    This topic has been closed for replies.
    Correct answer JoãoCésar17023019

    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

    1 reply

    JoãoCésar17023019
    Community Expert
    Community Expert
    April 19, 2022

    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

    NewOne01Author
    Participant
    April 19, 2022

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

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    April 19, 2022

    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