Remove Child () tutorial in Flash CS3 using Actionscript 3

Thursday, August 16, 2007 1:26

This tutorial will teach you how to use the removeChild() method within Flash CS3 using actionscript 3. As you already have addChild that allows your Sprite or MovieClip to be displayed at run time RemoveChild can be set to these Objects along with Event Handlers to removes them from the stage as and when needed. This could go on to be used for game development and learning Applications.

 

This tutorial requires you have an intermediate knowledge of both Flash and ActionScript. If you are new to learning Actionscript 3 we will go through the workings of all the code and explain exactly what each of the sections of the class are doing. In this tutorial you will learn a way of using event listeners / handlers, removing a child and how to write the class correctly in Flash in order to do so.

 

 

 

 

Your Ad Here

To start off open a new File in Flash and name it RemoveChildTutorial for this tutorial mainly otherwise you can name your files what you wish. It is good practice to name your initial class file the same as your fla file but not nesscery. 

 

Then open a .as actionscript file and name that as same RemoveChildTutorial

 

You dont need to do anything with the fla file as all your workings will be done in code within the .as file.

The code explained

package{ this is a must and has to be written at the start of any class you write in AS3

 

These are a set of import staements for the classes within AS3 we are going to be using. notice where you use a * in your importstaments will import the entire class as oppose to just importing say the Sprite subclass

import flash.display.Sprite;
import flash.events.*;

import flash.display.DisplayObject;

This is where you setup your class, (always name your classes the same as you .as file name !.) then the class extends the Sprite class functionalities

    public class RemoveChildTutorial extends Sprite{

       

Here we have declared a name that we wish to use for the Sprite

        private var sprite:Sprite;

This function draws out a circle in black and puts it onto the stage note addChild() has been used

        public function RemoveChildTutorial(){

            sprite = new Sprite();

            sprite.graphics.lineStyle(1, 0×000000);

            sprite.graphics.beginFill(0×000000, 1)

            sprite.graphics.drawCircle(265,200,50);

            sprite.graphics.endFill();

            addChild(sprite);

           

Here we have an event listener that will listen out for the click of a mouse

           

        addEventListener(MouseEvent.CLICK, onMouseDown);

       

        }

       

       

 

As soon as the event listener hears the click of the mouse it tell the event handler to carry out its instructions as in this case the event handler is set to remove the circle from the stage when the mouse is clicked. As well as giving you a trace message saying "child has been removed".

        private function onMouseDown(event:MouseEvent):void{

            removeChild(sprite);

            trace("child has been removed");

            }

        }

    }

 

This is the final result please do comment and share your views thanks

 

 


Similar Posts:

Share/Save/Bookmark

You can leave a response, or trackback from your own site.

5 Responses to “Remove Child () tutorial in Flash CS3 using Actionscript 3”

  1. dave says:

    August 27th, 2007 at 1:26 pm

    Nice tutorial Thanks

  2. plexofoam says:

    September 18th, 2007 at 4:15 am

    I get this error.

    1087: Syntax error: extra characters found after end of program.

    Here is the code I am using.

    _package{

    import flash.display.Sprite;
    import flash.events.*;

    import flash.display.DisplayObject;

    public class RemoveChildTutorial extends Sprite{

    private var sprite:Sprite;

    public function RemoveChildTutorial(){

    sprite = new Sprite();

    sprite.graphics.lineStyle(1, 0×000000);

    sprite.graphics.beginFill(0×000000, 1)

    sprite.graphics.drawCircle(265,200,50);

    sprite.graphics.endFill();

    addChild(sprite);

    addEventListener(MouseEvent.CLICK, onMouseDown);

    }

    private function onMouseDown(event:MouseEvent):void{

    removeChild(sprite);

    trace(”child has been removed”);

    }

    }

    }

  3. shaz says:

    September 18th, 2007 at 1:37 pm

    Hi there plexofoam thanks for pointing this out its actually at the start of the code not the end. _package{ should not have the( _ ) before it and should be written as just package{. I will update this right now thank you.

  4. shaz says:

    September 18th, 2007 at 1:38 pm

    tutorial has now been updated with correction

  5. hintsofhints says:

    June 7th, 2008 at 8:56 am

    You forgot the part where you put the class name in the Document class field, a trivial step, but your instructions won’t work without it.
    And running a spellchecker wouldn’t hurt ;) (the grammar checker too)
    Great concise tutorial otherwise :) So many others take so long to read for getting the answer to a simple question. Thanks for your post ^_^

Leave a Reply