Run-time instances
Properties of new instance
Of course simply create the new instance is not enough. The most important is to manipulate this instance"s properties.
It is possible to change new instance position, size, transparency… any property programmatically.
We will modify our previous example.
- Open the previous file where we"ve created the ball instance by button click
- Delete all old code
- Enter the following code in the main timeline frame
thenumber = 0; thewidth = 50; theheight = 50; thealpha = 100; thex=10; they=30; thedepth = 1;
This code declare and initialize the variables we will be using to generate instances - Type this code for the button"s event handler
on (release) { thenumber ++; thedepth ++; thealpha -=7; thex+= 15; they+= 3; thewidth -=3; theheight -=3; attachMovie("ball", "ball" + thenumber ,thedepth); setProperty("ball" + thenumber,_x,thex); setProperty("ball" + thenumber,_y,they); setProperty("ball" + thenumber,_width,thewidth); setProperty("ball" + thenumber,_height,theheight); setProperty("ball" + thenumber,_alpha,thealpha); }At first we increment "thenumber" and "thedepth" and change values for all the properties of the symbol.Then we create the new instance using attachMovie(). As parameters we pass the linked identificator ("ball"), the name for the new instance "ball" + thenumber to be sure that name is always different, because "thenumber" variable is incremented every time, and "thedepth" which is also incremented. Pay attention to the depth – if you want to keep all instances that you"ve created on the stage – you need to assign the unique depth to each instance…
The last step – we use setProperty Action - by passing name of the instance, name of property to be changed and value of the property.
- Test the movie. On each button click it should draw one more circle with the shift to the right and size change. Experiment with other properties you know and their values.