AS3 seeing every score - actionscript-3

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;
}

Related

Loading images using the URLRequest

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

Making health reduce by 1 every second

I'm programming a tamagotci style game and I need to health by one, every second. I already have a score number in place that is changed when you feed the target. I would be incredibly grateful if someone could give me a hand in editing the current code I have at the moment, to reduce health by 1 every second. Here is my code if anyone needs to see it;
health=100
txtform.font = "Arial";
txtform.color = 0x000000;
txtform.size = 24;
txtform.bold = true;
healthdisplay.defaultTextFormat=txtform;
addChild(healthdisplay);
healthdisplay.text=("Health: " + health)
healthdisplay.x=75
healthdisplay.y=-20
healthdisplay.autoSize=TextFieldAutoSize.CENTER;
}
public function IncreaseHealth(points:int){
health+=points
healthdisplay.text=("Health: " + health)
healthdisplay.defaultTextFormat=txtform;
}
Make a function DecreaseHealth() that you call every second. Something like:
var timer:Timer = new Timer(1000); // 1 second = 1000 milliseconds.
timer.addEventListener(TimerEvent.TIMER, DecreaseHealth);
timer.start();
Where DecreaseHealth() has the following signature:
function DecreaseHealth(event:TimerEvent):void
{
health -= 1;
// Do printing or whatever else here.
}

AS3 Timer - Add zero to single digit countdown, change font colour

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.

Why can't I addChild() in actionscript 3?

probably a really easy question but I've been trying to figure this out for a while...
I'm trying to add a child to the stage to represent the game score like so:
var gameScoreField:TextField = new TextField();
var gameScore:int = 0;
public function Game()
{
// add score
gameScoreField.text = "Score: " + gameScore;
gameScoreField.embedFonts = true;
var format:TextFormat = new TextFormat();
format.font = "Tahoma";
gameScoreField.setTextFormat(format);
addChild(gameScoreField);
}
But when I run the game it isn't showing on the stage. I have no errors and no warnings.
Why is this? Thanks for your help!
Public courtesy to get this off of the unanswered list. Asker or answerer are welcome to post their own answer instead of accepting this one, if they want.
The asker needed to embed the Tahoma font, or set embedFonts to false.

Score system based on time NAN

I've created a score system in my flash Quiz game where the faster you answer a question, the more points you get. At the moment however my tracer shows 'NAN' when I run my game. Can anybody see why this is?
var score:int = 0;
var count:int = 0;
var mTimer:Timer;
mTimer = new Timer(100, 70);
function processScore():void {
var count:int = mTimer.currentCount;
var score:int = score + (700 - (count * 10));
trace("score registered");
}
trace(aUserAnswers[numLoops] + " " + returnedNumber);
if(aUserAnswers[numLoops] == returnedNumber){
processScore();
}
returnedNumber is when a button is clicked, if the number matches that which is in the array, the question is correct.
Thank you
You're redeclaring count and score inside processScore(). That makes them local variables to the function, unrelated to the previous declared variables of the same name. This means that their values are lost when the function finishes and the previous variables are unchanged. I'm guessing that at some point you divide something by one of them and since you'll always be dividing by zero you get NAN.