Newest Articles

MegaCombs
Flash Media Player
XML Driven Pie Chart
Base Defender
Hangman Game
8 Ball Pool


Popular Articles

True Fullscreen Flash Mode
Mp3 Player with XML Playlist
Image Slider
Flash Media Player
3d Rotating Image Cube
Catapult Game


Random Articles

Picture Puzzle
Image Slider
Krypto Movie Quotes
Disappearing Button
Infinitely Zooming Image
Photo Reel


Links

Foundation-Flash
MickM
TutorialQuest
Tutorialsphere.com - Free Online Tutorials
Newgrounds
TWiT
Link to SwfSpot
Swf Spot



rss feed

Instantiating an Object

Instantiating an Object
AddThis Social Bookmark Button
Description: Robots are generated and deleted at the click of the mouse.
Author: John Bezanis
Added: March 1st 2007
Version: Flash 8


This tutorial demonstrates how you can create multiple instances of an object using actionscript. Begin by drawing your object. I drew (badly) a robot character.
convert to symbol
Next select the object and right click it. Select Convert To Symbol. Set the name to robot, the type to Movie Clip and select Export for ActionScript. This will convert your drawing into a symbol. You will see it appear in the library. Since it is now a symbol, click it on the stage (not the library) and hit delete on the keyboard.
We are now going to create a button that creates robots, or instances of the robot symbol to be more technical. Select Window -> Common Libraries -> Buttons. Open the classic buttons folder, then Arcade buttons subfolder, then click and drag "arcade button - red" to the stage. You now have a button on the stage that doesn't do anything. Single click the button and open up the Actions tab. Insert the following code:
  1. on(press){
  2.   createRobot();
  3. }
This inserts the code into the button instance. If you look at the code, you will see a call to the function createRobot(). Click the stage area to change the focus to the stage. You will notice the code in the Actions frame disappears because now the stage is focused, not the button. We are now going to write the createRobot() function. Insert the following code into the Actions Frame:
  1. var robotSerialNumber=1;
  2. function createRobot(){
  3.   //create a robot at a random location on the stage and increment the robot serial number
  4.   this.attachMovie("robot", ("r"+robotSerialNumber++), this.getNextHighestDepth(), {_x:Stage.width*Math.random(), _y:Stage.height*Math.random()});
  5. }
We can now create a robot with the click of the red button. Now, we are going to have each robot instance deleted when they are clicked. In the library, right-click -> edit the robot symbol. Open the Action Frame and insert the following code:
  1. function onPress(){
  2.   this.removeMovieClip();
  3. }
That's it. Robots are now created by clicking the red button and deleted by clicking a robot.

The source file is available below for download.
Download Source File
Comments
what if you wanted to stop after a certain number of instances?
March 21st 2007 12:03AM   -   mr. P
Change the code on the button to check the serial number of the robot
on(press){
if(robotSerialNumber<=5){
createRobot();
}
}
This limits it to 5 robots total.
If you want to have a limit on the stage at one time, you could have a counter of the robots on stage, and increment it each time a robot is created and decremented each time a robot is destroyed.
March 21st 2007 06:03AM   -   John
How would you go about removing all the dynamically created instances with the click of one button? Even if there are several instances of a vriety of symbols.
April 19th 2007 07:04PM   -   lake
How can i rotate an image in same path and not blur in flash with the help of actionscript?
June 26th 2008 04:06AM   -   jefry
That's pretty neat! How would you remove all robots at once though?
November 12th 2009 05:11AM   -   kids digital camera
I have bookmarked your site. Thanks for sharing
December 13th 2009 08:12AM   -   dental
Thank you very much for the source file. Your help is very welcome.
December 29th 2009 11:12PM   -   free bike games
great robot object creating. thanks for sharing.
December 31st 2009 03:12AM   -   cheap cricket phones
Great work every buddy can get lots of interesting information, keep on posting this type of brilliant articles. I really like this keep it up again!
January 15th 2010 09:01PM   -   evening dresses
I love this post, i replaced robots with naked girls. LOLz
January 23rd 2010 01:01AM   -   best cricket phones
Great information..
January 24th 2010 09:01AM   -   Acai Berry 8
It's easy to create Robot objects.
January 25th 2010 11:01PM   -   book
This robot look like android logo of google.
January 25th 2010 11:01PM   -   film
Special thank for good post.
February 5th 2010 09:02PM   -   à¹‚หลดเพลงà
Thank for the information
February 6th 2010 07:02AM   -   à¹‚หลดเพลงm
Special thank for good post.
February 10th 2010 04:02AM   -   à¹‚หลดเพลงà
Special thank for good post.
February 10th 2010 08:02AM   -   à¹‚หลดเพลงà
Add a Comment
name:
website (optional):
captcha type the characters into the box
message (5000 characters or less):