I need help with a timer. I’d like to create a bomb-like digital countdown timer for a game.
Using a digital font
always double digits e.g. 10, 09...01, 00 (to look like a bomb timer).
And finally during the last few seconds, turning the font red to increase the drama.
What I currently have below is a basic countdown timer, 20-0. The countdown variable starts at 20, is reduced by one every 1000ms and this number is shown in the text field.
But the font is generic, once the count gets below ten the numbers don’t have a zero in front of them, and I have no idea how to change the font colour in the final seconds.
public class UsingATimer extends Sprite
{
//The Timer object
public var timer:Timer= new Timer(1000, countdown);
public var countdown:Number = 20;
//The Text objects
public var output:TextField = new TextField();
public var format:TextFormat = new TextFormat();
public function UsingATimer()
{
//Initialize the timer
output.text = countdown.toString();
timer.addEventListener(TimerEvent.TIMER, countdownHandler);
timer.start();
//Set the text format object
format.font = "Helvetica";
format.size = 200;
format.color = 0x000000;
format.align = TextFormatAlign.RIGHT;
//Configure the output text field
output.defaultTextFormat = format;
output.autoSize = TextFieldAutoSize.RIGHT;
output.border = false;
output.text = "20";
//Display and position the output text field
stage.addChild(output);
output.x = 200;
output.y = 100;
}
public function countdownHandler(event:TimerEvent):void
{
countdown--;
output.text = countdown.toString();
}
}
If there are no basic digital fonts I’ll have to embed one, which I should be okay with but any help with the other two problems would be greatly appreciated.
Sorry if the code is all over the place, I’m a beginner.
Thanks for your time.
I think you've mostly got it already. What you can do is inside your countdown handler, just check if the value is less then 10, if so append a 0 in front before displaying.
countdown--;
if(countdown < 10){
output.text = "0" + countdown.toString();
} else {
output.text = countdown.toString();
}
Then to change the colour, it would be the same logic, check for what number you want it to change colour on, then change the colour in your TextFormat object and apply it to your TextField.
For a digital font, you could search for and embed one in to flash, but if all you need are numbers/limited characters, you could also make one as well with movie clips/graphics since they are relatively straight forward. Depends on how fancy you need it I guess as well as how flexible.
Related
Im new to ActionScript 3 and am trying to get a number to deplete when i start a timer
So far ive got it so you click the button 'btngo' and a timer begins whilst the ball object moves.
I also want a dynamic text that starts at 100 to deplete down to 0 in 10 seconds when this button is clicked, but unfortunately i have no clue how to do it
Any help will be greatly appreciated, thank you :)
import flash.utils.Timer;
import flash.events.TimerEvent;
stop();
var timer1:Timer = new Timer (10,10000);
var timer2:Timer = new Timer (10,10000);
timer1.addEventListener(TimerEvent.TIMER, forward);
timer2.addEventListener(TimerEvent.TIMER, back);
btngo.addEventListener(MouseEvent.CLICK, green);
btnstop.addEventListener(MouseEvent.CLICK, red);
function green(e:MouseEvent):void {
timer2.stop();
timer1.start();
trace("timer started");
}
function red(e:MouseEvent):void {
timer1.stop();
timer2.start();
trace("timer started");
}
function forward(e:TimerEvent):void {
ball.x += 2;
}
function back(e:TimerEvent):void {
ball.x -= 2;
}
Well, here are two functions which will get you what you are looking for. This first one is the creation of the text:
import flash.text.TextField;
import flash.text.TextFieldType;
var dynamic_text:TextField;
function CreateText()
{
// You can also replace this part with code that creates a textfield that you have in your library,
// in that case just make sure the textfield is set to dynamic in the textfield properties
// Create a new textfield.
dynamic_text = new TextField();
// Make sure its type is set to Dynamic.
dynamic_text.type = TextFieldType.DYNAMIC;
// Position it somewhere on the screen.
dynamic_text.x = 10;
dynamic_text.y = 10;
// Set a default text for now.
dynamic_text.text = "Time: " + time_left;
// Make sure the players cannot select the text.
dynamic_text.selectable = false;
// Add it to your stage (or other parent, take your pick)
stage.addChild(dynamic_text);
}
This second one is called every TimerEvent:
function OnTimerChange()
{
// Update the text based on the new time.
dynamic_text.text = "Time: " + time_left;
}
You will have to call this OnTimerChange function inside your own "forward" and "backward" functions, and in those two functions you will have to calculate how much time has passed.
For an example how to keep track of time that has passed since you pressed a button:
actionscript 3 how to keep track of time elapsed?
Hope this helped.
recently started to learn ActionScript 3 and already have questions.
question remains the same: I'm uploading a picture using the object Loader.load (URLRequest). Loaded and displayed a picture normally. But it is impossible to read the values of attributes of the image height and width, instead issued zero. That is, do this:
var loader:Loader=new Loader();
var urlR:URLRequest=new URLRequest("Image.jpg");
public function main()
{
loader.load(urlR);
var h:Number = loader.height;// here instead of the width of the image of h is set to 0
// And if you do like this:
DrawText(loader.height.toString(10), 50, 50); // Function which draws the text as defined below
// 256 is displayed, as necessary
}
private function DrawText(text:String, x:int, y:int):void
{
var txt:TextField = new TextField();
txt.autoSize = TextFieldAutoSize.LEFT;
txt.background = true;
txt.border = true;
txt.backgroundColor = 0xff000000;
var tFor:TextFormat = new TextFormat();
tFor.font = "Charlemagne Std";
tFor.color = 0xff00ff00;
tFor.size = 20;
txt.x = x;
txt.y = y;
txt.text = text;
txt.setTextFormat(tFor);
addChild(txt);
}
Maybe attribute values must be obtained through the special features, but in the book K.Muka "ActionScript 3 for fash" says that it is necessary to do so. Please help me to solve this. Thanks in advance.
Well it's simple.
Flash is focused on the Internet, hence such problems.
If you wrote loader.load (urlR); it does not mean loaded. Accordingly, prior to the event confirming the end of loading, in loadare Null
if, instead of certain functions would be more code that would perhaps tripped your approach.
Yeah plus still highly dependent on the size of the file that you read.
Well, in general it all lyrics. Listen event on loader.contentLoaderInfo.addEventListener (Event.INIT, _onEvent), onEvent and read properties.
You need to wait for your image to load to be able to get values out of it.
Attach an eventListener to your URLLoader.
var urlR:URLRequest = new URLRequest("Image.jpg");
loader.load(urlR);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
function loader_complete(e:Event): void {
// Here you can get the height and width values etc.
var target_mc:Loader = evt.currentTarget.loader as Loader;
// target_mc.height , target_mc.width
}
Loader
Flash/AS3 noobie here.
I'm attempting to display text letter-by-letter (which is working great). However, I want the animation to delay ~500 milliseconds each time a period/end of sentence is encountered. So far the relevant part of my code looks like this:
public function displayLoop(e:Event):void
{
if (pos == textToDisplay.length - 1)
{
stop();
return;
}
firstParagraph.appendText(textToDisplay.charAt(pos));
if (textToDisplay.charAt(pos) == String.fromCharCode(46))
{
//here's where I want to delay??
}
pos++;
}
In this case, firstParagraph is the name of my dynamic text object, textToDisplay is the String of text that is going to be displayed letter-by-letter, and pos is simply the position we're at when displaying the text, so we can keep track of it.
I'm guessing there's a simple solution to this problem, perhaps using a Timer EventHandler?
I appreciate any help anyone has to offer, thanks!
I think the following will be helpful for coding up what you want:
String.split() - This method will help you split up your paragraph into sentences and store them in an array. (Keep in mind that not all periods are full stops, so perhaps you will need to use some regular expressions to deal with special cases like when they are used as elipsis, or decimals.):
e.g.
textToDisplay.split('.');
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html#split()
Array.shift() - This method will return to you the first element in the array, and then remove it from the array. If you have your sentences stored in an array, you can keep calling shift() to get the next sentence that needs to be shown:
e.g.
var sentences:Array = textToDisplay('.');
var next_sentence:String = sentences.shift();
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#shift()
Timer - This object, like you mentioned, will help you create those delay intervals between appending the sentences:
e.g.
var myTimer:Timer = new Timer(1000, sentences.length);
myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
myTimer.start();
function timerHandler(e:Event) {
firstParagraph.appendText(sentences.shift());
}
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Timer.html
No need to track position since the timer has a counter built into it.
import flash.text.TextField;
import flash.utils.Timer;
import flash.events.TimerEvent;
var textToDisplay:String = 'AB.CDE.FGHI.JKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
var tf:TextField = new TextField()
tf.width = 500
tf.wordWrap = true
tf.height = 400
addChild(tf)
var timer:Timer = new Timer(100)
timer.addEventListener(TimerEvent.TIMER, onTimer)
timer.start()
function onTimer(e:TimerEvent):void{
timer.delay = 100
tf.appendText(textToDisplay.slice(timer.currentCount-1,timer.currentCount))
if(timer.currentCount == textToDisplay.length){
timer.stop()
}
if(textToDisplay.slice(timer.currentCount-1,timer.currentCount) == '.'){
timer.delay = 500
}
}
I have recently taken up programming and encountered a problem when it comes to score display. The score has not problem incrementing and displaying it is just that as the score updates it does not remove the last score. After a dozen frames I have a jumble of scores displayed. I have spent a few days google searching to see if I could find any type of answer but not seen a problem similar to this.
My Code:
public function balldistance(event:Event){ // function called on ENTER_FRAME in order to update the distance of the ball object
var txt:TextField = new TextField();
txt.text = "Distance: " + String(balldist);
txt.x = 25;
txt.y = 25;
addChild(txt);
trace(balldist); // I added this line in my code for troubleshooting purposes just so I could see the balldist augment.
balldist += Ball5.dx; // I am having the score(balldist) augment based on the distance the ball has traveled from its starting point.
}
A friend of mine suggested a removeChild(txt) but when i tried this it did not show the score updating.
Thank you
It looks like you are creating a new txt:TextField EVERY time ENTER_FRAME is triggered.
Try declaring/initializing it once, outside of that listener function:
var txt:TextField = new TextField();
txt.x = 25;
txt.y = 25;
addChild(txt);
Then on enter frame reference the SAME txt TextFeild instance, instead of making a new one over and over again:
public function balldistance(event:Event){
txt.text = "Distance: " + String(balldist);
balldist += Ball5.dx;
}
So I am creating a module and I have a screen that I need to be able to allow the users to write questions that they have on their screens in a text box. Does anyone know how to do this?
This is the basic setup that I use for every screen:
package screens
{
import flash.filters.*;
import flash.text.*;
import mapSystem.screenSystem.*;
import mapSystem.*;
import screens.*;
import caurina.transitions.Tweener;
public class screen4 extends screenBase
{
public function screen4(pSystem:mapManager)
{
super(pSystem);
numActions = 1;
}
public override function onAction()
{
if (actionStep == 1)
{
map.fID("54");
}
}
public override function onEnter()
{
map.zoomTo("full");
}
}
}
For users to input text, simply create a textfield and set its "type" property to TextFieldType.INPUT. When you go to retrieve this data, just access the textFields "text" prop.
Update -
Ok = simple google search on "AS3 textField tutorial", first hit was this tutorial, which I yanked and added a couple things to for you. Its fairly basic and well documented, so, depending on your level of experience, should prove illuminating.
//Creating the textfield object and naming it "myTextField"
var myTextField:TextField = new TextField();
//Here we add the new textfield instance to the stage with addchild()
addChild(myTextField);
//Here we define some properties for our text field, starting with giving it some text to contain.
//A width, x and y coordinates.
myTextField.text = "input text here";
myTextField.width = 250;
myTextField.x = 25;
myTextField.y = 25;
//#b99 addition
myTextField.type = TextFieldType.INPUT;
//This is the section for our text styling, first we create a TextFormat instance naming it myFormat
var myFormat:TextFormat = new TextFormat();
//Giving the format a hex decimal color code
myFormat.color = 0xAA0000;
//Adding some bigger text size
myFormat.size = 24;
//Last text style is to make it italic.
myFormat.italic = true;
//Now the most important thing for the textformat, we need to add it to the myTextField with setTextFormat.
myTextField.setTextFormat(myFormat);
Hope that helps!