Results 1 to 9 of 9
Thread: random mc appear on condition
-
April 13th, 2012, 11:41 AM #1144Registered User
postsrandom 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.
-
April 13th, 2012, 11:43 AM #21,391Registered User
postswhat code do you have so far?
-
April 14th, 2012, 08:58 AM #3144Registered User
postsThis 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)
-
April 14th, 2012, 10:41 AM #41,391Registered User
postsok - 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(); }
-
April 16th, 2012, 10:35 AM #5144Registered User
postsThanks 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 ?
-
April 17th, 2012, 01:29 AM #61,391Registered User
postsyes - 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; }
-
April 17th, 2012, 09:43 PM #7144Registered User
postsHi 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.
-
April 17th, 2012, 10:07 PM #81,391Registered User
postsok - 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; }
-
April 17th, 2012, 11:11 PM #9144Registered User
postsIt 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.

Reply With Quote

Bookmarks