I'm creating app and I need to show game time MM:SS format. But I don't know why timer doesn't wrok It shows 0:00.359 (359 of miliseconds) and not change. Where is the problem? I can't find It. Thank you.
var timer:Timer; //import flash.utils.Timer;
var txtTime:TextField;
var tmpTime:Number; //this will store the time when the game is started
//your constructor:
public function MemoryGame()
{
timer = new Timer(1000); //create a new timer that ticks every second.
timer.addEventListener(TimerEvent.TIMER, tick, false, 0, true); //listen for the timer tick
txtTime = new TextField();
addChild(txtTime);
tmpTime = flash.utils.getTimer();
timer.start(); //start the timer
//....the rest of your code
}
private function tick(e:Event):void {
txtTime.text = showTimePassed(flash.utils.getTimer() - tmpTime);
}
//this function will format your time like a stopwatch
function showTimePassed(startTime:int):String {
var leadingZeroMS:String = ""; //how many leading 0's to put in front of the miliseconds
var leadingZeroS:String = ""; //how many leading 0's to put in front of the seconds
var time = getTimer() - startTime; //this gets the amount of miliseconds elapsed
var miliseconds = (time % 1000); // modulus (%) gives you the remainder after dividing,
if (miliseconds < 10) { //if less than two digits, add a leading 0
leadingZeroMS = "0";
}
var seconds = Math.floor((time / 1000) % 60); //this gets the amount of seconds
if (seconds < 10) { //if seconds are less than two digits, add the leading zero
leadingZeroS = "0";
}
var minutes = Math.floor( (time / (60 * 1000) ) ); //60 seconds times 1000 miliseocnds gets the minutes
return minutes + ":" + leadingZeroS + seconds + "." + leadingZeroMS + miliseconds;
}
//in your you-win block of code:
var score = flash.utils.getTimer() - tmpTime; //this store how many milliseconds it took them to complete the game.
Try
timer.currentCount
instead of
flash.utils.getTimer()
It will return the number of times the timer has fired the TIMER-Event.
You need not do this.
var time = getTimer() - startTime;
In your code, startTime is already time elapsed due to
showTimePassed(flash.utils.getTimer() - tmpTime);
OR
you can call
showTimePassed();
and change time calculation as
var time = getTimer() - tmpTime;
Related
I have the following count up code, but am not sure how I would be able to include an if else statement to have the count-up stop at 15 seconds for example.
Here is the code:
var timer:Timer = new Timer(100);
timer.start();
timer.addEventListener(TimerEvent.TIMER, timerTickHandler);
var timerCount:int = 0;
function timerTickHandler(Event:TimerEvent):void{
timerCount += 100;
toTimeCode(timerCount);
}
function toTimeCode(milliseconds:int) : void {
//create a date object using the elapsed milliseconds
var time:Date = new Date(milliseconds);
//define minutes/seconds/mseconds
var minutes:String = String(time.minutes);
var seconds:String = String(time.seconds);
var miliseconds:String = String(Math.round(time.milliseconds)/100);
//add zero if neccecary, for example: 2:3.5 becomes 02:03.5
minutes = (minutes.length != 2) ? '0'+minutes : minutes;
seconds = (seconds.length != 2) ? '0'+seconds : seconds;
//display elapsed time on in a textfield on stage
timer_txt.text = minutes + ":" + seconds+"." + miliseconds;
}
First, for efficiency, you can use the timers built in currentCount property to know how much time has elapsed (instead of making a timerCount var to that end)
To stop the timer after 15 seconds, simply set the appropriate repeat count so it ends at 15 seconds, or stop it in the tick handler after 15 seconds have past:
var timer:Timer = new Timer(100,150); //adding the second parameter (repeat count), will make the timer run 150 times, which at 100ms will be 15 seconds.
timer.start();
timer.addEventListener(TimerEvent.TIMER, timerTickHandler);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerFinished); //if you want to call a function when all done
function timerTickHandler(Event:TimerEvent):void{
toTimeCode(timer.delay * timer.currentCount); //the gives you the amount of time past
//if you weren't using the TIMER_COMPLETE listener and a repeat count of 150, you can do this:
if(timer.delay * timer.currentCount >= 15000){
timer.stop();
//do something now that your timer is done
}
}
Im doing a little game in actionscript, and i have a timer that starts when the gamer starts and end when the game ends.
But i want to show in a textfield the value of my timer during the game like 1,2,3,4,5,6, etc.
How can i can acess that timer propriety to get the value?
My timer code is this:
startTime = (new Date().time);
endTime = (new Date().time);
public function testTime():void
{
const 5_min = 5 * 60 * 1000;
const 2_min = 2 * 60 * 1000;
var timeDiff:Number = endTime - startTime;
if (timeDiff < 2_min) {
trace("Good!");
} else {
trace("Bad!");
}
}
And here (above) i create a txt field. Now, how i can show the timer value while playing the game?
var timer_txt:TextField;
pontuacao = new TextField();
timeDiff = 0;
timer_txt = new TextField();
timer_txt.text = String(timeDiff);
stage.addChild(timer_txt);
timer_txt.x = 470;
timer_txt.y = 320;
Well, first of all, in AS you cannot begin your variable names with a number, therefore this code should absolutely not compile (5_min, 2_min = WRONG!).
You have your startTime, which is ok. Now you will have to check the current time in some kind of event (Timer, EnterFrame) or interval (setInterval) and update the endTime value accordingly. Once you have it, you just count the difference and divide it by 1000 (I assume you don't want to show it in millis)
Something like this:
import flash.utils.setInterval;
var s:Number = new Date().getTime(); //start time
var e:Number; //end time
var d:Number; //difference
setInterval(function () {
e = new Date().getTime();
var oldDiff:Number = d;
d = int((e - s) / 1000);
if(oldDiff != d) trace(d + " seconds since launch");
},
100
);
I'm using AS3 to pull XML data, one field is a time field in the XML and displays an hour. I'm getting the date and then the AS3 loads the proper node based on teh time set in the XML. This is all working perfect - I have two times as variables, one is the system time (which is set in UTC time) the other is one hour ahead.
The two variables are currentHour and newHour - it's doing everything I want however I'd like to now create a countdown between these two hours and display time remaining in the minutes.
Here is the complete code for that.
Get the time from XML using AS3
These seems straight forward but I'm having a hard time. I've tried this:
var data:Array = [currentHour, newHour];
var aDate:String = data[0].split(" ")[0];
var dateElements:Array = aDate.split("-");
var date1:Date = new Date();
date1.setMinutes(int(data[0].split(" ")[1].split(":")[0]));
dateElements = data[1].split(" ")[0].split("-");
var date2:Date = new Date();
date2.setMinutes(int(data[1].split(" ")[1]));
var elapse:Number = date2.getTime() - date1.getTime();
trace("minutes: " + date2.getMinutes());
But that isn't right, so I tried this:
if(currentHour < newHour)
{
var dayDiff:Number = newHour-currentHour;
// make sure it’s in the future
if (dayDiff > 0)
{
var seconds:Number = dayDiff / 1000;
var minutes:Number = seconds / 60;
}
trace(minutes, seconds);
}
If someone could help me get unstuck that would be amazing. Thank you!
Created a new answer which didn't contain the spam from previous one where I tried to figure out what the actual issue was and what the expectations of the program was. Anyways to summarize:
User wanted to, given a specific date/time, find out how long until the next complete hour, so for instance given the time 4:47pm wanted to find out how many minutes and seconds left until 5:00pm.
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.globalization.NumberFormatter;
import flash.utils.getTimer;
import flash.utils.Timer;
var timer:Timer = new Timer(250);
var my_date:Date = new Date();
var targetDate:Date = new Date();
//calculate how many total milliseconds left until next whole hour since that is how as3 is using Date-objects
var timeUntilWholeHourMS:Number = (60 - my_date.getMinutes()) * 60 * 1000;
targetDate.setTime(my_date.getTime() + timeUntilWholeHourMS);
//clear the "second and milliseconds" part of the new time since we want the whole hours
targetDate.setSeconds(0, 0);
var secondsLeft:Number = (targetDate.time - new Date().time) / 1000;
//make sure it is in the future
if (secondsLeft > 0) {
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();
}
function onTimer(e:flash.events.TimerEvent):void {
secondsLeft = (targetDate.time - new Date().time)/1000;
if (secondsLeft > 0) {
var minutes:Number = Math.floor(secondsLeft/60);
var seconds:Number = Math.floor(secondsLeft%60);
trace("minutes left: " + minutes, ", seconds left: " + seconds);
} else {
//time limit reached
timer.removeEventListener(TimerEvent.TIMER, onTimer);
timer.stop();
trace("Time limit reached!");
}
}
I'd reccomend you using Timer. At first declare a variable seconds (between currentHour and newHour):
private var seconds:int;
Then assign it value
seconds = (newHour - currentHour) * 3600;
Then declare a timer which will tick every second (second parameter tells how much times will it tick):
var timer:Timer = new Timer(1000, seconds)
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
And then create timerHandler that will make all necessary updates:
function func(e:TimerEvent):void {
--seconds;
//if you have some textfield that shows minutes left, update it's text here
//timeTextfieldText.text = int(seconds / 60) + " minutes left";
trace(int(seconds / 60));
}
A bit unclear on how you want to present the data, but this is how to get it working at least. You need to replace "currentHour & newHour" with actual hours. And then handle the cases inside onTimer with approriate code.
The thing that differentiates this code towards the other solutions are that this takes a timestamp when you start and then whenever a timer event occurs it will check the current time against that timestamp. Meaning it doesn't matter if the flash timer is off by a couple of milliseconds etc.
import flash.events.TimerEvent;
import flash.globalization.NumberFormatter;
import flash.utils.getTimer;
import flash.utils.Timer;
var timer:Timer = new Timer(250);
var currentHour:Number = 14;
var newHour:Number = 15;
var totalSeconds:Number = 0;
var timestampStart:Number = 0;
if(currentHour < newHour) {
totalSeconds = (newHour - currentHour) * 3600;
if (totalSeconds > 0) {
timestampStart = getTimer();
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();
}
}
function onTimer(e:flash.events.TimerEvent):void {
var secondsRunning:Number = (getTimer() - timestampStart) / 1000;
var secondsLeft:Number = totalSeconds - secondsRunning;
if (secondsLeft > 0) {
var minutes:Number = Math.floor(secondsLeft/60);
var seconds:Number = Math.floor(secondsLeft%60);
trace("minutes left: " + minutes, ", seconds left: " + seconds);
} else {
//time limit reached
timer.removeEventListener(TimerEvent.TIMER, onTimer);
timer.stop();
trace("Time limit reached!");
}
}
You need to add an enterFrame event hanler
private var frameCount:int = 0;
private var diff:int = 3600;//the seconds between the two hours, you could set it here
this.addEventListener(Event.ENTER_FRAME, handler);
private function handler(event:Event):void {
frameCount++;
if (frameCount >= stage.frameRate) {
frameCount = 0;
diff--;
if (diff < 0) {
this.removeEventListener(Event.ENTER_FRAME, handler);
}
}
var minutes:int = diff/60;
}
I have tried following code and its working but how do i stop when its reach 130 ?
var textValue:Number = 67.1;
var addValue:Number = .1;
my_txt.text = textValue.toString();
function counter(){
textValue += addValue;
my_txt.text = textValue.toString();
}
setInterval(counter, 10);
setInterval returns a unique ID as an unsigned int (uint). You can use clearInterval with this ID to stop the interval. The code:
var textValue:Number = 67.1;
var addValue:Number = .1;
var myInterval:uint;
function counter(){
textValue += addValue;
my_txt.text = textValue.toString();
if( textValue >= 130 ) {
clearInterval(myInterval);
}
}
myInterval = setInterval( counter, 10 );
You can stop an interval by using clearInterval. Try this:
var textValue:Number = 67.1;
var addValue:Number = .1;
my_txt.text = textValue.toString();
function counter(){
textValue += addValue;
my_txt.text = textValue.toString();
//check for end value
if (textValue>=130)
{
//clear the interval
clearInterval(intervalID);
}
}
//store the interval id for later
var intervalID:uint = setInterval(counter, 10);
Since it seems like you may be using actionscript 3, I suggest not using an interval at all. A Timer object may be better as it can offer better control, such being able to set the number of times it fires off before stopping itself and being able to easily start, stop, and restart the timer as needed.
Example of using a Timer object and adding an event listener for each tick
import flash.utils.Timer;
import flash.events.TimerEvent;
// each tick delay is set to 1000ms and it'll repeat 12 times
var timer:Timer = new Timer(1000, 12);
function timerTick(inputEvent:TimerEvent):void {
trace("timer ticked");
// some timer properties that can be accessed (at any time)
trace(timer.delay); // the tick delay, editable during a tick
trace(timer.repeatCount); // repeat count, editable during a tick
trace(timer.currentCount); // current timer tick count;
trace(timer.running); // a boolean to show if it is running or not
}
timer.addEventListener(TimerEvent.TIMER, timerTick, false, 0, true);
Controlling the timer:
timer.start(); // start the timer
timer.stop(); // stop the timer
timer.reset(); // resets the timer
Two events it throws:
TimerEvent.TIMER // occurs when one 'tick' of the timer has gone (1000 ms in the example)
TimerEvent.TIMER_COMPLETE // occurs when all ticks of the timer have gone (when each tick has happened 11 times in the example)
API Documentation: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Timer.html
In my case, the timer I make doesn't reduce its time whenever a function is called. What code will I change or add in order to reduce the time in my timer?
Timer code:
var count:Number = 1200;
var lessTime:Number = 180;
var totalSecondsLeft:Number = 0;
var timer:Timer = new Timer(1000, count);
timer.addEventListener(TimerEvent.TIMER, countdown);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timesup);
function countdown(event:TimerEvent) {
totalSecondsLeft = count - timer.currentCount;
this.mainmc.time_txt.text = timeFormat(totalSecondsLeft);
}
function timeFormat(seconds:int):String {
var minutes:int;
var sMinutes:String;
var sSeconds:String;
if(seconds > 59) {
minutes = Math.floor(seconds / 60);
sMinutes = String(minutes);
sSeconds = String(seconds % 60);
} else {
sMinutes = "";
sSeconds = String(seconds);
}
if(sSeconds.length == 1) {
sSeconds = "0" + sSeconds;
}
return sMinutes + ":" + sSeconds;
}
function timesup(e:TimerEvent):void {
gotoAndPlay(14);
}
At this point the timer.start(); is placed on a frame so that the timer starts as it enters the frame.
The delay property on Timer is what you are looking for. In your handler, change the timer's delay:
function countdown(event:TimerEvent)
{
totalSecondsLeft = count - timer.currentCount;
this.mainmc.time_txt.text = timeFormat(totalSecondsLeft);
//change the timer delay
timer.delay -= lessTime;
}
I assumed by your code sample that you wanted to subtract lessTime from the timer delay on each timer interval. If you want to change the delay to something else, then just adjust the code accordingly.
UPDATE
The above code is for decreasing the interval (delay) between each timer fire. If what you'd like to do instead is decrease the the amount of intervals (repeatCount) it takes for the timer to reach TIMER_COMPLETE, then you want to change the repeatCount property on Timer:
//set the timer fire interval to 1 second (1000 milliseconds)
//and the total timer time to 1200 seconds (1200 repeatCount)
var timer:Timer = new Timer(1000, 1200);
//reduce the overall timer length by 3 minutes
timer.repeatCount -= 300;
ANOTHER UPDATE
Keep in mind that when you alter the repeatCount, it doesn't affect the currentCount. Since you are using a separate count variable and timer.currentCount to calculate the displayed time remaining, it doesn't look like anything is changing. It actually is though - the timer will complete before the displayed time counts down to zero. To keep your time left display accurate, make sure to subtract the same amount from count as you are from repeatCount:
timer.repeatCount -= 300;
count -= 300;