ActionScript

From SpenchWiki
Jump to: navigation, search
  • Using the Loader component to load a JPG
/**
 Requires:
  - Loader component instance on Stage (instance name: my_ldr)
  - Progress component instance on Stage (instance name: my_pb)
*/

System.security.allowDomain("http://www.helpexamples.com");

var my_ldr:mx.controls.Loader;
var my_pb:mx.controls.ProgressBar;

my_pb.mode = "polled";
my_pb.source = my_ldr;
my_ldr.autoLoad = false;


//Create Listener Object
var pbListener:Object = new Object();
pbListener.complete = function(evt_obj:Object){
  // event_obj.target is the component that generated the complete event,
  // i.e., the progress bar.
    trace("Minimum value is: " + evt_obj.target.minimum + " bytes");
    trace("Maximum value is: " + evt_obj.target.maximum + " bytes");
    trace("Current ProgressBar value = " + evt_obj.target.value + " bytes");
}
//Add Listener
my_pb.addEventListener("complete", pbListener);

// when autoLoad is false loading does not start until load() is invoked
my_ldr.load("http://www.helpexamples.com/flash/images/image1.jpg");
  • Loading variables using LoadVars()
var my_lv:LoadVars = new LoadVars();
my_lv.onLoad = function(success:Boolean):Void {
    if (success) {
        trace(this.dayNames); // Sunday,Monday,Tuesday,...
    } else {
        trace("Error");
    }
}
my_lv.load("http://www.helpexamples.com/flash/params.txt");
  • Loading and traversing XML content using XML()
this.createTextField("my_txt", 10, 10, 10, 320, 100);
my_txt.autoSize = "left";
my_txt.border = true;
my_txt.multiline = true;
my_txt.wordWrap = true;

var reviews_xml:XML = new XML();
reviews_xml.ignoreWhite = true;
reviews_xml.onLoad = function (success:Boolean):Void {
    if (success) {
        var childItems:Array = reviews_xml.firstChild.childNodes;
        for (var i:Number = 0; i < childItems.length; i++) {
            my_txt.text += childItems[i].firstChild.firstChild.nodeValue + "\n";
        }
    } else {
        my_txt.text = "Unable to load external file.";
    }
}
reviews_xml.load("http://www.helpexamples.com/flash/xml/reviews.xml");
  • Specifying and accessing FlashVars
this.createTextField("my_txt", 10, 10, 10, 100, 21);
my_txt.text = _level0.username;

NOTE This should now be done using SWFObject:

<script type="text/javascript" src="swfobject.js"></script>
<div id="flashcontent">JavaScript and Flash Player are required.</div>
<script type="text/javascript">
	var fo = new SWFObject("file.swf", "viewer", "100%", "100%", "8", "#181818");
	fo.addVariable("quality", "best");
	fo.addVariable("scale", "noScale");
	fo.addVariable("var", "value");
	fo.write("flashcontent");
</script>

Old object/embed method:

<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.adobe.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="550" height="400" id="flashvars" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="movie" value="flashvars.swf" />
<param name="FlashVars" value="username=Thomas" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<embed src="flashvars.swf" FlashVars="username=Thomas" quality="high" bgcolor="#ffffff" width="550" height="400" name="flashvars" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />
</object>
  • Playing an FLV

Drag the video object from the Library panel to the Stage to create a video object instance. With the video object selected on the Stage, type my_video in the Instance Name text box in the Property inspector (Window > Properties > Properties). With the video instance still selected, type 320 in the width text box and 213 in the height text box in the Property inspector. Select Frame 1 in the Timeline, and open the Actions panel (Window > Actions). Type the following code in the Actions panel:

var connection_nc:NetConnection = new NetConnection();
connection_nc.connect(null);
var stream_ns:NetStream = new NetStream(connection_nc);
my_video.attachVideo(stream_ns);
stream_ns.play("http://www.helpexamples.com/flash/video/lights_short.flv");

this.createTextField("loaded_txt", this.getNextHighestDepth(), 10, 10, 160, 22);
var loaded_interval:Number = setInterval(checkBytesLoaded, 500, stream_ns);
function checkBytesLoaded(my_ns:NetStream) {
    var pctLoaded:Number = Math.round(my_ns.bytesLoaded / my_ns.bytesTotal * 100);
    loaded_txt.text = Math.round(my_ns.bytesLoaded / 1000) + " of " + Math.round(my_ns.bytesTotal / 1000) + " KB loaded (" + pctLoaded + "%)";
    progressBar_mc.bar_mc._xscale = pctLoaded;
    if (pctLoaded >= 100) {
        clearInterval(loaded_interval);
    }
}