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

load external swf into existing mc

Participant ,
Apr 09, 2013 Apr 09, 2013

I think this is very old question,

in as2 you can just add a external swf inside a mc.  like this -  loadMovie ("example.swf", "_root.mc");  and that's it.

how do i do that in as3? this following doesn't work..

var newLoader:Loader = new Loader();

function loadMc(evt:MouseEvent) {

    var request:URLRequest = new URLRequest("example.swf");

    newLoader.load(request);

    addChild(mc.newLoader);

}

can I go remove mc first, and then add the swf as mc again?

removeChild(mc);

addChild(mc);

TOPICS
ActionScript
1.5K
Translate
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 ,
Apr 09, 2013 Apr 09, 2013

there's generally no reason to do that but, you can:

var newLoader:Loader = new Loader();

function loadMc(evt:MouseEvent) {

    var request:URLRequest = new URLRequest("example.swf");

    newLoader.load(request);

    mc.addChild(newLoader);

}

Translate
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
Participant ,
Apr 10, 2013 Apr 10, 2013

Hi Kglad,

my 'mc' has another clip 'mc1' in it. so i tried to remove mc1 after loaded example.swf

I did try these method

#1

var newLoader:Loader = new Loader();

function loadMc(evt:MouseEvent) {

    var request:URLRequest = new URLRequest("example.swf");

    newLoader.load(request);

    mc.addChild(newLoader);

     mc.removeChild(mc1); <--

}

#2

var newLoader:Loader = new Loader();

function loadMc(evt:MouseEvent) {

     var target = mc.mc1;

    var request:URLRequest = new URLRequest("example.swf");

    newLoader.load(request);

    mc.addChild(newLoader);

     removeChild(target); <--

}

both don't work, example.swf is added, but can't get rid of the original mc

you know in as2, when I call 'loadMovie ("example.swf", "_root.mc");', example.swf will just replace mc.

Translate
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
Guru ,
Apr 10, 2013 Apr 10, 2013

Look here how loading in AS3 works.

Important: You must always wait until your swf is fully loaded until you do any DisplayListManipulations (adding or emoving childs, accessing stage, root or parent) with it.

Use Event.COMPLETE for that.

Then move your lines

    mc.addChild(newLoader);

     mc.removeChild(mc1);

inside that Handler-function

Translate
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
Participant ,
Apr 10, 2013 Apr 10, 2013

Hi, moccamaximum,

thank  you, going to test-fail-test for a while with this.

meanwhile Im gonna ask ahead another thing

Hi kglad, you gave me the source file for jpegTest yesterday, I tested it work like charm,

now you have these,

import JPEGEncoder;

var bmpd:BitmapData=new BitmapData(mc.width,mc.height);

bmpd.draw(mc);

var jpegEnc:JPEGEncoder =  new JPEGEncoder(75);

var jpegDat:ByteArray = jpegEnc.encode(bmpd);

then in function localSave it send jpegDat to php for generating the jpeg.

I want to pack these into calling function, so after example.swf loaded into mc, can call the function so the jpegDat will have the data of example.swf and generate the jpeg from it? 

jpgDatGenerate('mc');

function jpgDatGenerate (target) {

var bmpd:BitmapData=new BitmapData(target.width,target.height);

bmpd.draw(target);

var jpegEnc:JPEGEncoder =  new JPEGEncoder(75);

var jpegDat:ByteArray = jpegEnc.encode(bmpd);

}

obviously something wrong, it gives me something about jpegDat not defined at localSave

what have I missed?

Translate
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 ,
Apr 10, 2013 Apr 10, 2013

don't confound threads.  keep the jpegencoder code in that thread.

as for removing mc1, if that's a child movieclip of mc, your code is fine.  you don't need to wait until loading is complete to remove a child movieclip.

if you're trying to remove mc, then your code (and this thread) makes no sense.  a loader is a displayobject and can be (and usually is) added to the displaylist without creating a holder movieclip (like mc) as its displayobjectcontainer.

if you're trying to remove some previously loaded swf, you can use the same loader (which can only load one swf/bitmap at any one time), or apply unload() (or better, unloadAndStop() ) to the loader and/or you can remove the loader just like any child.

Translate
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
Participant ,
Apr 10, 2013 Apr 10, 2013

hi, Kglad,

sorry about that , going to post jpegtest problem there

to conclude what I'm trying to do , you see in the jpegTest, you have the rainbow pic 'mc',

inside 'mc' i created a child clip 'mc1' to contain the rainbow,.

then in the content with the buttons I made a new button that would load the example.swf into 'mc' and replace the rainbow 'mc1'.

and then jpegDat should then contain dataByte of example.swf but not the rainbow.

still test-fail-testing for whatever i got so far.

Translate
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
Participant ,
Apr 10, 2013 Apr 10, 2013

var newLoader:Loader = new Loader();

function loadMc(evt:MouseEvent) {

    var request:URLRequest = new URLRequest("example.swf");

    newLoader.load(request);

    mc.addChild(newLoader);

     mc.removeChild(mc1); <--

}

it gives me undefined property mc1 , I do have mc1 inside 'mc'!! and that was why I tried this, to put mc.mc1 inside 'target', no error, but then in the swf it doesn't remove the rainbow either

#2

var newLoader:Loader = new Loader();

function loadMc(evt:MouseEvent) {

     var target = mc.mc1;

    var request:URLRequest = new URLRequest("example.swf");

    newLoader.load(request);

    mc.addChild(newLoader);

     removeChild(target); <--

}

Translate
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
Participant ,
Apr 10, 2013 Apr 10, 2013

for moccamaximum,

I tried the methods with Event:COMPLETE, like these

pictLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);

    function imgLoaded(event:Event):void

    {

        mc.addChild(pictLdr.content); 

        //mc.removeChild(mc1.content); <-- doesn't work

    }

it still do exact same thing like my code, mc1 still debugged undefined..

just what's wrong with this mc1, i kept opening mc to see if mc1 in it, it's right there!

Translate
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
Participant ,
Apr 10, 2013 Apr 10, 2013

hi all,

surprisingly, turns out this is the answer!

removeChild(mc.mc1); X

mc.removeChild(mc1); X

mc.removeChild(mc.mc1); !!!  <-- this is really strange syntax if you ask me, but it does the job finally.

function loadMc(evt:MouseEvent) {

    var request:URLRequest = new URLRequest("example.swf");

    newLoader.load(request);

    mc.addChild(newLoader);

     mc.removeChild(mc.mc1); <--!!

}

Thank you again for all the helps!

Translate
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
Participant ,
Apr 10, 2013 Apr 10, 2013

hi kglad,

going to try that too, just in case, thanks a lot

Translate
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 ,
Apr 10, 2013 Apr 10, 2013
LATEST

no, don't botther.

if mc1 is not defined in the scope of the timeline that contains the code, you'll need to use:

mc.removeChild(mc.mc1)

Translate
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 ,
Apr 10, 2013 Apr 10, 2013

replace

mc.removeChild(mc1);

with

MovieClip(mc).removeChild(mc1);


Translate
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