as3 accessing CHILD in External loaded swf file - actionscript-3

How do I get to the MovieClips within an externally loaded Movie Clip.
Lets say I have a movie Called ONE in the swf I just loaded. How can I work with it alpha, position and other properties of the children of this clip?
CODE IN MAIN TIMLINE:
var LoadedMC:Loader = new Loader();
LoadedMC.load(new URLRequest("amplitude.swf"));
LoadedMC.addEventListener(Event.COMPLETE, bL);
function bL (e:Event):void{
addChild(LoadedMC);
}
Now its loaded I want to change the postino of the clip called ONE in the movie LoadedMC I just created.
Thanks for your help.
-Ed

I would think you would do something like:
//Cast the content into a movie clip
var someMC:MovieClip = MovieClip(LoadedMC.content);
//or try
var someMC:MovieClip = LoadedMC.content as MovieClip;
//then access the child
someMC.ONE.x = 50;
someMC.ONE.y = 100;

Related

MovieClip doesn't dispatch mouse event after nesting MovieClip loaded from external SWF

Mostly, problem is described in the title... I tried to load an external SWF file that contains some named MovieClip instances (exporting and naming is done by Flash CS5 software) and to add some of externaly loaded (named) MovieClip-s in MovieClip object which is created in my code. Problem appears when i add MOUSE_CLICK listener to parent MovieClip. Simply, it does not dispatch event when i click on it at the stage...
private var loader:Loader;
public function Example(){
loader = new Loader();
var request:URLRequest = ... // URL to external SWF
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadingCompleted)
loader.load(request);
}
private function loadingCompleted(event:Event):void{
var mc:MovieClip = loader.content as MovieClip;
var myMovie:MovieClip = new MovieClip();
myMovie.addChild(mc.getChildByName("object_name"));
myMovie.addEventListener(MouseEvent.CLICK, myMovieClicked);
addChild(myMovie); // myMovie (with nested mc) appears on the stage
}
private function myMovieClicked(evt:Event):void{
//never reached
}
EDIT: I didn't mention that i'm working in Flex using FlashBuilder 4.5 where i created ActionScript project. Code above is body of Example class, which is main SWF class.
EDIT AFTER ANSWER: myMovie.mouseChildren = false solves the problem. Earlier i tried to set mouseEnabled = true, and it didn't fix the problem. But i'm confused about event flow now... Even if child is target node, why mouse listener on parent MovieClip doesn't recieve event (in capture phase) when parent is still on event flow? Moreover, when i create another movie clip in my code (whit some simple shape inside) and add it to myMovie, everything works fine. What is so special when i obtain movie clip from externaly loaded SWF?
Have you tried doing myMovie.mouseEnabled = true and myMovie.mouseChildren = false ?

load an external swf (FlashPaper pdf) into a flash project

I have to do a very simple swf application able to show a series of pdf files.
Actually I was able to create on a single layer the menu interface (some buttons on frame 0 which redirect the user to other frames where the pdf should be showed).
Here my question:
I need a way to read the pdf inside the flash frame.
I've found a possible solution converting the pdf into an swf with FlashPaper 2
Now I wish to know how import the swf into the frame.
Reading some actionscript 3 guides I was able to create a container movieclip (a simple rectangle) into which I have loaded the swf with this code:
var swf:MovieClip;
var loader:Loader = new Loader();
var defaultSWF:URLRequest = new URLRequest("test.swf"); //test.swf is my converted pdf
loader.load(defaultSWF);
screen_01.addChild(loader); //screen_01 is the container rectangle converted to movieclip
I've used a container movieclip to mantain the other objects (menu buttons) on the frame, and ecause it helps with the swf positioning.
I seen it is possible also using loader.x and loader.y and it works.
Unfortunately I wasn't able to control width and height of the swf/pdf file (loader.width and loader.height exists but if used cause the swf will not loaded at all)
Solution:
I've found a possible solution at this page. The idea is to change swf scale only after the load process is complete (positioning can be done even before).
Anyway I had to use Actionscript 2 for this project because FlashPaper converted pdf doesn't work properly with AS3, I don't know why...
here the code:
//insert an emplty movieclip to load the swf, I've called it screen_01
var movLoad:MovieClipLoader = new MovieClipLoader();
var myListener:Object = new Object();
myListener.onLoadInit = function(thisMc:MovieClip) {
thisMc._height = 600;
thisMc._width = 900;
thisMc._x = 50;
thisMc._y = 30;
};
movLoad.addListener(myListener);
movLoad.loadClip("folder/flashpaper_converted_pdf.swf.swf",screen_01);
You can modify the size of the loader using scaleX and scaleY properties.
Keep them equals so the swf isn't stretched.
You may also want to listen to the COMPLETE event to do such a thing :
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
function onComplete(e:Event)
{
var loader:Loader = e.currentTarget.loader;
loader.scaleX = loader.scaleY = 2; //Double the size of the loaded swf
}

Access children of embedded aswf

