AS3 ScrollPane dispatch event to content - actionscript-3

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?

Related

Flash / AS3 - How to Disable Child SWF Mouse Clicks Without Disabling Mouse Hover Events

I have a flash browser application that loads in a child swf using a Loader object.
The child swf has a mouseclick listener that calls nativateToUrl, and opens a new web page. It also contains mouse hover events that make things in the swf move around.
This application that does the loading is like a container for the child swf and should open a different web page when it is clicked. So essentially I want to suppress the child swf mouseEvent and listen for the mouse click on the container swf, opening the new URL.
I have heard of other people putting an invisible sprite over the original content so that the child swf doesn't actually catch a mouseclick event. However, this won't work in my case because then it doesn't get the mouse hover events either. I want the mousehover events to go through and make the things in the child swf move but also suppress the click event.
So, for all the flash masterminds out there... is this possible? And how can I do it?
:) thanks.
If you want to suppress all MouseEvent.CLICK events from reaching a loaded SWF, you can do the following:
Let's assume you have the following setup:
var loader:Loader = new Loader();
loader.load(new URLRequest("child.swf"));
addChild(loader);
What you can do, is listen on the capture phase for a click event on the Loader:
loader.addEventListener(MouseEvent.CLICK, handler, true);//the third parameter (true) tells flash to listen on the capture phase of an event's lifecycle
Then in the click handler from that, cancel the event so it doesn't propagate down to the loaded SWF:
function handler(e:Event):void {
e.stopImmediatePropagation();
}
If you don't understand the phases of events, this diagram from Adobe may help:
Source
As an aside, if you wanted to suppress all mouse stuffs from the SWF, a much better way (instead of adding an invisible sprite) is to just do this:
loader.mouseChildren = false;

AS3: add event listeners to loaded AS2 SWF

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.

Loading and using SWF files

