How can I take a simple counter and I want to load the current count into a textbox?
How can I take a simple counter and I want to load the current count into a textbox?
My code:
count1 = new Timer(count);
count1.addEventListener(TimerEvent.TIMER,stopWatch);
count1.start();
pri开发者_Go百科vate function stopWatch(event:TimerEvent):void{
var myTextBox:TextField = new TextField();
myTextBox.text = count1.currentCount;
addChild(myTextBox);
}
Resulting error:
1067: Implicit coercion of a value of type int to an unrelated type String.
You are getting this error because you are assigning an int
to the .text
field. It will only accept a string
and will not implicitly cast it. Use the toString
method of int
supplied to you by the base Object
class.
myTextBox.text = count1.currentCount.toString();
See also, this answer on the same subject.
Update, regarding comment:
I would avoid using addChild
when the event fires. Assuming stopWatch
is called more than once, the posted code will add a new TextField
every time. Instead, just create it once, but set the .text
field every time the event fires.
// Create text box to show timer output
var myTextBox:TextField = new TextField();
addChild(myTextBox);
// Create timer and start
var count1 = new Timer(count);
count1.addEventListener(TimerEvent.TIMER,stopWatch);
count1.start();
// Update text box whenever TIMER event fires
private function stopWatch(event:TimerEvent):void{
myTextBox.text = count1.currentCount.toString();
}
Have you tried converting your int
to a string? Something like this:
myTextBox.text = String(count1.currentCount);
See if that works?
精彩评论