AS3: add event listeners to loaded AS2 SWF - actionscript-3

What I am trying to do is - simply load an external SWF into my AS3 code.
I then want to show it on my stage - and be able to catch the 'ROLL_OVER', 'ROLL_OUT' and 'MOUSE_CLICK' events that happen with the SWF, meaning - I want to know when the user hovers over the loaded SWF and when he clicks on it.
If I load an external AS3 SWF - it all works fine, and I can trace the events successfully.
If I load an external AS2 SWF - in some types of AS2 banners I can catch the events, and in some - I can't.
It is important to note that I cannot control the loaded SWFs and I cannot code them in a different manner.
The way I load the external SWFs is like this:
.
var loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaded);
loader.load(new URLRequest(externalSwfURL));
function onLoaded(evt:Event):void
{
// The reason I don't create the MovieClip like this is because I need to support
// both AS2 and AS3 that will be loaded, and loaded AS2 cannot be casted to 'MovieClip'
//var mc:MovieClip = MovieClip(evt.target.content);
// This method allows me to load both external AS2 and AS3 SWF files
var mc:MovieClip = new MovieClip();
mc.addChild(loader);
// Add the events that I want to track
mc.addEventListener(MouseEvent.ROLL_OVER , onMouseEnterSWF);
mc.addEventListener(MouseEvent.ROLL_OUT , onMouseLeaveSWF);
mc.addEventListener(MouseEvent.CLICK , onMouseClickSWF);
mc.x = 100;
mc.y = 100;
stage.addChild(mc);
}
.
What I have found out is that if the loaded AS2 SWF has a transparent button on top of it (a lot of them have that) - then the mouse events aren't fired back up to my AS3 code ! They are somehow 'swallowed' inside the loaded AS2 SWF and not bubbled up.
If I try to load an AS3 SWF that has a transparent button as the top layer - it works, and still bubbles up the mouse events to my AS3 code.
Can anyone tell me why this happens ?
PS - if I load an AS2 SWF that doesn't have a transparent button as a top layer - than the mouse events ARE bubbled up to my AS3 code.
Here is a link to an AS2 SWF file that has the 'transparent button' that blocks the events from bubbling up to the AS3 code:
link to AS2 SWF

ActionScript 2 runs in a different virtual machine and is not compatible with ActionScript 3. Communication between the two SWFs ist not even not easy, but works only with a local connection. ActionScript 2 also doesn't have an event system so this would be the second part to take care of.
-> it can't work as you expect it to.

Related

When the game of an external swf is finished, remove the external swf

I am creating a series of mini games and animations and plan on bringing them all into one flash file. Each of the games need to disappear when the player has finished them. I don't know what to put into the section in the external swfs when the game is done. I currently just have a trace ("finished").
In my main swf you should click start and a game swf appears and when you are finished it, it should disappear.
I have looked up tutorials and they something about accessing variables in the external swfs but I got nowhere with them.
in place of trace("finished");, dispatch something like a complete event:
dispatchEvent(new Event(Event.COMPLETE));
Then listen for that event in the container swf and respond appropriately.
Here is an example for the host (parent) swf:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadDone, false,0,true);
loader.load(new URLRequest("temp2.swf"));
function loadDone(e:Event){
trace("CONTENT LOADED");
addChild(loader.content);
loader.content.addEventListener(Event.COMPLETE,go,false,0,true); //listen on the capture phase (third parameter true) if you're complete event is NOT on the main timeline of your child swf
}
function go(e:Event):void {
trace("Content is all done");
removeChild(loader.content);
loader.unloadAndStop(); //tries to free the app of all memory used by the loaded swf
loader = null;
}
Another way, is in place of trace("finished"), you could have the child swf remove itself (as per a comment on the question). I would consider this a less desirable solution (though more encapsulated) as it lessens re usability and will likely still leave the swf in memory:
stop();
if(this.parent){
this.parent.removeChild(this);
}

How to play two swf files in as3

hai i want play two swf files in my action script project.In this two files one swf file works on the detection face in front of the system.Other swf plays the flv file.when face is detected player must be stooped if not player must be plays the flv file.
I know how to load the swf file but i cant handle the functionality regarding starting and stoping player.
the snippet of code shows how can i load external swf file .and i will explain each line of code in comments
public function videos(view:Sprite)
{
this.box = view;//IT GETS Sprite object from other class because of need to display the player also.
request = new URLRequest("Untitled-1.swf");
currentSWF=new MovieClip();
loader= new Loader();
loader.load(request);
box.addChild(loader);
currentSWF = MovieClip(loader.content);
loader.addEventListener(Event.COMPLETE,loadComplete);
//addChild(loader);
currentSWF.gotoAndPlay(1);//when i put this line of code in comments it plays the external swf also.
}
I hope u understand my doubt .can any one explain how to handle my things .i am new to this action script.please help me
Loaded files automatically play, unless you tell them explicitely not to. You’ll have to listen to the Event.INIT event, and stop the movie there:
loader.AddEventListener(Event.INIT, initLoader);
function initLoader (event:Event)
{
MovieClip(event.currentTarget.content).stop();
}
This will stop the movie before it is attached to the stage, and before it starts playing—so it won’t do that unless you start it again.
Note that you shouldn’t access loader.content in any way before the INIT or COMPLETE events, as it’s very likely that the content isn’t loaded then. As such you should put all your manipulating actions into the COMPLETE event:
box.addChild(loader);
loader.addEventListener(Event.COMPLETE, loadComplete);
function loadComplete (event:Event)
{
// Now it’s safe to access the `content` member:
currentSWF = MovieClip(loader.content);
// Of course this one would play the movie again, so you probably want
// to call that later on a button click or something.
currentSWF.gotoAndPlay(1);
}

as3 loading screen

