Copy link to clipboard
Copied
package{
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.Sprite;
public class timer2 extends Sprite {
public function timer2() {
var count:Number = 60;
var myTimer:Timer = new Timer(1000,count);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();
function countdown(event:TimerEvent):void {
myTimer2.text = String((count)-myTimer.currentCount);
}
}
}
}
i am creating a timer and i have a error can somebody help me with this|???tnx in advance?
You need to import the class, ie:
import flash.text.TextFormat;
Copy link to clipboard
Copied
myTimer2.text
"myTimer2" is not defined in anywhere?
Copy link to clipboard
Copied
yes i also noticed it earlier but when i changed it to this codes
package{
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.Sprite;
import flash.text.TextField;
public class timer2 extends Sprite {
public function timer2() {
var count:Number = 60;
var myTimer:Timer = new Timer(1000,count);
var Timer2 : TextField = new myTimer2();
function countdown(event:TimerEvent):void {
Timer2.text = String((count)-myTimer.currentCount);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();
}
}
}
}
it says 1067: Implicit coercion of a value of type myTimer2 to an unrelated type flash.text:TextField.
what is the problem here?
Copy link to clipboard
Copied
var Timer2 : TextField = new myTimer2();
For this to work "myTimer2" needs to be a class extending TextField, but it does not appear to be.
Shall I give you an example of how to display a countdown in a Sprite?
Copy link to clipboard
Copied
sure tnx in advance!
it should be in package also huh..
Copy link to clipboard
Copied
package {
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.utils.Timer;
public class Countdown extends Sprite {
private var textfield:TextField;
public function Countdown():void {
var count:uint = 60;
var myTimer:Timer = new Timer(1000, count);
myTimer.addEventListener(TimerEvent.TIMER, countdown, false, 0, true);
myTimer.start();
textfield = new TextField();
textfield.text = String(count);
addChild(textfield);
}
private function countdown(e:TimerEvent):void {
textfield.text = String(parseInt(textfield.text) - 1);
}
}
}
Copy link to clipboard
Copied
tnx but how can i change the font style, font size and location of the text????tnx agen
Copy link to clipboard
Copied
To change the font style/size you can either use StyleSheet or TextFormat but the latter would be easier for this example. Just do something like:
textfield.defaultTextFormat = new TextFormat("Baghdad", 24, 0xff0000, true);
(NB. you need to embed fonts if you want to use non-device fonts.)
To set the position of the TextField just do:
textfield.x = 30;
textfield.y = 60;
Copy link to clipboard
Copied
where will i put the
textfield.defaultTextFormat = new TextFormat("Baghdad", 24, 0xff0000, true)
sorry im so dumb..
because i put it this way..
package {
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.utils.Timer;
public class timer2 extends MovieClip {
private var textfield:TextField;
public function timer2():void {
var count:uint = 60;
var myTimer:Timer = new Timer(1000, count);
myTimer.addEventListener(TimerEvent.TIMER, countdown, false, 0, true);
myTimer.start();
textfield = new TextField();
textfield.defaultTextFormat = new TextFormat("Arial", 24, 0xff0000, true);
textfield.x = 30;
textfield.y = 60;
textfield.text = String(count);
addChild(textfield);
}
private function countdown(e:TimerEvent):void {
textfield.text = String(parseInt(textfield.text) - 1);
}
}
}
and the error is this
1180: Call to a possibly undefined method TextFormat.
tnx again..
Copy link to clipboard
Copied
You need to import the class, ie:
import flash.text.TextFormat;
Copy link to clipboard
Copied
Thank u so much i trully helped...
and it works
Find more inspiration, events, and resources on the new Adobe Community
Explore Now