• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Animate AS3 addChild doesn't removeChild

Community Beginner ,
May 25, 2021 May 25, 2021

Copy link to clipboard

Copied

Hello, I am coding a game with an inventory and I am using addChild to move the item clicked on into the inventory.  This works fine, however when I move forward on the time line and then return to the same frame, the item remains where it was in addition to being in the inventory.  My understanding is that the addChild automatically removes the child from the stage when it puts it into another MC, however this is not what's happening.  I've tried removeChild however as the child has initially been removed, it throws up an error message.  Here's code for the function triggered by the eventListener:

 

function toInventory(event:MouseEvent):void {

inventory_mc.addChild(desertflower_mc);
trace("Flower goes to Inventory");
desertflower_mc.x = 400;
desertflower_mc.y = 290;
desertflower_mc.removeEventListener(MouseEvent.MOUSE_DOWN, toInventory);

 

How can I ensure that once the item is in the inventory, when I return to the frame where it was before it will no longer be there?

Thanks in advance!

TOPICS
ActionScript , Code , How to , Timeline

Views

197

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 26, 2021 May 26, 2021

Copy link to clipboard

Copied

LATEST

addChild doesn't necessarily remove anything.  it can re-parent a displayobject and change the object's depth.

 

for example, if inventory_mc is not on the stage, adding a stage object (eg, deserflower_mc) to inventory_mc will remove desertflower_mc from the stage. 

 

but when you return to a keyframe where that object is instantiated, it will have all the properties (including parent, position etc) it originally had before your code changed its properties. there's nothing you can do to prevent that.

 

you could work around it by maintaining a database (eg, an abstract object or even a sharedobject), but when you mix timeline and code displayobject manipulation you're making things complicated. it would be easier to use code to add all your potential inventory items.

 

eg, on the frame that instantiates desertflower_mc, remove it from the stage and in your library assign it a class (eg, Desertflower).  you could then use code to instantiate it:

 

if(!desertflower_mc){

var desertflower_mc:MovieClip=new Desertflower;

addChild(desertflower_mc);

// position etc

}

 

function toInventory(event:MouseEvent):void {

inventory_mc.addChild(desertflower_mc);
trace("Flower goes to Inventory");
desertflower_mc.x = 400;
desertflower_mc.y = 290;
desertflower_mc.removeEventListener(MouseEvent.MOUSE_DOWN, toInventory);

}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines