Skip to main content
Participant
March 4, 2008
Question

AS2 - 3 Migration

  • March 4, 2008
  • 3 replies
  • 457 views
Hi, I am new to Flash CS3 so sorry if this is an easy question, I found a nice easy example of making a digital clock online, I followed the example and everything worked using AS2 but when I tried to do the same in AS3 the _root.onEnterFrame = function() { at the top of my code did not work. I read in the help files that this has been changed so I have tried to change this to work but I have not been successful. Could anyone tell me what the top line of code should be to get this working as I am unable to get it right.

Thanks
Mark
This topic has been closed for replies.

3 replies

urz17Author
Participant
March 10, 2008
Thanks David for your reply, it has helped me understand it a lot better. I'll also have a look at your book and the new one.

Cheers
Mark
Inspiring
March 4, 2008
Woops! I copy/pasted that last bit wrong.

If you paste exactly what I typed in the named-function version, you'll
get an error. Change these last few lines:


time_txt.text = myHours + ":" + myMinutes + ":" + mySeconds;
}
};

... to these:


time_txt.text = myHours + ":" + myMinutes + ":" + mySeconds;
};

(Get rid of that one stray } character.)


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."


Inspiring
March 4, 2008
Hi,

Only David Stiller could make the above look so easy; David I purchased your Book "Foundation Flash CS3 for Designers" a DAMN GOOD BOOK. Beginners in AS3 should purchase this great resource.

Hmm. You've obtained a two wheel bike by now, surely? ;)

Kind Regards,

Boxing Boom
Inspiring
March 4, 2008
Mark,

> I found a nice easy example of making a digital clock online,
> I followed the example and everything worked using AS2
> but when I tried to do the same in AS3 the _root.onEnterFrame
> = function() { at the top of my code did not work.

That's part of the change needed, but there's actually a bit more, too.
Fortunately, the strict nature of AS3 really encourages you (meaning anyone)
to think through all the bits and pieces of a block of code. I find that
AS3 has helped me become a better programmer just because it isn't as
forgiving as AS2 was.

The structure of event handlers in AS3 -- almost across the board -- is
easy enough to grasp. Check it out:

objectReference.addEventListener(someEvent, someFunction);

In this case, objectReference refers to some object for which you're
handling an event. You were using _root before, and AS3 no longer refers to
the same entity with the word "_root". As it turns out, you didn't
especially neet "_root" in AS2 either. Assuming your code was in a frame of
the main timeline, you could have used "this" (without the quotes) or even
simply nothing, as the object the code was *in* (the main timeline) would be
understood. The someEvent part means the event you're listening for, and
the someFunction part refers to the function to perform when that event
occurs. You could either use an anonymous function (like you were using) or
a named function. I'll show you both.

addEventListener(
Event.ENTER_FRAME,
function(evt:Event):void {

var myDate:Date = new Date();
var myHours:String = myDate.getHours().toString();
var myMinutes:String = myDate.getMinutes().toString();
var mySeconds:String = myDate.getSeconds().toString();

if (myHours.length < 2) {
myHours = "0" + myHours;
}

if (myMinutes.length < 2) {
myMinutes = "0" + myMinutes;
}

if (mySeconds.length < 2) {
mySeconds = "0" + mySeconds;
}

time_txt.text = myHours + ":" + myMinutes + ":" + mySeconds;
}
);

So as you can see, the addEventListener() method is used to assign an
anonymous function to the Event.ENTER_FRAME event. The addEventListener()
method belongs to the EventDispatcher class, so any class that inherits from
EventDispatcher is capable of using addEventListener() -- and that includes
movie clips (the MovieClip class), text fields (TextField), buttons
(SimpleButton), and tons more. In fact, if you look up EventDispatcher in
the ActionScript 3.0 Language and Components reference, you'll be able to
see what it's family tree is, and you can click through the descendents
until you find objects you're more familiar with, like movie clips and
buttons. Alternately, you can look up the class entries for movie clips and
buttons, and you'll see EventDispatcher as an earler ancestor for both of
those objects.

In the above, the same myDate variable holds an instance of the myDate
class. The major difference here -- besides the new way of handling
events -- is that I went with strings for the myHours, myMinutes, etc.
variables. Originally, they're numbers. That's what the Date.getHours()
method returns, for example. In AS2, you could do something like this ...

myHours = "0" + myHours;

... and not get dinged (I've used variations on the above code tons of
times!) -- and the problem is, really, you're converting the numeric myHours
variable into a string with that line, because you're concatenating the
numeric value of myHours with a string of the numeral zero. ActionScript
3.0 balks at that, and really, fair is fair. myHours *isn't* a string,
after all, so it's a bit dicey to treat it like one.

So ... in this AS3 translation, I just opted to convert those numbers to
strings in the first place. Because they're strings, you can't make numeric
comparisons on them like ...

if (myHours < 10) { ...

... because in the stricter AS3, an attempt of comparison gives you the
warning, "Hey, bro, myHours is a string! You can't compare a string to a
number!" (I paraphrase, of course). So instead, I opted to invoke the
String.length property on each string variable to see if it has less than
two characters. This comparison amounts to the same thing.

Finally, everything is assigned to the TextField.text property of your
time_txt instance.

If this were a named function, it might look like this:

addEventListener(Event.ENTER_FRAME, enterFrameHandler);

function enterFrameHandler(evt:Event):void {
var myDate:Date = new Date();
var myHours:String = myDate.getHours().toString();
var myMinutes:String = myDate.getMinutes().toString();
var mySeconds:String = myDate.getSeconds().toString();

if (myHours.length < 2) {
myHours = "0" + myHours;
}

if (myMinutes.length < 2) {
myMinutes = "0" + myMinutes;
}

if (mySeconds.length < 2) {
mySeconds = "0" + mySeconds;
}

time_txt.text = myHours + ":" + myMinutes + ":" + mySeconds;
}
};


David Stiller
Co-author, Foundation Flash CS3 for Designers
http://tinyurl.com/2k29mj
"Luck is the residue of good design."