Reputation: 827
When i press keyboard down button the textfield is added to sprite but not through timer event call. Why is that ?
package
{
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.text.TextField;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Test extends Sprite
{
public function Test()
{
var timer:Timer = new Timer(3);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
timer.addEventListener(TimerEvent.TIMER, onTime);
timer.start();
}
function onKeyDown(event:KeyboardEvent):void {
// graphics.lineStyle(10,Math.random() * 10000, 10);
// graphics.drawCircle(100, 100, 80);
var txtFld:TextField = new TextField();
txtFld.x = 200;
txtFld.y = 200;
txtFld.width = 25;
txtFld.height = 15;
txtFld.text = "90";
addChild(txtFld);
}
function onTime(event:Timer):void {
var txtFld:TextField = new TextField();
txtFld.x = 100;
txtFld.y = 100;
txtFld.width = 25;
txtFld.height = 15;
txtFld.text = "80";
addChild(txtFld);
}
}
}
Upvotes: 0
Views: 139
Reputation: 15570
In the timer event handler, you've cast the incoming event object as Timer
, not TimerEvent
. This causes Flash player to throw an error.
Also, this code will continue to add TextFields because the Timer is looping and calling this function on every loop.
function onTime(event:TimerEvent):void {
//stop the timer
evt.target.stop();
var txtFld:TextField = new TextField();
txtFld.x = 100;
txtFld.y = 100;
txtFld.width = 25;
txtFld.height = 15;
txtFld.text = "80";
addChild(txtFld);
}
Upvotes: 1