How do you create these "loading, please wait" splash screens with a loading bar and percentage for your swf? Does as3 have some built-in methods or should it be designed from scratch?
I mean, how do you detect that your swf file is being loaded into client's browser?
Oh and I have all my content stored in one frame only. pure as3 code.
If all of your code is on the timeline, then the easiest way is to make a second swf that is only used for loading your main swf. If your main swf is called main.swf, the code in your loading swf would be something like this:
//make a loader
var loader:Loader = new Loader()
//listen for loading progress
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
//listen for when the load is finished
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
//load begins...
loader.load(new URLRequest("main.swf"));
function onProgress(event:ProgressEvent):void
{
//calculate how much has been loaded
var percentageLoader:Number = event.bytesLoaded / e.bytesTotal;
//use your percentage number here to drive a loader bar graphic
}
function onComplete(event:Event):void
{
//remove listeners now that loading is done
loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
//add loaded swf to the stage
addChild(loader.content);
}
As an aside, externalizing your assets opens up new preloading possibilities. Instead of having everything in your main swf, you can have your main swf load external assets, and preload those assets. Since your main swf would then be small, there would be no need to have a second swf just for loading your main swf.
There's a few different ways to do this.
Either you can have a two step process - create a swf file that simply loads in the main file, reporting progress (by listening to the progress-event on the loader) and then adding the loaded swf as a child.
Or, you can place all your content on frame 2, and stay on frame 1, checking the root.loaderInfo.bytesLoaded compared to root.loaderInfo.totalBytes values, reporting the percentage and then moving on to frame 2 when done.
Personally, I think method 1 is better, since you don't need to sully yourself with frames.
There is also a third way which uses some weird meta-data tags to simulate the second way, but without frames. It's a bit messy, but should be googleable.
Flash has its own component you can use to accomplish this:
http://help.adobe.com/en_US/ActionScript/3.0_UsingComponentsAS3/WS5b3ccc516d4fbf351e63e3d118a9c65b32-7fa4.html
Or you can build your own progress bar from scratch:
http://www.zedia.net/2008/the-right-way-to-do-a-preloader-in-as3/
There are lots of resources on the web that allow you to customize the progress bar- browse around and find one that works for you and your coding style.

Transfer a BitmapData object from an AS2 swf to a parent AS3 swf via SWFBridge

I need to transfer a BitmapData object created in an AS2 swf to an AS3 swf. I'm using gskinner's SWFBridge to establish a two way communication between both flash movies.
The AS3 movie loads the AS2 swf which works completely standalone and lets the user manipulate MovieClips and finally generate an image from the composition he creates. I need the AS3 movie to receive this image (bitmapData), do some fancy image processing stuff AS2 isn't able to do and send the new image back to the AS2 movie.
So here's the code
AS2 swf:
var userCompo_mc:MovieClip = container.createEmptyMovieClip("userCompo_mc",10);
var image:BitmapData = new BitmapData(userCompo_mc._width, userCompo_mc._height);
finalCompo.attachBitmap(image); // Just to make sure the final bitmap is right
image.draw(userCompo_mc, compo.title);
//Send the image to the AS3 movie
sb1.send("imageTransfer",image);
AS3 swf:
function imageTransfer(bitmapData:BitmapData, title:String):void
{
var bmp:Bitmap = new Bitmap(bitmapData);
this.addChild(bmp);
trace(title); // --> returns the right title
trace(bitmapData); // --> returns null
}
I think using something like copyPixel32(), saving everything into an array and then passing it to AS3 would do the trick but it's really a performance hog.
Also, I'm not allowed to convert the AS2 swf into AS3 code.
Any suggestions?
Thanks you!
It seems like the as2 movie adds some decorations/movieclips to a an image.
After that you draw it and send attempt to send it to as3.
Since looping though all the pixels is slow, as you mention, I imagine it's faster to draw the as2 content from the as3 movie.
e.g.
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaded);
loader.load(new URLRequest('yourAS2Movie.swf'),new LoaderContext(true));
function loaded(event:Event):void{
var as2Clip:AVM1Movie = AVM1Movie(loader.content);
var bd:BitmapData = new BitmapData(as2Clip.width,as2Clip.height,false,0);
bd.draw(as2Clip);
addChild(new Bitmap(bd));
}
In this snippet the as2 content is drawn on load. In your case you would trigger/call a function that draws the as2 content via SWFBridge, after the as2 movie is ready/setup for what you need.
This works assuming you want to display the as2 content inside the as3 movie, which means you will load the as2 movie anyway. If not, either you load the as2 content, but don't add it to the display list, which means you'll be loading the as2 movie. Otherwise, you could try to save your final bitmap from the as2 movie using a server side language(like php for example), then trigger a function in the as3 movie, via SWFBridge, that will load the previously saved image.
HTH

AS3 ScrollPane dispatch event to content

i've loaded external swf into ScrollPane and i need to dispatch click event to this external swf. is it possible? ScrollPane.content.dispatchEvent(new MouseEvent(MouseEvent.CLICK,true)); doesn't work. this is obvious cuz ScrollPane.content is an DisplayObject and it have not CLICK event...
I can't use MovieClip as container for external swf cuz external swf is a documents converted to swfs using openoffice and it doesn't want to load inside MovieClip but perfectly loads inside ScrollPane and react on mouse clicks,but i need to simulate mouse click on it.
so you're saying that the following won't work or you haven't tried it?
var exSWF:MovieClip = MovieClip( ScrollPane.content );
or
var exSWF:Sprite = Sprite( ScrollPane.content );
Not sure to understand what you mean when you say that your external SWF won't load into a MovieClip.
Do you know what version of Actionscript was used for the external SWF, you can check that in debug mode by looking at the properties of the ScrollPane.content?