Hello World

AS3

This is one of the first demos I wrote in AS3. Here, I am experimenting with the Flash authoring environment - the two buttons were created beforehand in the Flash timeline rather than coded in AS3. The code for the demo is provided at the bottom of the page.

Click the add and subtract buttons or use the arrow keys to see what happens.

The following code was added straight into the first frame of the flash movie.

var num:Number = 0;
var dx:Number = 0, dy:Number = 0;

Counter.addEventListener(MouseEvent.CLICK, CountIt);
SubCounter.addEventListener(MouseEvent.CLICK, SubIt);
TheCount.text = "0"

var Box:Sprite = new Sprite();
var T:TextField = new TextField;

T.text = "Use the arrow keys to move me.";
T.width = 300;
T.x = 20;

Box.graphics.lineStyle(0x000000);
Box.graphics.beginFill(0x777777);
Box.graphics.drawRect(0, 0, 200, 20);
Box.graphics.endFill();
Box.x = 150;
Box.y = 250;

this.addChild(Box);
Box.addChild(T);

stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyUnpressed);

var mainTimer:Timer = new Timer(5); // every 20 milliseconds
mainTimer.addEventListener(TimerEvent.TIMER, MainLoop);
mainTimer.start();

function CountIt(e:MouseEvent):void {
num++;
TheCount.text = String(num);
}

function SubIt(e:MouseEvent):void {
num--;
TheCount.text = String(num);
}

function KeyPressed(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.LEFT) dx = -2;
if (e.keyCode == Keyboard.RIGHT) dx = 2;
if (e.keyCode == Keyboard.UP) dy = -2;
if (e.keyCode == Keyboard.DOWN) dy = 2;
}

function KeyUnpressed(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.LEFT) dx = 0;
if (e.keyCode == Keyboard.RIGHT) dx = 0;
if (e.keyCode == Keyboard.UP) dy = 0;
if (e.keyCode == Keyboard.DOWN) dy = 0;
}

function MainLoop(e:TimerEvent):void {
Box.x += dx;
Box.y += dy;
if (Box.x < 0) Box.x = 0;
if (Box.y < 0) Box.y = 0;
if (Box.x > 350) Box.x = 350;
if (Box.y > 380) Box.y = 380;
e.updateAfterEvent()
}

Languages: ActionScript (AS3/AS2) • Ajax • Assembly • ASP • C • C++ • CSS • DHTML • HaXe • HTML/HTML5 • Java • JavaScript • JSON • jQuery • MySQL • Pascal • PHP • Visual Basic • XML

Software & Frameworks: Adobe Air • Adobe Flash • Android • Audacity • CoolEdit • Dreamweaver • Eclipse • Facebook Integration • FireBug • Flash Builder • FlashDevelop • Flex • Gimp • Illustrator • MacOS • Media Encoder • MS Office • Netbeans • Open Office • Photoshop • phpMyAdmin • TortoiseSVN • Visual Studio • Windows • XAMPP • UNIX