|
Hello World! on a Timer
package
{
/* a tad tutorial
copyright 2008 (t)a.d
start package
import all of display text and events
and import flash utils timer */
import flash.display.Sprite;
import flash.display.Graphics;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.text.TextFieldType;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
import flash.utils.Timer;
/* start the actual swf file
this extention is the sprite that takes up the stage */
public class TimerTimeline extends Sprite
{
/* assign variable types */
private var theTimeline:Timer;
private var theShowTyping:TextField;
private var theShowTypeFormat:TextFormat;
public function TimerTimeline()
{
/* create a text field format */
theShowTypeFormat = new TextFormat();
theShowTypeFormat.font = "Arial";
theShowTypeFormat.color = 0x000000;
theShowTypeFormat.size = 16;
theShowTypeFormat.align = TextFormatAlign.CENTER;
/* create a text field with the above format */
theShowTyping = new TextField();
theShowTyping.type = TextFieldType.DYNAMIC;
theShowTyping.defaultTextFormat = theShowTypeFormat;
theShowTyping.autoSize = TextFieldAutoSize.LEFT;
theShowTyping.text = "H";
/* add the text field to the stage
will be in top left corner
can reposition with theShowTyping.x and theShowTyping.y */
this.addChild(theShowTyping);
/* theTimeline can be thought of as 11 frames at 1 frame per 500 milliseconds (half a second)
it does not repeat
really its a timer that goes once every 500 milliseconds 11 times then stops
if you want it to go forever put 0 instead of 11
it can be controlled with .start() .stop()
however, when it reaches 11 (or whatever num your using other than 0) it will not start again
until you use the .reset() command
it only has the currentCount property (used below)
if the repeatCount (the 11 spot) is not 0
the spot with the milliseconds is called the delay */
theTimeline = new Timer(500, 11);
/* now an event listener is added to this timer
which itself is an event
it gets some special events called TimerEvents
namely TIMER and TIMER_COMPLETE
timer fires on every repeatCount interval
and timer complete fires when and only when the repeatCount is finished
(therefore not initialy 0)
note that the last timer event will happen and then the timer complete event */
theTimeline.addEventListener(TimerEvent.TIMER, doTheAnimation);
theTimeline.start();
/* the replay button */
var resetButton:Sprite = new Sprite();
resetButton.graphics.lineStyle(1, 0x000000);
resetButton.graphics.beginFill(0x000000, .2);
resetButton.graphics.drawRect(0, 0, 65, 20);
/* replay text */
var resetButtonTXT:TextField = new TextField();
resetButtonTXT.type = TextFieldType.DYNAMIC;
resetButtonTXT.selectable = false;
resetButtonTXT.defaultTextFormat = theShowTypeFormat;
resetButtonTXT.autoSize = TextFieldAutoSize.LEFT;
resetButtonTXT.text = "replay";
/* position of the button and text */
resetButton.y = 25;
resetButtonTXT.y = resetButton.height/2 + resetButtonTXT.height/2;
resetButtonTXT.x = resetButton.width/2 - resetButtonTXT.width/2;
/* give the sprite a pointer cursor */
resetButton.buttonMode = true;
/* add the replaytimer func as the onclick */
resetButton.addEventListener(MouseEvent.CLICK, replayTimerHandler);
/* add text then button to the display */
addChild(resetButtonTXT);
addChild(resetButton);
}
/* this is the func called on each repeatCount interval
so its called eleven times
once every 500 milliseconds */
private function doTheAnimation(event:TimerEvent):void
{
/* event.target is theTimeline timer
currentCount is how we get the current interval
we have placed it in a var named currentFrameOn
we use appendText to add each letter per interval */
var currentFrameOn:int = event.target.currentCount;
switch (currentFrameOn)
{
case 1:
theShowTyping.appendText("e");
break;
case 2:
theShowTyping.appendText("l");
break;
case 3:
theShowTyping.appendText("l");
break;
case 4:
theShowTyping.appendText("o");
break;
case 5:
theShowTyping.appendText(" ");
break;
case 6:
theShowTyping.appendText("W");
break;
case 7:
theShowTyping.appendText("o");
break;
case 8:
theShowTyping.appendText("r");
break;
case 9:
theShowTyping.appendText("l");
break;
case 10:
theShowTyping.appendText("d");
break;
case 11:
theShowTyping.appendText("!");
break;
}
}
/* the function below resets the timer
after clearing theShowTyping */
private function replayTimerHandler(event:MouseEvent):void
{
theTimeline.stop();theShowTyping.text = "H";
theTimeline.reset();
theTimeline.start();
}
}
}
|