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

Replacing an instance on the stage with an instance from library

Community Beginner ,
Nov 13, 2017 Nov 13, 2017

Copy link to clipboard

Copied

I have several instances (movieclips) in the library that will be placed on the stage when the user clicks the appropriate button.

My first thought was to create a container for the movieclips to load into:

var mcContainer: Sprite = new Sprite();

addChild(mcContainer);

This is the code for one of the buttons:

btnStep1.addEventListener(MouseEvent.CLICK, Step1);

function Step1(event: MouseEvent): void {

var Step1: Step1;

Step1 = new Step1();

mcContainer.addChild(Step1);

Step1.x = 238;

Step1.y = 167;

trace: ("Step1");

}

I was hoping that loading movieclip Step2 into the mcContainer would automatically remove Step1 movieclip.

The end user has the ability to click any one of 6 buttons so I could not use removeChild since it could be one of a possible 6 movieclips loaded.

What am trying to achieve to load an instance from the library with a mouse click and if the user clicks one of 5 other buttons, the corresponding instance loads and replaces the instance currently on the stage.

TOPICS
ActionScript

Views

273

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
LEGEND ,
Nov 13, 2017 Nov 13, 2017

Copy link to clipboard

Copied

I don't have the reference handy but I believe there is a method called removeChildAt() whereby you don't need to know which object is in the container, just remove whichever occupies it using the index.

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
Community Beginner ,
Nov 13, 2017 Nov 13, 2017

Copy link to clipboard

Copied

Thanks. I need to figure out where the instance is in the index.

My initial test removed the objects on the stage and not the intended instance.

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
LEGEND ,
Nov 14, 2017 Nov 14, 2017

Copy link to clipboard

Copied

As JoãoCésar indicates, be sure to make it a method of the container.  You could use the numChildren property to loop thru and remove all of any, but if you know there will ever only be one at a time, then just checking if there is one is sufficient to target the child at the 0 index (it cannot be anywhere else).

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
Community Expert ,
Nov 13, 2017 Nov 13, 2017

Copy link to clipboard

Copied

I'm not sure if I understand your problem... Does your container have only one instance at a time?

If so...

Assuming that you have a container MovieClip on the stage called 'container' and five buttons called 'square', 'roundedSquare', 'circle', 'pacMan', and 'pentagon'.

import flash.events.MouseEvent;

import flash.utils.getDefinitionByName;

import flash.utils.getQualifiedClassName;

stage.addEventListener(MouseEvent.CLICK, mouseHandler);

function mouseHandler(e:MouseEvent):void

{

     if (e.target == square || e.target == roundedSquare || e.target == circle || e.target == pacMan || e.target == pentagon)

     {

     var C:Class = Class(getDefinitionByName(getQualifiedClassName(e.target)));

     if (container.numChildren > 0)

          container.removeChildAt(0);

     container.addChild(new C());

     }

}

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
Community Beginner ,
Nov 14, 2017 Nov 14, 2017

Copy link to clipboard

Copied

LATEST

Yes. The container will only have one instance at any time.

I appreciate everyone's help and will work this code into my project and see if I get it to work.

Another possible option I may have come up with from Ned's suggestion:

btnStep1.addEventListener(MouseEvent.CLICK, Step1);

function Step1(event: MouseEvent): void {

SoundMixer.stopAll();

var Step1: Step1;

Step1 = new Step1();

mcContainer.removeChildren();

mcContainer.addChild(Step1);

Step1.x = 238;

Step1.y = 167;

trace ("Step1");

}

Testing and more testing

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