|
|||
| week 12b - director audio, globals/properties, duplicate sprites, collision detection | |||
globals, properties : |
use variables to communicate between code By default, variables used in sprite behaviors are local to that sprite, and persist only during the execution of the code. For example, in the following behavior code the variable count would equal 1 every time the sprite is clicked.
The count variable can never increase because it is recreated each time the code runs. In addition, the value of this variable is not available to any other code in the Director application. There are two approaches to working with variables that you want to survive longer. The first is property variables, which allows a sprite behavior to retain the value of a property variable over time. The second is global variables, which makes the variable accessible to any code running in the application.
|
|
| properties : | each sprite instance gets a personal copy of the variable Properties of behaviors are specific to each sprite the behavior is applied to, and persist as long as the sprite is on the stage (like any variable in Flash on a movieClip). For example, if two sprites have the following behavior, they will maintain their own counts, independent of each other. This would be useful if each sprite were maintaining its own score, hit count, on/off state, etc.
A sprite property can also be modified by other code. For example, if the above code was on sprite 2, then the following code would change the value of that property:
|
|
| globals : | make a variable accessible to all code Global variables have one value for the entire Director application, regardless of where it is accessed (similar to the _root.variableName technique we used in Flash). For example, if two sprites have the following behavior, they will update the same count variable. This is useful if each sprite is updating a system-wide variable, for example the total score for a game.
Any code that declares a variable as global (using the above syntax) can access the global variable. Simply put "global variableName" at the top of the code.
|
|
| global, property names : | use a standard naming convention for your global and property variables It is a good idea to use a standard method for naming global and property variables. This way it is clear how a variable is being used in the code. A suggested naming convention is as follows:
|
|
duplicating sprites : |
creating sprites on the fly Sample Director movie: view duplicateSprite.dcr, download duplicateSprite.dir As with Flash, it is possible to create new sprites in the Director score while an application is running. The approach is a little different than the one we used in Flash, but the effect is the same. Here are steps needed to create a new sprite:
|
|
| the code : | from duplicateSprite.dir
|
|
collision detection : |
determining if two sprites intersect Sample Director movie: view directorCollision.dcr, download directorCollision.dir It is possible to detect if two sprites intersect in Director using the following collision detection method:
This method requires that both sprites have the "matte" type of ink used, otherwise it will not work.
|
|
| the code : | from directorCollision.dir
|
|
|
|
| all materials on this web site © copyright 2003, Philip van Allen |
top |