In the post with 3D CountDown FIVe3D (see also How I Did It: write a countdown to Flash ), was proposed to create a class object in Actionscript CountDown, here's a similar version in JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| / ** * Class CountDown * * @ Author Giovambattista Fazioli * @ @ Email g.fazioli undolog.com * @ Web http://www.undolog.com * * @ Param dd (string) 'month day, year' * * / dd ) { function countdown (dd) { / / Init target time new Date ( dd ) ; var target = new Date (dd); = target. getTime ( ) ; this. targetTime = target. getTime ();
/ ** * Refresh countdown * / = function ( ) { this. refresh = function () { new Date ( ) ; var today = new Date (); today. getTime ( ) ; var today = currentTime. getTime (); / / Time left ( this . targetTime - currentTime ) ; this. _leftMilliseconds = (this. targetTime - currentTime); Math. floor ( this ._leftMilliseconds / 1000 ) ; this. _leftSeconds = Math. floor (this. _leftMilliseconds / 1000); Math. floor ( this ._leftSeconds / 60 ) ; this. _leftMinutes = Math. floor (this. _leftSeconds / 60); Math. floor ( this ._leftMinutes / 60 ) ; this. _leftHours = Math. floor (this. _leftMinutes / 60); / / No module = Math. floor ( this ._leftHours / 24 ) ; this. leftDays = Math. floor (this. _leftHours / 24); / / For print = this ._leftMilliseconds % 1000 ; this. leftMilliseconds = this. _leftMilliseconds% 1000; = this ._leftSeconds % 60 ; this. leftSeconds = this. _leftSeconds 60%; = this ._leftMinutes % 60 ; this. leftMinutes = this. _leftMinutes 60%; = this ._leftHours % 24 ; this. leftHours = this. _leftHours 24%; } ( ) ; this. refresh (); } |
Example
1 2 3
| new countDown ( '1 1, 2009' ) ; var cd = new CountDown (1 '1, 2009 '); / / Show how many days, hours, minutes, seconds and milliseconds to January 1, 2009 |
Continued ...
Here are some tips on how to write good code Object Oriented (OO) in Adobe Flash, especially for those still using version MX waiting to go to CS3.
Organize classes folders
First of all, the organization of classes makes the job significantly easier code maintenance. You can also create a real library you can reuse in other projects. Flash uses a classification related to the filesystem, and then organizing them into folders will also be reflected on the import of classes. For example, if we create the sequence of folders "mylibrary / graphics / plot" and insert our own ActionScript class "PlotClass.as", when we should use to import the class:
1
| grafica . plot . PlotClass ; mylibrary imports. graphics. plot. PlotClass; |
If the library (folder) "MyLibrary" is not in your movie folder or project, use the Flash publish settings to select the path:

Continued ...
When "triggered" an event of an object context, which is the parent object that represents the scope of all variables, becomes the object itself. This feature, typical of object-oriented programming, can be disorienting causing errors and bugs in the code. Furthermore, in certain situations, can prevent access to some variables "before" available.
First of all we see a feature of ActionScript related to context management which, if included, will save us a few hours of unnecessary tests. When we have to do with graphics or simply nested one inside the MovieClip, property _parent solve any problems in context. For example, if the MovieClip padre_mc It contains another MovieClip figlio_mc and use the following code:
1 2 3 4 5
| / / Code inside padre_mc ( ) { figlio_mc. onRelease = function () { this ) ; // figlio_mc trace (this); / / figlio_mc this . _parent ) ; // padre_mc trace (this. _parent); / / padre_mc } |
Risalisre the "father", then, is clear and immediate.
With classes as well, code-only, or extension of the MovieClip property _parent is not available and could complicate things! Immaginiiamo to have a class MyClass :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| MovieClip { Class MyClass extends MovieClip { function MyClass () { / / Constructor } / / function myMethod () { this ) ; // myClass trace (this); / / myClass ; var num = 5; ; var cp = this; .... [ un oggetto ] ; var myObject = new [an object]; ( ) { myObject. onLoad = function () { this ) ; // myObject trace (this); / / myObject cp ) ; // myClass trace (cp), / / myClass } } |
, ovvero il puntatore alla classe MyClass . The context of MyMethod() is, of course, this , that is the pointer to the class MyClass . . The variable num , for example, has a life cycle enclosed within the method MyMethod() . It is therefore only visible within the method and will be destroyed on exit. . Same thing for the variable myObject initializes an event which, however, in this case onLoad . lasciandoci, apparentemente, senza il puntatore alla classe madre myClass . When triggered onLoad() will become the context myObject leaving, apparently, without the pointer to the parent class myClass . , ha lo stesso contesto della variabile myObject ! A closer look at the code we see that the variable cp , defined in the method myMethod() , has the same context of the variable myObject ! dell'oggetto myObject in quanto condivide con esso lo stesso contesto. In fact the variable cp (class pointer) will be visible within the method onLoad object myObject as it shares the same context. ha necessità di “vivere” più al lungo del previsto visto che ha “allocato” un evento (di tutto questo se ne occupa Flash a nostra “insaputa”). In fact the method myMethod() is not just because the object is deallocated myObject has the need to "live" over the longer than expected because it has "allocated" an event (all of this is dealing with Flash in our "knowledge").
Advice
For events associated with buttons or MovieClip would be a good habit to use the form:
1 2 3 4 5
| my_mc. onRelease = myOnRelease; myOnRelease function () { this ) ; // _root trace (this) / / _root "Click me" ) ; trace ("Click me"); } |
Instead of the more rapid and immediate:
1 2 3 4
| ( ) { my_mc. onRelease = function () { this ) ; // my_mc trace (this); / / my_mc "Click me" ) ; trace ("Click me"); } |
The advantage of using an external function is to have a neutral context and the ability to access the function regardless of the MovieClip that controls it. In the second case, for example, if you wanted to force the execution of the code related to "click" of the MovieClip should I use a code like this:
In practice I call the event as if it were a method. However, I have - in fact - to have the MovieClip my_mc . Another way, however, I can execute code directly calling myOnRelease() . Obviously, as shown in the examples, eye to contexts. . In the first case, the context of myOnRelease() is the parent of the object where it is written, for example, _root . In the second case, however, the context is always the MovieClip my_mc !
Forcing a different context
We conclude with a tricks useful in many circumstances. in grado di forzare un contesto di un metodo o funzione. Flash provides a library, mx.utils with an object Delegate can force a context of a method or function. Its use is very simple:
1 2 3 4 5 6 7 8
| utils . Delegate ; import mx. utils. Delegate; / / // puntatore ad un MovieClip my_mc var, / / pointer to a MovieClip // un bottone my_btn var / / a button function myFunction () { this ) ; trace (this); } create ( my_mc , myFunction ) ; my_btn. onPress = Delegate. create (my_mc, myFunction); |
The event onPress my_btn the button points to a "new" function created precisely with the context my_mc. When you click on the button my_btn, the code runs in myFunction() as context will my_mc!
Continued ...
Latest Comments
datrix : Thank you very much!
Robert : I rispsoto your questions with pleasure. The idea is really great. I am looking for a solution ...
Sting : @ Darius - you can see an example here: http://www.fight4fun.it/ clicking on: MAPS I hope ...
vik : Giustappunto I'm working on a project and the client asked me to show all the news (which are CPT) in ...
Giovambattista Fazioli : @ paso: absolutely. Simply identifying the field [cci] input [/ cci] you want to ...