Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 9 of 9

Thread: using Array inside a loop to make some objects visible/invisible

  1. #1

    using Array inside a loop to make some objects visible/invisible

    Hello!

    I am not an expert on Arrays and have some problems with them.
    I have a Flash animation in which I display 5 instances of Symbols (movie clips) on the Scene.
    I create an Array with them, called "Firsts".
    There are other 5 instances (which are initially hidden) with which I create anothet Array, named "Seconds".
    When the user clicks on an image of "Firsts", letīs say "Firsts[2]", for instance, then "Seconds[2]" is made visible. If it is already visible it's made invisible.

    This is my code:

    Code:
    import flash.display.MovieClip;
    var Firsts:Array =new Array();
    var one:MovieClip=getChildByName('Sharing') as MovieClip;
    var two:MovieClip=getChildByName('mano') as MovieClip;
    var three:MovieClip=getChildByName('face') as MovieClip;
    var four:MovieClip=getChildByName('exchan') as MovieClip;
    var five:MovieClip=getChildByName('mondrian') as MovieClip;
    Firsts[0]=one;
    Firsts[1]=two;
    Firsts[2]=three;
    Firsts[3]=four;
    Firsts[4]=five;
    var Seconds:Array =new Array();
    var six:MovieClip=getChildByName('Sharingmovie') as MovieClip;
    var seven:MovieClip=getChildByName('manomov') as MovieClip;
    var eight:MovieClip=getChildByName('movieface') as MovieClip;
    var nine:MovieClip=getChildByName('exchangemovie') as MovieClip; 
    var ten:MovieClip=getChildByName('mondrianmovie') as MovieClip;
    Seconds[0]=six;
    Seconds[1]=seven;
    Seconds[2]=eight;
    Seconds[3]=nine;
    Seconds[4]=ten;
    import flash.display.MovieClip;
    for (var i:int=0;i<Firsts.length;i++)
    {
    Firsts[i].buttonMode=true;
    Firsts[i].addEventListener(MouseEvent.CLICK, clickPag1);
        function clickPag1(event:MouseEvent):void {
    	gotoAndPlay(1);
    	if (Seconds[i].visible == true) {Seconds[i].visible = false}
    	else {Seconds[i].visible = true}
    	for(var t:int=0;t<Seconds.length && t!= i;i++)
    	  {
    	  var bottle:MovieClip = getChildByName('Seconds[t]') as MovieClip;
    	  if (bottle.visible == true) {bottle.visible = false;
    	  Seconds[t].gotoAndStop(1);}
    	  }
    	
    }
    }
    It works fine till (and included) the:
    gotoAndPlay(1);
    but them it displays an error #1009: something like: "it's not possible to access a property or method of a reference of a null object".

    So I guess I am not defining the object properly. I don't know why since I am doing (it seems to me) the same as when i use Firsts(i) just some lines above.

    Also the code in blue is wrong for sure. Here I just try to code it in a way that in case that another object (which belong to the Seconds Array, of course) is visible, I make it invisible. I use that loop for it not to include the object which has just been shown.

    thank you very much for any help I can get.

    Regards.

  2. #2
    1,391
    posts
    Registered User
    ok - first of all you never need to import the Class more than once, so you should remove the second declaration

    next, you can clean up those array declarations a bit - since those instances are already instantiated on the timeline and have instance names (according to your code), you can simply insert them into the array - as in:
    Code:
    var firsts:Array = [Sharing,mano,face,exchan,mondrian];
    var seconds:Array = [Sharingmovie,manomov,movieface,exchangemovie,mondrianmovie];
    next, you have the clickPag1() method declared inside the loop and can be externalized - but there is also a way to use the information from the event object to achieve your objective - because the two arrays are 'parallel' so-to-speak, you can use the index of one element to get the corresponding one from the other array - so, for instance, examine the following:
    Code:
    var firsts:Array = [Sharing,mano,face,exchan,mondrian];
    var seconds:Array = [Sharingmovie,manomov,movieface,exchangemovie,mondrianmovie];
    
    function init():void {
        for( var i:int=0; i<firsts.length; i++ ) {
            firsts[i].buttonMode = true;
            firsts[i].addEventListener( MouseEvent.CLICK, clickPag1 );
        }
    
        for( var j:int=0; j<firsts.length; j++ ) {
            seconds[j].visible = false;
            seconds[j].buttonMode = true;
            seconds[j].addEventListener( MouseEvent.CLICK, clickPag2 );
        }
    }
    
    function clickPag1( e:MouseEvent ):void {
        seconds[ firsts.indexOf( e.currentTarget ) ].visible = true;
        e.currentTarget.visible = false;
    }
    
    function clickPag2( e:MouseEvent ):void {
        e.currentTarget.visible = false;
    }

  3. #3

    Afrostyle

    Hello and thank you very much.

    It works, but not entirely.
    Because the: "e.currentTarget.visible=false;" within the first function is hidding the object which one clicks on, not the object being shown (if any) from the second Array.
    And also (this is what I was trying in blue writing) since the MC of the second Array (seconds) are animated objects and they are initially stopped, I would like them to start (from their first frame) when they are called.
    I used something like: "thenameoftheMC.gotoAndPlay(1);"
    I use this also because the objects already shown and hidden may be still running and if they are called again and they don't start running from their first frames the user would see them already started (from intermediate frames).

    I show what I had already made (only for two objects), which worked but not fine because some objects started to run (from their beginning) when they were called (as expected) but others needed to be called twice to be shown running (instead of stuck or stopped like now). I don't know why.since the code was the same for the five objects. I thought this could be solved with Arrays and the For loop, besides it's much of a simpler code.


    Code:
    import flash.display.MovieClip;
    var botocn:MovieClip = getChildByName('exchan') as MovieClip;
    botocn.buttonMode = true;
    botocn.addEventListener(MouseEvent.CLICK, clickPag8);
    
    
    function clickPag8(event:MouseEvent):void {
        gotoAndPlay(1);
        var minuevoMC:MovieClip = getChildByName('exchangemovie') as MovieClip;
                   exchangemovie.gotoAndPlay(1);
        if (minuevoMC.visible == true) {minuevoMC.visible = false}
        else {minuevoMC.visible = true}
        var botzs:MovieClip = getChildByName('manomov') as MovieClip;
        if (botzs.visible == true) {botzs.visible = false;
        manomov.gotoAndStop(1);}
        var botvs:MovieClip = getChildByName('Sharingmovie') as MovieClip;
        if (botvs.visible == true) {botvs.visible = false;
         Sharingmovie.gotoAndStop(1);}
       var botbs:MovieClip = getChildByName('movieface') as MovieClip;
        if (botbs.visible == true) {botbs.visible = false;
        movieface.gotoAndStop(1);}
        var botvrs:MovieClip = getChildByName('mondrianmovie') as MovieClip;
        if (botvrs.visible == true) {botvrs.visible = false;
         mondrianmovie.gotoAndStop(1);}
    }
    import flash.display.MovieClip;
    var botocnx:MovieClip = getChildByName('mondrian') as MovieClip;
    botocnx.buttonMode = true;
    botocnx.addEventListener(MouseEvent.CLICK, clickPag82);
    
    
    function clickPag82(event:MouseEvent):void {
        gotoAndPlay(1);
        var minuevoMC:MovieClip = getChildByName('mondrianmovie') as MovieClip;
                    mondrianmovie.gotoAndPlay(12);
        if (minuevoMC.visible == true) {minuevoMC.visible = false}
        else {minuevoMC.visible = true}
        var botzsx:MovieClip = getChildByName('manomov') as MovieClip;
        if (botzsx.visible == true) {botzsx.visible = false;
        manomov.gotoAndStop(1);}
        var botvsx:MovieClip = getChildByName('Sharingmovie') as MovieClip;
        if (botvsx.visible == true) {botvsx.visible = false;
         Sharingmovie.gotoAndStop(1);}
       var botbsx:MovieClip = getChildByName('movieface') as MovieClip;
        if (botbsx.visible == true) {botbsx.visible = false;
        movieface.gotoAndStop(1);}
        var botvrsx:MovieClip = getChildByName('exchangemovie') as MovieClip;
        if (botvrsx.visible == true) {botvrsx.visible = false}
    }
    Sorry if it is confusing, it's very likely that I am not explaining the problem clearly.

    I would appreciate any help again. thank you. In any case I have learnt some interesting things from the previous message.
    Last edited by Jose_M; April 25th, 2012 at 07:55 PM.

  4. #4
    1,391
    posts
    Registered User
    ok - so the way i thought you wanted this to go is that there is a series of objects on stage, when clicked it becomes invisible and 'reveals' another corresponding clip from the 'seconds' array, then when that instance is clicked it disappears - which is what the above will do - if you want to restart the instance revealed from the seconds array when shown, in the clickPag1() method add the line:

    seconds[ firsts.indexOf( e.currentTarget ) ].gotoAndPlay(1);

    without stepping through the logic in the conditional tangle you have going there, i'd say that there are going to be issues that come up as you add more instances to the system, you'll have to continually go back and add more and more conditions to each instances handlers to maintain the functionality that you're looking for - you we're right in that using some loops and arrays will help to clarify the declarations and operation of your code - and you'll notice that to add ay amount of clips to the system, you only need to add the instance names to the appropriate arrays without any further code required - but you'll have to actually put these advices into practice, for instance: i still see there are two declarations for importing the MovieClip Class, where i had mentioned previously that this was not advisable (although that may be 'old' code if i'm reading this correctly)

  5. #5

    Afrostyle

    Hello,

    thank you very much again.
    Sorry for not expressing myself properly. It works fine, but I need something more. Because I don't want the MC (from the First Array), which i click on, to dissapear but the one related to it (from the Second Array).
    The line:

    e.current.visible=false; (within first function)


    makes the MC from the first Array to dissapear. I need to substitute this for something like:

    seconds.visible=false;

    but it doesn't work. This is to hide the current shown MC (from second Array) when I click on a MC from first Array. This way there is always just one MC shown from the second array. When one is shown the previous one is hidden.
    The five MV of the first Array are always shown.

    Yes, I have the tendency to add some scripts several times, just in case they are needed (because very often I am not sure about it); in the first case it was a typo (import MC class)xd, by copying and pasting. I have to correct this tendency!

    And the other problem (the one i started in blue), although less important, I haven't been able to solve it.
    Supposedly, (in the piece of code of mine) when I click on a MV (from first Array) the MV already displayed from second Array is sent to its first frame and stopped:

    For instance: Sharingmovie.gotoAndStop(1);


    And when I click on its match from first Array (Sharing) I send it again to its frist frame and play it:

    Sharingmovie.gotoAndPlay(1);

    Since this means I am "sending" it twice to its first frame, I have also tried doing this just once, but the problems continues.
    What happens is that the first time I click on a MC (from first Array) this is displayed correctly. But I don't know why, next time I click on it it's shown but stopped (unless I have given time to the previous time to go to the end ). Then next time it runs fine. Next is stopped, etc..It is shown running properly every two clicks. (This happens with every MV, although some seemed to work always fine at first).

    I am planning to add more MC, so I really need to use loops.XD

    Thank you very much again for your help.
    Last edited by Jose_M; April 26th, 2012 at 09:03 AM. Reason: some typos

  6. #6
    1,391
    posts
    Registered User
    I don't want the MC (from the First Array), which i click on, to dissapear but the one related to it (from the Second Array)
    alrighty, now i gotcha so you'd just want to change the clickPag1() method to:
    Code:
    function clickPag1( e:MouseEvent ):void {
        seconds[ firsts.indexOf( e.currentTarget ) ].visible = false;
    }
    see, in the above line the instance clicked is e.currentTarget, by using the indexOf() method of the Array class you can 'look up' the position of the instance - then, using that number you can 'target' the corresponding instance from the seconds array

    Supposedly, (in the piece of code of mine) when I click on a MV (from first Array) the MV already displayed from second Array is sent to its first frame and stopped:

    For instance: Sharingmovie.gotoAndStop(1);

    And when I click on its match from first Array (Sharing) I send it again to its frist frame and play it:

    Sharingmovie.gotoAndPlay(1);

    Since this means I am "sending" it twice to its first frame, I have also tried doing this just once, but the problems continues.
    what doesn't make sense here is that according to what you've described, it sounds as though the clips from the firsts array, 'toggle' the visibility of the clips from the seconds array - that is to say the second time you click on the object from the 'firsts' array it appears again? otherwise how could you click on or 'see' it twice?

    What happens is that the first time I click on a MC (from first Array) this is displayed correctly. But I don't know why, next time I click on it it's shown but stopped
    what's 'shown stopped'? the instance from the seconds array is made visible again on the second click?

    Then next time it runs fine. Next is stopped, etc..It is shown running properly every two clicks. (This happens with every MV, although some seemed to work always fine at first).
    so it now sounds like your wanting to toggle the visibility of the seconds instance using the first as buttons - this is much different than the initial description

    so now according to this, it would seem to operate more like this:
    Code:
    var firsts:Array = [Sharing,mano,face,exchan,mondrian];
    var seconds:Array = [Sharingmovie,manomov,movieface,exchangemovie,mondrianmovie];
    
    function init():void {
        for( var i:int=0; i<firsts.length; i++ ) {
            firsts[i].buttonMode = true;
            firsts[i].addEventListener( MouseEvent.CLICK, clickPag1 );
        }
    
        for( var j:int=0; j<firsts.length; j++ ) {
            seconds[j].visible = false;
            seconds[j].gotoAndStop(1);
        }
    }
    
    function clickPag1( e:MouseEvent ):void {
        var mc:MovieClip = MovieClip( seconds[ firsts.indexOf( e.currentTarget ) ] );
        mc.visible = !mc.visible;
        ( mc.visible ) ? mc.gotoAndPlay(1) : mc.gotoAndStop(1);
    }

  7. #7
    Hi and thank you very much again.

    Sorry,
    (((((My five MC of the first Array are shown on the top of the frame. They are used like/as buttons, yes. They show the same image than the ones bellow (of second Array). The difference is that the ones below are animated and I want them to be shown running, from their beginning.

    when I talk about clicking twice I don't mean clicking twice on the same image.
    I mean. the process, is the following. At first no images (animations) are shown on the botton.
    If I click, for instance, on the first image from the top, then the first image/animation from the botton is shown (they are in no-visible mode).
    This first animation runs fine, from its beginning.
    then, I click, for instance, on the last image from the top. Inmediatly the first animation bellow is hidden and the last is shown. It runs fine as well.
    But now, if i click again on the first image above, its correspondent animation bellow is shown but not running. The same would happen to the other image/animation. and if I click on the first image from the top (now it would be its third time) the one on the bottom would be shown running correctly.The same would happen with the other image/animation. If i click again on the first image (now it would be its fourth time) it would be shown running.
    So, first time, it runs, second it doesn't, third it runs, fourth it doesnīt, fifth it runs, etc, etc..)))))

    Sorry again because English is not my native language.

    *****I have just tested it. The problem about the animations not running seems to be solved!(thanks a lot)
    there is still something missing. It is the fact that when a new image/animation is displayed the previous one/s are not hidden. I would like the previous image/animation to be hidden when a new one is displayed by a click.
    I guess some code has to be included within the function.
    With this piece of code that you provided me:

    seconds[ firsts.indexOf( e.currentTarget ) ].visible = true;

    the image/animation from the second Array is displayed.
    But how can i hide the previous one?

    That's why I was trying to add (at the beginning of the function) something like:

    Seconds.visible=true;

    It doesn't work.


    p.s. Actually I wouldn't like the animation on the bottom to dissapear when its corresponding image above is clicked again (it was fine at first, but as I may (or for sure.xd) have explained it badly, it has been changed in the code).


    thank you
    Last edited by Jose_M; April 26th, 2012 at 02:04 PM. Reason: adding p.s

  8. #8
    1,391
    posts
    Registered User
    alrighty, now were getting somwhere

    so if i'm following you correctly now, the issue is that when any of the buttons are clicked, you want the previously shown animation from the seconds array to be removed - got it - so more along the lines of:
    Code:
    function clickPag1( e:MouseEvent ):void {
        var mc:MovieClip = MovieClip( seconds[ firsts.indexOf( e.currentTarget ) ] );
    
        for( var obj in seconds ) {
            seconds[obj].visible = ( seconds[obj] == mc );
            ( seconds[obj].visible ) ? seconds[obj].gotoAndPlay(1) : seconds[obj].gotoAndStop(1);
        }
    }
    this should remove (make invisible) all of the seconds array, except the instance needed

    PS - no worries about language, and actually kudos! i couldn't communicate in another myself - so well done

  9. #9

    Hello,

    thank you very much cbeech, it works really fine!
    Besides, I have learnt something new. I can't appreciate enough.

    Regards

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012