Game Development Reference
In-Depth Information
This doesn't ensure that the object is on the stage before it's initialized. To do that, you need to use
an event called =@@A@[PK[OP=CA. You can put an =@@A@[PK[OP=CA event listener in the constructor
method. When the object is actually added to the stage, the event handler is triggered and you can
use it to initialize the class.
Here's a format you can use:
lq^he__h]ooC]iaK^fa_patpaj`oIkrea?hel
w
lq^he_bqj_pekjC]iaK^fa_p$%
w
]``ArajpHeopajan$Arajp*=@@A@[PK[OP=CA(kj=``a`PkOp]ca%7
y
lner]pabqj_pekjkj=``a`PkOp]ca$arajp6Arajp%6rke`
w
++Ejepe]hevalnklanpeao
y
y
In this example, the kj=``a`PkOp]ca event handler does the same job as the ejep method you used
in previous chapters. The only difference is that the directives it contains are run only when the object
is displayed on the stage.
If you need to be absolutely certain that all the objects are on the stage before you initialize
a class, consider using the BN=IA[?KJOPNQ?PA@ event, which runs only after all the objects have
been displayed on the stage. Similar to AJPAN[BN=IA , it's triggered in every frame.
If you want to use it to initialize a class, you'll probably only want to run once. You can do this by
removing the event after the first time it runs. You can use this format:
lq^he__h]ooC]iaK^fa_patpaj`oIkrea?hel
w
lq^he_bqj_pekjC]iaK^fa_p$%
w
]``ArajpHeopajan$Arajp*BN=IA[?KJOPNQ?PA@(kjBn]ia?kjopnq_pa`%7
y
lner]pabqj_pekjkjBn]ia?kjopnq_pa`$arajp6Arajp%6rke`
w
++Ejepe]hevalnklanpeao
naikraArajpHeopajan$Arajp*BN=IA[?KJOPNQ?PA@(kjBn]ia?kjopnq_pa`%7
y
y
If your class needs to access an object on the stage as part of its initialization, BN=IA[?KJOPNQ?PA@
guarantees that it can to find it. (If you want BN=IA[?KJOPNQ?PA@ to run every frame, you can
leave out the naikraArajpHeopajan directive from the preceding example.)
 
Search Nedrilad ::




Custom Search