PDA

View Full Version : My Clock is just overwriting itself


Halapino
01-08-2011, 10:14 PM
The clock displays, but the numbers overwrite themselves to create a blur. I cannot figure out how to get the text to refresh. On a side note, I cannot get the hours/minutes/seconds to get a zero added if they are under 10, keep getting told I can't combine a string with a number.


dateText.addEventListener(Event.ENTER_FRAME,showDa te);

private function showDate(event:Event):void{
var dText:TextField = new TextField();
var theDay:String = new String();
var theMonth:String = new String();
var theDate:Number = new Number();
var theYear:Number = new Number();
var theMinutes:Number = new Number();
var theHours:Number = new Number();
var theSeconds:Number = new Number();
var myDate:Date = new Date();
var ampm:String = new String();
//Retrieve the day, month and year from the date class.
var weekdays:Array = new Array ("Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday");
var months:Array = new Array ("Jan","Feb","Mar","Apr","May","Jun","Jul",
"Aug", "Sep", "Oct","Nov","Dec");
theDay=weekdays[myDate.getDay()];
theMonth=months[myDate.getMonth()];
theDate=myDate.getDate();
theYear=myDate.getFullYear();
theHours=myDate.getHours();
theMinutes=myDate.getMinutes();
theSeconds=myDate.getSeconds();
//put zeros where needed
//if (theHours<10) { theHours = "0" + theHours; } else { theHours = theHours; }
//if (theMinutes<10) {theMinutes = "0" + theMinutes;} else {theMinutes = theMinutes;}
//if (theSeconds<10) {theSeconds = "0" + theSeconds;} else {theSeconds = theSeconds;}
if (theHours>12 ) {theHours = theHours-12; ampm = "PM";} else
if (theHours == 12) {ampm = "PM";} else {ampm = "AM";}
if (theHours == 0) {theHours = 12;}
//Display the date in the dynamic text field.
dText.width = 350;
dText.height = 40;
dText.x = (stage.stageWidth)/2 - (dText.width)/2;
dText.y = 10;
dText.scaleY=2
// does not work! dText.text="";
dText.text=theDay+", "+theMonth+" "+theDate+", "+theYear+" - "+theHours+":"+theMinutes+":"+theSeconds+" "+ampm;
addChild(dText);
}

automator
01-09-2011, 12:59 PM
If you want to append a number to a string in order to display it in a text field, the number must be converted into a string. You can use the toString() method of the Number class. Ex:

var myNumber:Number = 2;
myTextfField.text = myNumber.toString();

If you need to convert a string to a number:

Number(String)

Halapino
01-09-2011, 01:34 PM
Yes, got the the zeros to show, thankyou.

Now just need to figure out how to refresh the time and not just overwrite it with a new time. I think it has something to do with adding it to the stage within the function itself, but I can't get it to write to the stage outside of the function.

automator
01-09-2011, 03:36 PM
Hmmmm... perhaps you shouldn't be using enter frame. Try a timer object instead:

tutorials101 [dot] blogspot [dot] com/2010/07/flash-actionscript-30-timer-class.html

Halapino
01-09-2011, 04:42 PM
It works now, not sure why, but I had to declare my time/date calculation variable outside the main function. I'm guessing it is then declared to all functions Private or Public. But I keep getting a warning saying that it is only visible to the inside this package. It works and I don't need it visible outside the package.

automator
01-09-2011, 06:17 PM
Yeah if you declare a variable inside of a function (called a local variable), that variable is only available within that function. Trying to access it from outside of a function or perhaps from a different function won't work. So if you have a variable that needs to be accessed by different functions, then you declare it outside.

Not sure about the other warning that you're getting though. Hopefully someone will step in to explain.

Halapino
01-10-2011, 12:54 AM
Browsing the web I noticed that people declared the variables as private or public (I'm not sure what that is or what the difference is) so I tried it out of curiosity and the warnings all went away.

I'm running good with no errors, warnings or problems. Thanks for your help and hopefully this can help some other newbie like myself in the future.