I'm new to AS3, and am trying to understand how externally loaded SWFs work in AS3. Since Flash 4/5, it was common to have one main SWF file in a Flash web project, and then load other SWF files into it, often for various "sections" of a website or web project. In the main file, we'd have masks animating the container movieclip(in which external sections/SWF files were loaded) and have animations and transitions play as the section finished loading and the loaded content was displayed.
In AS3, I've used the Loader class to load and display the external file, my main problem is in communicating with the loaded content, call it's functions, or call root functions from it.
In AS2, we could use someMovieClip.loadMovie("ExternalContent.swf") and the ExternalContent file would load inside someMovieClip. You could access functions on the "External.swf" main timeline using someMovieClip.function();. And inside the "ExternalContent.swf", we could use _root.function() to access functions in the main file ExternalContent was being loaded into. Doing this in AS3 seems bizarre and neurotic, and I feel like I'm missing something fairly basic here.
//Loading in ExternalContent.swf into the sprite
//ExternalContent has a movieclip called "boxes" on it's main timeline
//boxes has a boxesPrompt() function in it's timeline.
var sprite:Sprite = new Sprite();
addChild(sprite);
var loader:Loader = new Loader();
loader.load(new URLRequest("ExternalContent.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaded);
function onLoaded(event:Event):void
{
sprite.addChild(event.target.content);
sprite.boxes.boxesPrompt();
//Flash gives the following compiler error at the above
//Scene 1, Layer 'Layer 1', Frame 1, Line 21 1119: Access of possibly undefined property boxes through a reference with static type flash.display:Sprite.
//But when I comment out sprite.boxes.boxesPrompt() and use this, it works:
event.target.content.boxes.boxesPrompt()
}
The boxesPrompt() function inside the "ExternalContent.swf" just traces it's parent, grand-parent, and great grand-parent - trace(this.parent.parent.parent);. And when I call that function inside the onLoaded event-handler using "event.target.content.boxes.boxesPrompt()", it shows that the Boxes object(which was on the main timeline of External.SWF), has a parent movieclip, a grand-parent sprite, and a great grand-parent object mainTimeline.
I thought re-parenting the loaded content into the sprite would allow me to access the loaded content as easily as loadMovie() used to be - accessing loaded content like it was present directly inside the clip it was loaded in. But that doesn't work at all.
So to rephrase, my question is:
How do I communicate from the main "loader" SWF file, with the content that's loaded in. I don't want to communicate using event.target.content.{etc} because then I'd only be able to address the loaded content inside the Loader's event.complete event handler.
How do I "re-parent" loaded content, so I can place it inside some movieclip/sprite on the main timeline of the loader file, rather than using some really long convoluted way.
How to communicate from inside the loaded content, to the main/loader file. Previously, we'd use _root.functionName() to do stuff like play some animation transitioning from the current externally loaded "section" to another section. How'd I go about doing that.
AS2 & AS3 is vastly different. But you will have to swallow the fact that AS3 has been developed as an improvement over AS2. So any transition you make, is also for the better.
For eg : The _root in AS2 allowed global objects & variables to accessed & changed anywhere, which is a bad practice & leads to non maintainable code in a project.
Having said that, let me address your questions:
If you are able to get access to the loaded content with
event.target.content... you should save it inside a,say class
variable & may access it later elsewhere in the class.
You must understand that you will be able to access the content only
after loading it, so have to wait for it to complete anyway &
event.complete handler is probably your best bet.
I doubt if you can pick random content from a loaded swf & re-parent it into the current swf.As explained you might not have a long convoluted way.
Accessing the parent could be done in many ways. You can use .parent or actually call a function from the parent swf passing its reference to the child.
var sprite;
addChild(sprite);
var loader:Loader = new Loader();
loader.load(new URLRequest("ExternalContent.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaded);
function onLoaded(event:Event):void
{
sprite = event.target.content;
//This should work
sprite.boxes.boxesPrompt();
}
See this example for more info.

Get Child of loader from externally loaded swf

READY? Here it is:
var TheLoader:Object = parentObj.TheExit as Object;
This line gets me to the swf I loaded and I can now change the alpha, move it etc...
BUT HOW HOW HOW... do I get one of its children and control get that same control?
Example Code:
TheLoader.TheChild.alpha = .3; // Does not work!
5 days on this issue is WAY TOO LONG! Here is the 3rd post with the same issue but more detail. as3 externally loaded swf from network to control externally loaded swf from network
I just made this shorter to get attention to the ONE LINE I NEED!!!
THANKS!
If you look at the documentation for Loader, you'll see that it has a content property. This is how you access your loaded content. So, for example, if TheLoader is a Loader, and if TheChild was an instance on the timeline of the loaded SWF, you could do:
var child : Sprite = MovieClip(TheLoader.content).TheChild;
This child is not available until the content has been actually loaded, so be sure to listen to for the COMPLETE event on the Loader's contentLoaderInfo before accessing the content:
TheLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
function onLoaded(event : Event):void
{
trace(MovieClip(loader.content).theChild);
}
Also note that you may run into security issues if the loader and loadee SWFs are not on the same domain. In this case, you will have to call Security.allowDomain in the SWFs to allow the cross-scripting.

AS3: add event listener to loaded swf

I'm trying to listen for a custom event from an SWF I've loaded and I'm just not able to capture it. The loading code right now is just:
public function loadGame(gameSrc:String,gX:Number,gY:Number):void {
var loader = new Loader();
var addedDefinitions:LoaderContext = new LoaderContext();
addedDefinitions.applicationDomain = new ApplicationDomain();
loader.load(new URLRequest(gameSrc));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaded);
function onLoaded(evt:Event):void {
var game:MovieClip = MovieClip(evt.target.content);
game.x = gX;
game.y = gY;
chamber.mc_gameHolder.addChild(game);
Tweener.addTween(chamber.mc_gameTitle,{alpha:1,time:.75});
game.addEventListener("showQuiz",showQuiz);
}
}
I know the event is being fired from my loaded SWF because I also have a listener in there that traces out a "hello" when it's fired.
Anyone? And apologies if this has been posted before - search didn't turn up anything specific.
I ran into the same problem. Here is what you need to do. When instantiating your LoaderContext, make sure the LoaderContext is using SecurityDomain.currentDomain. That will solve your problem.
This would work only if both SWFs are AVM2Movie (made using AS3), which I assume is the case here because otherwise casting to MovieClip would have thrown an error on run-time.
Are you sure that the event is dispatched by the document class of the loaded swf and not by one of its children? Because you are calling addEventListener on game which is the document class (root) of the loaded SWF and it won't catch events dispatched by its children. Can you show the code where you dispatch the event?
It may be possible that the event is being dispatched before the Event.COMPLETE event. Try adding a listener for the Event.INIT event. The Event.INIT event is dispatched when the Loader first has access to the loaded swf's document object.
hm, shouldn't it be :
addedDefintions.applicationDomain = ApplicationDomain.currentDomain to permit the loaded video to 'access' the parent one ?
Also for testing purposes I suggest to bubble the event up to make you haven't missed out a display object in between.
Things to consider:
The loaded clip should be from a security-enabled domain. If it's not the same domain the loading flash resides at, it should be included in the crossdomain.xml file. And loaded too.
Manually setting "allowDomain" via Security.allowDomain towards the loaded clip's domain is never bad.
The event should be bubbling, as Flash might add a layer or two of containers between the "game" var and the actual content.
Both loading and loaded clips MUST be AS3.
It is possible that the target clip is trying to load things from it's own proper location, so when you load it under a different URL, it can't find the files and fails, never reaching the event-firing phase in the first place.
Create a LoaderContext that sets the ApplicationDomain to the currentDomain, then pass it with your load() call:
loader = new Loader();
loader.contentLoaderInfo.addEventListener( Event.COMPLETE, handleLoaded );
var loaderContext:LoaderContext = new LoaderContext(false, new ApplicationDomain( ApplicationDomain.currentDomain ) );
loader.load( new URLRequest("yoMama.swf"), loaderContext );
You can confirm that you don't have a crossdomain issue by having the loading swf trace a var or function call from the loaded swf. If you get the expected result, crossdomain.xml is not your issue.
You have to consider some things in your situation.
(1) if you are loading code from the SAME domain, the app domain is not necessary.
(2) if you are loading code form DIFFERENT domains, check the [crossdomain.xml] policy file first... second... if you dont want to waste time fixing this... use PHP and curl the file to your domain... then load that (Flash will think its loading it from the same domain).