Copy link to clipboard
Copied
Hey,
So i want my UiLoader to random select a Swf.
I have 3 swf files and i can't seem to figure out how to make it select random just as a dice, wich i made to try and get random answer.
So here's my code, including the dice kinda thingi. I'm a starter and just can't figure it out lol, please any ideas / suggestions / answers?
Thanks
note: the 'knopLinks' and 'knopRechts' are mc buttons i use for the dice i want them to do the same but with swf in an uiloader
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.net.URLRequest;
//Declaring Variables
var dice:MovieClip = diceMc;
var knopLinks:MovieClip = knopLinks;
var knopRechts:MovieClip = knopRechts;
var currentRoll:uint = 0;
var urlRequest:URLRequest = new URLRequest();
var loader:UILoader = loaderMc;
//Number = 0.5 or - 0.5
//int = 1 or -1
//uint = >0
//Init
dice.stop ();
knopLinks.stop ();
knopRechts.stop ();
//Events
knopLinks. addEventListener ( MouseEvent.MOUSE_DOWN,knopLinksDown );
knopLinks. addEventListener ( MouseEvent.MOUSE_UP,knopLinksUp );
knopRechts. addEventListener ( MouseEvent.MOUSE_DOWN,knopRechtsDown );
knopRechts. addEventListener ( MouseEvent.MOUSE_UP,knopRechtsUp );
//Functions
function knopLinksDown(e:MouseEvent){
dice.play();
}
function knopLinksUp(e:MouseEvent){
currentRoll = 1 + Math.round(Math.floor(Math.random() * 3));
dice.gotoAndStop(currentRoll);
}
function knopRechtsDown(e:MouseEvent){
dice.play();
}
function knopRechtsUp(e:MouseEvent){
currentRoll = 1 + Math.round(Math.floor(Math.random() * 3));
dice.gotoAndStop(currentRoll);
}
function buttonDown(e:MouseEvent){
trace(e.currentTarget.swf);
urlRequest = new URLRequest('swf/' + e.currentTarget.swf)
loader.load(urlRequest);
SoundMixer.stopAll();
}
Message was edited by: KayKiffen
Copy link to clipboard
Copied
the easiest way is to list the 3 swfs in an array and then randomly select an array element:
var swfA:Array=['swf/swfname1.swf','swf/swfname2.swf','swf/swfname3.swf'];
yourbutton.addEventListener(MouseEvent.CLICK,clickF);
function clickF(e:MouseEvent):void{
loader.load(new URLRequest(swfA[Math.floor(swfA.length*Math.random())]));
}
Copy link to clipboard
Copied
Put the Strings for the swf files into an array and use the currentRoll value minus 1 to pick one of them. Then in your buttonDown function you could use....
urlRequest = new URLRequest('swf/' + swfURLArray[currentRoll])
It looks like your random number code has an unnecessary Math.round() in it. Math.floor will take care of the rounding you need by itself.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now