Modern addiction: ActionScript 3.0 Chit chat about AS 3.0, OO concepts, and Flash in general…

26Jul/090

Enable stage object in loaded SWF

Suppose you load some SWF in your main SWF, and you want it to have access to main's stage. In Document Class of loaded SWF you should add:

public function LoadedSWFConstructo() {
  this.addEventListener( Event.ADDED_TO_STAGE, init );
}
 
public function init( event:Event ):void {
  trace( stage.quality );
}

So, in loaded SWF constructor you would register for ADDED_TO_STAGE event, and then you can use stage object. Simple enough.

Why do you need this? Mostly when having your application in full browser window. When resizing occurs you would want to resize or move objects in you loaded SWF. Imagination is the limit. :-)

Tagged as: , , , No Comments
25Jul/091

Getting fresh data from database

2702418661_28a44ea538

Problem

Problem is that some browsers won't refresh data got from database. They cache query results. Example:

var url:String = "http://www.example.com/get_data.php";
var req:URLRequest = new URLRequest( url );
var loader:URLLoader = new URLLoader();
loader.load( req );

So with this you can load results from php document. But when you ask for results again, some browsers (I noticed IE) will cache results and you will get same results. Refreshing the page and loading your application again will help. But you don't want user to refresh page, don't you?

Solution

Solution is pretty simple. In order to get new result you must confuse browser. You can confuse it by giving it new url to load. So, on our original url we will give it some meaningless sufix which won't do anything other than generate new url.

url += "?randomVar=" + Math.random();

Your new url will look like this now:

http://www.example.com/get_data.php?randomVar=0.8319580480456352

This way on every refresh you get different number as sufix, making your url unique.

Finally your code should be:

var url:String = "http://www.example.com/get_data.php";
url += "?randomVar=" + Math.random();
var req:URLRequest = new URLRequest( url );
var loader:URLLoader = new URLLoader();
loader.load( req );
Tagged as: , , , , 1 Comment