Communicating to Parent SWF - actionscript-3

Hey guys I'm having such a hard time of it today.
I have a game, I've loaded into a parent SWF -
I would like my parent SWF to accept events I dispatch from within the game,
or have the ability to talk both ways.
--
Reason is, I would like to unload and load the game back in, once the end screen is active.
Any help and I'll give you beer.
thanks in advance.

Are the events dispatched by your game allowed to bubble up to your loader swf? The default value for the Event constructor sets this to false. If you were to set it to true, however, an event dispatched by a child swf would make it to its parent's listener.
For example: this.dispatchEvent(new Event("SOME_GAME_EVENT", true, true));
The first 'true' value says that the event should bubble up through the object hierarchy. The second says that the Event is cancelable. Once your loader swf has handled the Event, it is best to then call .stopPropagation() on the event so it does not further bubble unneccessarily.

can't you put in your parentSWF:
childSWF.addEventListener(MyCustomEvent.SOME_EVENT, listener) and just dispatch the events from the game?
As for talking the other way, it can call childSWF.someFunction() to call functions in the document class of childSWF, etc.

Related

How to properly dispatch event from Stage reference

I'm facing following problem from some times, and I always fail to overcome it.
I guess solution has to be provided in different way, but I would like to find out why this kind of code doesn't work.
Scenario:
Main class is Main.as in which there is a Sprite1 instance added to Main displayList.
There is also MyCustomClass instance which takes reference to Stage in constructor, to be able to dispatch an MyCustomClass.ABC event.
(in MyCustomClass)
stageRef.dispatchEvent(new Event(MyCustomClass.ABC));
then in my Main where sprite1 instance is placed on Main's displayList I want to listen for this MyCustomClass.ABC event dispatched by stage class reference from inside of MyCustomClass.
Finally if I put following code in my Main.as:
sprite1.addEventListener(MyCustomClass.ABC, onABC);
it doesn't work so I have to subscribe to this event directly by stage reference in Main class.
stage.addEventListener(MyCustomClass.ABC, onABC);
I thought that in the capture phase the event propagates from Stage to all the children, and sprite1 instance is a Main's child which is Stage's child. So, for me it should work but it doesn't.
I created an image describing objects on the displayList in this project. Please look at
link http://www.iv.pl/images/61170779800943350498.png
Thanks in advance for any explanations why it doesn't work. It doesn't work with any parameters I pass to listener (capturing) or dispatcher(bubbling).
capture phase event goes from target to stage while bubbling phase goes from stage to target. When stage is the target no object in the display list can catch that dispatched event since they cannot be included in the event phases. The target (stage) goes to stage then back to target (stage) and there's nothing in between to catch the event.
Typically when using stage to dispatch an event programmers want to make use of stage global nature (design that can be argued upon) but in that type of design it also mean that only stage should listen to its own dispatching.

Global Event Listener

Is there any way by which I can make my button work, no matter in which scene or frame it is.
Is there any way by which i can call that Event Listener to another frame for that particular instance?
for example:
I have a button home_btn, I want this button to work in all scenes, without changing it's instance name. I already added an Event listener in first scene, but it doesn't work,
In another frame or scene.
Below is the code.
home_btn.addEventListener(MouseEvent.CLICK, process_it);
function process_it(event:MouseEvent):void
{
nextFrame();
}
I don't know how to use dispatch event function for my button.
// This only works for that particular frame.
Since you're doing this in the timeline, if you put the home_btn.addEventListener(MouseEvent.CLICK, process_it);
on the first frame of your main timeline, it will be registered even if you change to a different frame.
You can keep the function in its existing frame as that should still be called.
The reason why this works as opposed to the code being on a different frame is because frame 1 is where your timeline will start and process all the display items and code needed during this frame. That is any function, variable, or listener you want active will be running and/or stored into memory at the start of the application since it will always start on frame 1 until specified otherwise. If you have the listener on a different frame then you will have to gotoAndStop() to that frame to add the listener ( otherwise you are specifying that you don't want to add the listener until your application is at the frame ). Adding a listener to a display object on frame 1 is contingent upon the display object also being on that frame.

Detecting MouseEvents for object below another object

So basically, I have a large movieclip, lets call it "hit" and a bunch of smaller "thumb" movieclips below it. I have ROLL_OVER and ROLL_OUT event listeners on the main "hit" movieclip that I use to position the thumbs correctly (the component is a kind of ticker).
I am having a problem with getting the ROLL_OVER, ROLL_OUT, and CLICK event listeners to fire on the "thumb" movieclips that are below "hit".
Right now I am using a hit test, which kind of works, but I'd like a simpler way. I am an actionscript-3 newbie so any help would be appreciated. Thanks!
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/MouseEvent.html#!flash/display/InteractiveObject.html#event:rollOver
Dispatched when the user moves a pointing device over an InteractiveObject instance. The event target is the object under the pointing device or a parent of that object. The relatedObject is the object that was previously under the pointing device. The rollOver events are dispatched consecutively down the parent chain of the object, starting with the highest parent that is neither the root nor an ancestor of the relatedObject and ending with the object.
Object under hit won't dispatch ROLL_OVER event if it's not a parent of hit. Only objects on top will dispatch it. Mouse cursor must literally touch the object. If there is something between cursor and object, event won't be dispatched.
This thread appears to be asking the same question. The solution including getObjectsUnderPoint seems to be the best choice.

Behavior of MOUSE_OVER eventListener in action-script-3

If the mouse is over an object before and while a MOUSE_OVER event is registered, does it trigger? I ask this because it appears that it doesn't in my program, and I want to know if this is a universal behavior of all MOUSE_OVER events. Is there a way around this?
I'm gonna avoid giving a code example here, because my program is large and complicated.
The MOUSE_OVER event will dispatch whenever the cursor enters the bounds of any interactive DisplayObject, such as a Sprite or MovieClip; this includes any of its children (see ROLL_OVER if you wish to ignore children).
As well, the event will dispatch in cases where an object is added to the stage and currently happens to be under the cursor.
It is important to make sure that your event listener has been registered before the Flash Player has dispatched the event -- system events are not queued beyond a single frame, and thus no handlers will be invoked for previous activity.

Detect mouseUp on stage

Is there a way to check the method that has been attached to the stage?
I have stage as global.. and need to fire some function in a object on mouseup...
Now it fires 2 or 3 depending how many objects i add..
I need something like..
if($.stage.hasEventListener(MouseEvent.MOUSE_UP, this.mouseUp) === false){
$.stage.addEventListener(MouseEvent.MOUSE_UP, this.mouseUp);
}
Or a better way to handle this?
I'm guessing you are adding the listener inside each object, no? That means every time you create an instance of your object you are adding yet another listener for stage mouse up events. If you really only want a single listener for this type of event, move it outside of the scope of the object and only add the listener once. Good luck!