A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Remove Child using class

  1. #1
    Junior Member
    Join Date
    Jun 2008
    Posts
    4

    Remove Child using class

    this is my class i am trying to use to communicate with my main stage and object. the code in question that is returning the error is the code in bold. I just took a stab in the dark at how to code it, but i keep getting error's saying that i don't have the correct parent and that the _mc i am adding it to can't be found. I know it has something to do with communicating to the stage the original item is on, but i can't figure it out.

    thanks in advanced. (below is the AS class i wrote called FoundItem, i have it linked as baseclass to my _mc i want to send through it.

    package
    {
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    public class FoundItem extends MovieClip
    {
    private var _origXScale:Number;
    private var _origYScale:Number;
    private var i:Number = 0;


    public function FoundItem()
    {
    _origXScale = this.scaleX;
    _origYScale = this.scaleY;
    this.addEventListener(MouseEvent.ROLL_OVER, growItem);
    this.addEventListener(MouseEvent.ROLL_OUT, shrinkItem);
    this.addEventListener(MouseEvent.CLICK, clickItem);
    }

    private function growItem(event:MouseEvent):void
    {
    this.scaleX *= 1.5;
    this.scaleY *= 1.5;
    }

    private function shrinkItem(event:MouseEvent):void
    {
    this.scaleX = _origXScale;
    this.scaleY = _origYScale;
    }

    private function clickItem(event:MouseEvent):void
    {
    parent(this).removeChild(parent(this).this)
    this.addChild(toolBar_mc);
    this.removeEventListener(MouseEvent.CLICK, clickItem);

    }
    }
    }

  2. #2
    Senior Member
    Join Date
    Oct 2007
    Location
    Leeds, UK
    Posts
    118
    Your line
    Code:
    this.addChild(toolBar_mc)
    is trying to add toolBar_mc as a child of the foundItem. I'm guessing that you actually want to do the opposite, and add the foundItem as a child of the toolbar? (I'm also guessing that toobar_mc is your main class?)

    If that's the case, try this:

    Code:
    parent.removeChild(this);
    mainClass.addFoundItem(this);
    removeEventListener(MouseEvent.CLICK,clickItem)
    and in your mainClass add the following function

    Code:
    function addFoundItem(foundThingy:FoundItem):void
    {
    addChild(foundThingy);
    }
    Hope that works. I'm a noob, so there may be better, more elegant ways of doing this.
    Last edited by DiamondDog; 06-01-2008 at 02:56 AM.

  3. #3
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Your syntax is first of all wrong. parent does not have a parameter. In needs to be this.parent or just parent. However, if you remove the parent you will also remove the object itself. What exactly in the Displaylist you want to remove?
    - The right of the People to create Flash movies shall not be infringed. -

  4. #4
    Junior Member
    Join Date
    Jun 2008
    Posts
    4
    Quote Originally Posted by DiamondDog
    Your line
    Code:
    this.addChild(toolBar_mc)
    is trying to add toolBar_mc as a child of the foundItem. I'm guessing that you actually want to do the opposite, and add the foundItem as a child of the toolbar? (I'm also guessing that toobar_mc is your main class?)

    If that's the case, try this:

    Code:
    parent.removeChild(this);
    mainClass.addFoundItem(this);
    removeEventListener(MouseEvent.CLICK,clickItem)
    and in your mainClass add the following function

    Code:
    function addFoundItem(foundThingy:FoundItem):void
    {
    addChild(foundThingy);
    }
    Hope that works. I'm a noob, so there may be better, more elegant ways of doing this.

    It didn't quite work, i am getting an error about the addFoundItem in the Class code. thanks for the try. I purchased a training video on action scripting and i am getting the information i need, but its very round about. not ever getting exactly what i need.

  5. #5
    Junior Member
    Join Date
    Jun 2008
    Posts
    4
    Quote Originally Posted by cancerinform
    Your syntax is first of all wrong. parent does not have a parameter. In needs to be this.parent or just parent. However, if you remove the parent you will also remove the object itself. What exactly in the Displaylist you want to remove?

    I am trying to use the code as a reusable way to remove a mc off the main stage and put it into a movie clip on the main stage. (the idea being that the items i choose can be clicked on and added to a visual basket.) and i would like to make a code that is reusable. I got it to where i write the code repetitively in the actions frame on the main stage.

    thanks for your help

  6. #6
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    Then you need to remove the MovieClip first and I am not sure you are doing that, since I don't know your movie.
    - The right of the People to create Flash movies shall not be infringed. -

  7. #7
    Junior Member
    Join Date
    Jun 2008
    Posts
    4
    The movie clip that i want to remove i am representing by "this" becuase the movie clip is what ever movie clip i set to use this class and it runs when i click on it in the main stage

    Once the movie clip is removed from the main stage i want to add it to the movie clip labeled toolBar_mc.

    would the code for adding look like this?
    toolBar_mc.addChild(toolBar_mc.this)

    but i tried that and it failed also......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center