I am embedding an swf file that has some children on its timeline. Like this:
[Embed(source="assets/skyscraper200x600.swf")]
private var Skyscraper :Class;
All children in the swf have an instance name, I double checked that when creating the swf in Flash CS5.
I am trying to access those children by name like this:
_bg = MovieClip(new Skyscraper());
_pig = MovieClip(_bg.getChildByName("chara_pig"));
_arrow = MovieClip(_bg.getChildByName("arrow_banner"));
However, both _pig and _arrow end up being null.
What's even stranger is that when I look at the Skyscraper object in the debugger, it shows a rather strange class name and a Loader as its only child (which in turn has no children). What's up with this?
.
I can access them like above if I do not embed the swf, but load it with a Loader. But I cannot do it in this case. I need to embed the swf.
So, how can you access children of embedded swfs?
I am not talking about accessing classes in the library of the embedded swf, but the instances on the timeline.
Here is a solution. You can also see the steps who helped me find this solution (describeType is your friend) :
public class Demo extends Sprite {
[Embed(source="test.swf")]
private var Test:Class
public function Demo() {
//first guess is that embed SWF is a MovieClip
var embedSWF:MovieClip = new Test() as MovieClip;
addChild(embedSWF);
//well, emebed SWF is more than just a MovieClip...
trace(describeType(embedSWF));//mx.core::MovieClipLoaderAsset
trace(embedSWF.numChildren);//1
trace(describeType(embedSWF.getChildAt(0)));//flash.display::Loader
var loader:Loader = embedSWF.getChildAt(0) as Loader;
//the content is not already loaded...
trace(loader.content);//null
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(){
var swf:MovieClip = loader.content as MovieClip;
var child:MovieClip = swf.getChildByName("$blob") as MovieClip;
//do nasty stuff with your MovieClip !
});
}
}
At the end of this tutorial http://jadendreamer.wordpress.com/2010/12/20/flash-as3-embedding-symbols-from-external-swf-game-tutorial there is an example of how it can be done
One solution is to embed the swf as an octet stream and reconstitute its bytes. However, I seem to remember reading somewhere that if you just set the mimeType to "application/x-shockwave-flash", you get a MovieClip that works as normal.

ActionScript: Adding multiple instances of the same swf file to the stage

I'm creating a game in ActionScript 3.0 using the FlashPunk game engine and the FlashDevelop SDK.
I've created my own MovieClip class that takes a preloaded movie clip file.
public function MyMovieClip(file:MovieClip, posX:int, posY:int, frameRate:int)
{
movieClip = file;
movieClip.x = posX;
movieClip.y = posY;
movieClip.stop();
FP.stage.addChild(movieClip);
timer = new Timer((1 / frameRate) * 1000);
timer.start();
timer.addEventListener(TimerEvent.TIMER, onTick);
}
The update for my movie clip is as follows:
private function onTick(e:TimerEvent):void
{
if (isRepeating)
{
if (movieClip.currentFrame == movieClip.totalFrames )
{
movieClip.gotoAndStop(0);
}
}
movieClip.nextFrame();
}
The problem that I'm having is that when I have multiple instances of the MyMovieClip class using the same swf file, only the last instance is rendered and is updated for each instance of the class that I have.(e.g 3 instances of MyMovieClip, the last instance is updates at 3 times the speed.)
If anymore info is needed I'll be happy to supply it.
You can create a new instance of the same loaded swf by doing this:
// re-use a loaded swf
var bytes:ByteArray = existingLoader.content.loaderInfo.bytes;
var loader:Loader = new Loader();
loader.loadBytes(bytes);
where existingLoader is the Loader that you used to load the swf in the first place.
The Loader used with loadBytes will dispatch another COMPLETE Event, so when you make a listener for that, you can use the 'cloned' version:
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onSWFClonedComplete);
You may have multiple instances of MyMovieClip, but what's with file:MovieClip which you are adding to the stage. If this is always the same instance of a MovieClip you will have this result, regardless how often you instantiate your MyMovieClip class, because you are adding the same instance multiple times to the stage.
You may have to load the "preloaded clip" multiple times or, if you are able to (you know the class name etc.), create a new instance of the desired class with getDefinitionByName() from your loaded clip and attach this new instance.

ActionScript 3 stage question HELP!

I have a 1.swf loads up a another 2.swf, while i doing things inside 2.swf like
1.swf is bigger
2.swf is smaller inside
stage.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
it keep refer to the parent's stage instead the 2.swf's stage.
Please help out. thanks
There is only one stage object, and it is always inherited from the root. Anytime you can access the property "stage" inside a DisplayObject (Sprite,MovieClip) it's actually just a reference to the root.stage that gets populated whenever that DisplayObject is added to the root stage, or a child of some object that eventually connects to the stage (the display list). The display list is just an object tree of various display objects that are connected to the root stage. Anyway so, about your question, if you just want to listen for events on your loaded swf do something like this:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
loader.load(new URLRequest("swf2.swf"));
private function loadComplete(e:Event):void
{
var swf2Clip:MovieClip = loader.content as MovieClip;
swfClip.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
}
private function onMouseOut(e:MouseEvent):void
{
//Do something when swf2 is moused out.
}