Archive for September, 2008:
A simple flash preloader
Build a preloader witch will inform our users how much of our content is loaded. You will learn how to make a custom looking preloader using Flash and AS3. This preloader can contain text, animation and whatever you wish.
Actionscript 2.0
var pc2:Number = 0;
this.onEnterFrame = function() {
//avoid NaN (Not a number)
if (this.getBytesLoaded() > 5) {
pc = Math.floor((this.getBytesLoaded()/this.getBytesTotal())*100);
pc2 = Math.floor((this.getBytesLoaded()/this.getBytesTotal())*300);
loader.bar._width = pc2
loader.t_num.text = pc+"%";
if (pc == 100 and !isNaN(pc)) {
delete this.onEnterFrame;
gotoAndPlay(2);
}
}
};
Actionscript 3
import flash.events.Event;
var prog:Number;
this.addEventListener(Event.ENTER_FRAME,loader);
function loader(e:Event) {
prog = Math.floor(((this.loaderInfo.bytesLoaded/this.loaderInfo.bytesTotal)*100));
progressbar.bar.width = prog/100 * 270; //270 is total width of the bar
progressbar.percent.text = prog + "%";
if (this.loaderInfo.bytesLoaded == this.loaderInfo.bytesTotal) {
//after finish loading remove the enterframe event
this.removeEventListener(Event.ENTER_FRAME,loader);
gotoAndStop(2);
}
}
Writing a simple actionscript preloader is easy, and does not requires much. One of the common problem faced when designers or developers when they compile their clip with preloaders is that the preloader takes quite some time to appear. The reason of this problem is the amount of chunk(movie clips, components, scripted sounds) needed for the preloader to appear is too high. This is due to your flash application is attached to a lot of scripted sounds, components and movieclips.
To fix this problem: -
a) Change the export classes to load from frame settings. To do click click for publish settings. Under Flash tab, click on the button beside Actionscript version called Settings. When the dialog pop up, change your export classes to load from frame settings to frame 2 instead.
RSS Reader with Actionscript 3
Here is my experient about building a flash movie that will load, parse and display RSS feeds. RSS is a format for syndicating news and webblogs, it has taken on a life of its own and has become perhaps the most popular XML format today. You will be exposed to quite a bit of information about working with XML documents in Flash.
loader.addEventListener(Event.COMPLETE, onLoaded);
lb.addEventListener(Event.CHANGE, itemChange);
function itemChange(e:Event):void {
ta.htmlText = lb.selectedItem.data;
}
var xml:XML;
function onLoaded(e:Event):void {
xml = new XML(e.target.data);
var il:XMLList = xml.channel.item;
for (var i:uint=0; i<il.length(); i++) {
lb.addItem({data:il.description.text()[i],
label:il.title.text()[i]});
}
}
function loadxmlnow() {
lb.removeAll();
loader.load(new URLRequest(url.text));
}
btn.addEventListener(MouseEvent.MOUSE_UP,
function(evt:MouseEvent):void {
loader.load(new URLRequest(url.text));
loadxmlnow();
}
);
btnstar.addEventListener(MouseEvent.MOUSE_UP,
function(evt:MouseEvent):void {
url.text = "http://thestar.com.my/rss/nation.xml"
loadxmlnow();
}
);
btncnn.addEventListener(MouseEvent.MOUSE_UP,
function(evt:MouseEvent):void {
url.text = "http://rss.cnn.com/rss/edition_world.rss"
loadxmlnow();
}
);
btnweather.addEventListener( MouseEvent.MOUSE_UP,
function(evt:MouseEvent):void {
url.text = "http://weather.yahooapis.com/forecastrss?p=MYXX0008"
loadxmlnow();
}
);
