Hi,
Very helpful best practices information, however, as previously
stated, this is my first Class. The custom greeting still returns
undefined. It's probably how I call them in the class instance
constructor?
import com.bushidodeep.TimeStamp;
//
var my_ts:TimeStamp = new TimeStamp(this, 0, 0, "Top of The Morning",
"Good Night Gracie");
On Jan 21, 2006, at 1:19 PM, Nathan Derksen wrote:
A few points: First, you are not actually setting the amMessage and
pmMessage properties anywhere. The variables that you are passing
through the constructor actually obscure those two properties
because they share the same variable name; they don't copy any
data. Second, it's a better idea to make the properties private,
and use getters and setters as is shown below. You still access the
properties in the same way (classInstance.amMessage = "foo"), but
it makes a function call instead. That way, if you need to do
something else in response to the changed property, you can easily
add it without affecting the API. Third, it's good to name private
properties using a naming convention that allows you to easily
identify in your code the fact that it is a class property. I
prepend a "p" to the variable name, others like prepending a "_"
character. Do whatever you want, just use it consistently.
class com.bushidodeep.TimeStamp {
private var now:Date;
private var pAMMessage:String;
private var pPMMessage:String;
// Movie clip that will contain visual
// elements of the hello.
private var container_mc:MovieClip;
//
//
public function TimeStamp(target:MovieClip, x:Number, y:Number,
amMessage:String, pmMessage:String) {
init(target, x, y, amMessage, pmMessage);
}
private function init(target:MovieClip, x:Number, y:Number,
amMessage:String, pmMessage:String):Void {
now = new Date();
pAMMessage = amMessage;
pPMMessage = pmMessage;
container_mc = target.createEmptyMovieClip("blah", 1);
container_mc._x = x;
container_mc._y = y;
container_mc.createTextField("messageText", 0, 0, 0, 400, 25);
container_mc.messageText.setNewTextFormat(createFormat());
container_mc.messageText.text = createGreeting();
}
private function createGreeting():String {
var greets:String = now.getHours()<12 ? pAMMessage : pPMMessage;
var fullGreeting:String = "The time is now
"+formatTime(now)+greets;
return fullGreeting;
}
private function createFormat():TextFormat {
var messageFormat:TextFormat = new TextFormat();
messageFormat.font = "Verdana";
messageFormat.color = 0xffffff;
messageFormat.bold = true;
return messageFormat;
}
private function formatTime(theDate:Date):String {
var hour:Number = theDate.getHours();
var minute:String = theDate.getMinutes()>9 ? theDate.getMinutes
().toString() : "0"+theDate.getMinutes();
var timeString:String = hour>12 ? (hour-12)+":"+minute+"PM." :
hour+":"+minute+"AM.";
return timeString;
}
public function set amMessage(message:String):Void
{
pAMMessage = message;
}
public function get amMessage():String
{
return pAMMessage;
}
public function set pmMessage(message:String):Void
{
pPMMessage = message;
}
public function get pmMessage():String
{
return pPMMessage;
}
}
Nathan
http://www.nathanderksen.com
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders