// Always start with a "package" package com.mobvcasting { // Like Java you need to import the classes you are going to use import flash.display.Sprite; import flash.text.TextField; // Network library import flash.net.*; // Events library import flash.events.*; // Timer library import flash.utils.Timer; // The class name is the same as the file name, case sensitive // Extending Sprite or MovieClip gives you the ability to do most things you can do in Flash public class Flajax extends Sprite { // Declare a String variable private var astring:String = "Hello World"; // Declare a Text Field private var displayText:TextField = new TextField(); // Declare the URL Loader private var loader:URLLoader; // Declare the request private var request:URLRequest; public function Flajax():void { // Set the text of the textfield to be our string displayText.text = "" + astring; // Display it addChild(displayText); this.scaleX = 1; this.scaleY = 1; this.width = 100; this.height = 100; this.x = 20; this.y = 40; //displayText.x = this.x + 20; //displayText.y = this.y + 20; // Set the Background Color so we can see how big it is.. this.opaqueBackground = 0xFF0000; // Create the URL Loader - Load any Web document, like you would with AJAX loader = new URLLoader(); // Register the function that will receive the content loader.addEventListener(Event.COMPLETE, pageLoaded); // Create the request request = new URLRequest("http://itp.nyu.edu/~sve204/liveweb/textmarks.txt"); // Setup the Timer var myTimer:Timer = new Timer(2000, 0); myTimer.addEventListener("timer", loadPage); myTimer.start(); // Make the request //loadPage(); // Called by the timer } private function loadPage(event:TimerEvent):void { // Make the request try { loader.load(request); } catch (error:Error) { trace("Unable to load requested document."); } trace("Should have loaded"); } public function pageLoaded(event:Event):void { trace("loaded: " + loader.data); displayText.text = loader.data; if (loader.data.valueOf() == "blue") { // have to use "valueOf" to do == comparison this.opaqueBackground = "0x0000FF"; trace("should be blue"); trace(this.opaqueBackground) } else if (loader.data.valueOf() == "red") { // have to use "valueOf" to do == comparison this.opaqueBackground = "0xFF0000"; trace("should be red"); trace(this.opaqueBackground); } else { this.opaqueBackground = "0x" + loader.data; trace("should be: " + loader.data); trace(this.opaqueBackground); } } } }