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: random mc appear on condition

  1. #1
    153
    posts
    Registered User

    random mc appear on condition

    I have 5 movie clips on stage made invisible. Each time only 1 movie clip appear randomly. The next movie clip appear on condition that the previous movie clip must disappear by pressing of a button.
    Example: mc - A,B,C,D,E

    If B appear, a button must be pressed to make B disppear in order for the next button to appear randomly.

    Need help.

  2. #2
    1,391
    posts
    Registered User
    what code do you have so far?

  3. #3
    153
    posts
    Registered User
    This is my code:

    var arr:Array=new Array ("A1","A2","A3","A4","A5")
    function hideandshow(){for(i=1;i<arr.length+1;i++){_root["A"+i]._visible=false;}
    _root["A"+Math.ceil(Math.random()*5)]._visible=true;};
    hideandshow();
    var int:Number=setInterval(hideandshow,1000)

  4. #4
    1,391
    posts
    Registered User
    ok - not too shabby

    one thing that may make things a little simpler in your code here is to store the 'instances' rather than their 'names' (although what you have will work just fine) - but what you need is to add the mouse handler to each instance and respond when clicked right - so you could do that by using an quick loop at initiatlization

    here's a quick reconfigure:
    Code:
    var clips:Array = [];
    
    for( var i:int=0; i<5; i++ ) {
        clips[i] = this["A"+(i+1)];
        clips[i].onRelease = clicked;
    }
    
    function hideAndShow():Void {
        for( var i:int=0; i<clips.length; i++ ) {
            clips[i]._visible = false;
        }
        clips[ Math.floor(Math.random()*clips.length) ]._visible = true;
    }
    
    function clicked():Void {
        hideAndShow();
    }

  5. #5
    153
    posts
    Registered User
    Thanks for the code.
    However mc will appear more than once in a session, for example A2, A4, A1, A2, A5.
    Will it be possible for it to be A2, A4, A1, A3, A5 in each session ?

  6. #6
    1,391
    posts
    Registered User
    yes - there would be several ways to do so depending on how your project is set up - but the important thing to note in the above code is the initial loop through the Array adding the on handler to each instance - so with that in mind here's another quick rewrite using Strings:
    Code:
    var clips:Array = ["A2", "A4", "A1", "A2","A5"];
    
    for( var i:int=0; i<clips.length; i++ ) {
        _root[ clips[i] ].onRelease = hideAndShow;
    }
    
    function hideAndShow():Void {
        for( var i:int=0; i<clips.length; i++ ) {
            _root[ clips[i] ]._visible = false;
        }
        _root[ clips[ Math.floor(Math.random()*clips.length) ] ]._visible = true;
    }

  7. #7
    153
    posts
    Registered User
    Hi Cbeech,
    Thanks for your code again.
    May be you misunderstand my previous question.
    It works but not exactly what I need.
    There are 5 movie clips on stage, in each session if the button is clicked 5 times, an individual movie clip appear only once for every 5 clicks session, for example, one session of 5 times clicking of a button, the movie clips may appear like (A3, A5, A2, A1, A4), there should not be a movie clip appear more than once in a session such as A3, A4, A1, A1, A5.

    Hope you understand.

  8. #8
    1,391
    posts
    Registered User
    ok - so then you only want to make one invisible on each click - and progress through the series?
    something more like:
    Code:
    var clips:Array = ["A2", "A4", "A1", "A2","A5"];
    var index:Number = 0;
    
    for( var i:int=0; i<clips.length; i++ ) {
        _root[ clips[i] ].onRelease = hideAndShow;
    }
    
    function hideAndShow():Void {
        this._visible = false;
        if( ++index == clips.length ) return;
        _root[ clips[ index ] ]._visible = true;
    }

  9. #9
    153
    posts
    Registered User
    It work ok but there is 1 thing I don't understand and not able to solve it. With all the 5 movie clips, 4 invisible and left 1.
    Attached Files Attached Files

Thread Information

Users Browsing this Thread

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

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