Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Random Numer

Random number function - returns a number between min and max
Receives (min, max)
Returns randomNum


function randRange(min:Number, max:Number):Number {
    var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
    return randomNum;
}

Actionscript

// Countdown code

countDown = function() {
	seconds_conta = seconds_conta - 1;
	hours = int(seconds_conta / 3600);
	minutes = int((seconds_conta / 60) - (hours * 60));
	//seconds = int(seconds_conta-((hours*3600)+minutes*60));
	seconds = int((seconds_conta % 60));
	
	//Se for menor que dez, acrescenta um zero
	if(minutes<10){
		minutes="0"+minutes;
	}
	if(seconds<10){
		seconds="0"+seconds;
	}
	
	//Será que precisa colocar no frame correto?
	timer_txt.text=(hours+":"+minutes+":"+seconds);
	
		//trace("seconds_conta: "+seconds_conta);
		//trace("hours: "+hours);
		//trace("minutes: "+minutes);
		//trace("seconds: "+seconds);
	
	if (seconds_conta == 0) {
          clearInterval(timer);
		  //Chama função de acabar a contagem!
		  //fim_das_questões();
     }
} 

///////// Acrescentar no código
//Tempo total inicial em segundos ->Calculado a partir da quantidade de questões
seconds_conta = 185;
///////// Inicia o timer
//A cada 1000 ms, ativa a função countDown, diminuindo um segundo do total
timer = setInterval(countDown, 1000);

loadVars (from debugger.lzx)

// description of your code here

    <method name='checkServerResponse'>
      &lt;![CDATA[&lt;![CDATA[
        var loadobj = new LoadVars();
        loadobj.onData = this.checkServerResponseHandler;
        var reqstr =  LzBrowser.toAbsoluteURL( "__dbgprobe.lzx?lzt=stat", false );
        loadobj.sendAndLoad(reqstr , loadobj, "POST" );
      ]>
    </method>

    <method args='src' name='checkServerResponseHandler'>
      &lt;![CDATA[&lt;![CDATA[
      // NB: This is a callback handler, and 'this' will be bound to some LoadVars object, not to Debug
      if (src == undefined) {
        Debug.freshLine();  
        Debug.addHTMLText('&lt;font color="#FF8800">Debugger cannot contact LPS server, switching to SOLO mode.&lt;/font>\n');
        Debug.setAttribute('solo_mode', true);
      }
      ]>
    </method>

Check connection with Zinc

function isConnected():Boolean {
var results = mdm.Network.checkConnection();
return results;
}
var hasConnection = isConnected();
mdm.Dialogs.prompt(hasConnection);

Internet Connection: Flash 8 Only.

var connected:Boolean = false;
function checkConnection():Void {
var myLoadVars:LoadVars = new LoadVars();
myLoadVars.onHTTPStatus = function(httpStatus:Number) {
if (httpStatus != 0) {
connected = true;
} else {
connected = false;
}
delete this;
};
myLoadVars.load("http://www.apple.com");
}
checkConnection();

SetTimeout : Flash

function testMe() {
 trace("callback: "+getTimer()+" ms.");
}

var intervalID:Number = setTimeout(testMe, 1000);

// clearTimeout(intervalID) // for clear the timeout

Dynamically placed buttons

It's simple, yet I often forget this.

var myMC:MovieClip;
var i = 1;
var pad = 5;
while (i<6) {
	trace(" i : "+i);
	myMC.duplicateMovieClip("myMC"+i, i);
	myMC.removeMovieClip();
	this["myMC"+i]._x = this["myMC"+(i-1)]._x+(this["myMC"+(i-1)]._width)+pad;
	this["myMC"+i].onRollOver = function() {
		trace(this);
	};
	i++;
}

Retreiving a Flash movie's domain name

Use the LocalConnection object to get the domain name of the server where the Flash movie is located.

var localDomainLC:LocalConneciton = new LocalConnection();
myDomainName = localDomainLC.domain();
trace( "My domain is " + myDomainName );



This will print out something like this:

My domain is example.com


Use Flash's MovieClipLoader Class

I'm always forgetting the syntax for this...

var oImgListener = new Object();
oImgListener.onLoadInit = function(target_mc)
{
     trace(target_mc._name + "  load complete");
     ...
}

var mclImg = new MovieClipLoader();
mclImg.addListener(oImgListener);
mclImg.loadClip("http://myurl.com/path/to/swf/or/jpg", mcImgLoader);