Event Bubbling, and Stop Propagation - actionscript-3

What is the difference between event.bubbles to false for any event, and setting event.stopPropagation() or stopImmediatePropagation() while handling event?
I'm using Flex4 with AS3.

Information found at this article - Introduction to event handling in ActionScript 3.0 is more demonstrative and easy to understand. It will enhance the above accepted answer by #Jason Sturges.
Event bubbling and event capturing are two faces of events. If you make the event.bubbles to false that means the event is marked as non-bubbling event.
bubbles: Indicates whether or not the event is an event that bubbles (and captures). This does not mean that the event went through or is going through a capture or bubbles phase, but rather it is a kind of event that can.
Below image (from the above article) shows how the event goes through the process.
The difference of the stopPropagation() and stopImmediatePropagation() will be more clear in following images.
StopPropagation :
StopImmidiatePropagation :

Setting bubbles to false means the event does not bubble up the display list at all.
stopPropagation() and stopImmediatePropagation() make the current event listener the last to process an event.
The difference between stopPropagation() and stopImmediatePropagation() is that stopImmediatePropagation() will not only prevent the event from moving to the next node, but it will also prevent any other listeners on that node from capturing their events.

Look at the example:
object.addEventListener( MouseEvent.CLICK, functionOne );
object.addEventListener( MouseEvent.CLICK, functionTwo );
If functionOne contains event.stopPropagation(), functionTwo will be called as well.
If it contains event.stopImmediatePropagation(), functionTwo will be ignored.

Related

How to detect menu button press in TVOS app using TVJS

I need to know when a document(screen) is popped off the stack in an Apple tvOS app. I thought detecting the Menu button press would be the simplest way, but I'm using TVJS and have not been able to figure out how to write the event handler.
Please help me write an event handler that will fire on document removal, menu button press or offer an alternative solution.
Subscribe to the event unload - it's triggered whenever a page disappears after being popped from the stack:
doc.addEventListener("unload", Presenter.onUnload.bind(Presenter));
[...]
onUnload: function(event) {
console.log("onUnload");
},
There is such thing of a handler for onDocumentRemoval or similar. What you can do, instead, is create a global select handler:
doc.addEventListener("select", self.doThing.bind(self));
And then check if the fired event comes from one of the buttons used to remove an element of the stack (let's suppose those buttons have a class named delete:
doThing: function(event){
var element = event.target;
if (element.getAttribute("class").contains("delete")){
//enter code here
}
EDIT 1:
I found the possible events the TVMLKit handles (I know it is in Swift/Objective-C, but the events are the same):
TVElementTypePlay
A play event has been dispatched.
TVElementTypeSelect
A select event has been dispatched.
TVElementTypeHoldSelect
A hold event has been dispatched.
TVElementTypeHighlight
A highlight event has been dispatched.
TVElementTypeChange
A change event has been dispatched.
Those events are only attachable to a template as far as I could test. I guessed the change event would be perfect if I could attach it to the navigationDocument to listen for changes, but those two options won't work and both fire errors:
Attached to the global:
navigationDocument.addEventListener("change", function(event){console.log(event)});
Attached to the documents array:
navigationDocument.documents.addEventListener("change", function(event){console.log(event)});
There is no built-in method for those above to listen for any change. The event, though, will work on a template listening to internal changes. But it won't fire when the template is pushed to or popped from the stack.
I am guessing you will need to re-design your app in order to achieve what you are looking for.

Flash ActionScript 3.0 button click over global MouseEvent.CLICK

I want do to the next one in flash with ActionScript 3.0:
Global event (if user click in any part of the screen by mouse):
addEventListener (MouseEvent.CLICK, nextc);
function nextc (event:MouseEvent): void
{nextFrame();}
Button event (if user click exact this button):
returnb54.addEventListener(MouseEvent.CLICK, returnb54a);
function returnb54a(event:MouseEvent):void
{prevFrame();}
But on the frame with this a global event and a button nothing happens when clicking the button.
Is there any way to prioritize button event over global?
Thank you.
I created a very simple application to test your question (I'm using the same names as you defined, to be easier to understand).
I have changed 3 points:
1-
this.stage.addEventListener (MouseEvent.CLICK, nextc);
2-
function returnb54a(event:MouseEvent):void
{
event.stopImmediatePropagation();
prevFrame();
}
3-
function nextc(event:MouseEvent): void
{
event.stopImmediatePropagation();
nextFrame();
}
The method: stopImmediatePropagation() prevents processing of any event listeners in the current node and any subsequent nodes in the event flow. This method takes effect immediately, and it affects event listeners in the current node. In contrast, the stopPropagation() method doesn't take effect until all the event listeners in the current node finish processing.
Try to implement these changes and see if will have the desired result.

How to capture all keyboard input to a custom element?

Here is my problem: I've got a swf loaed inside my loader an in that swf I have a keylistener:
stage.addEventListener(KeyboardEvent.KEY_DOWN, this.__onKeyDown, false, int.MAX_VALUE);
Now I'm adding a TextInput to this stage and I would like this input to catch all keyboard events while I'm focusing it. Is it possible to do so that native __onKeyDown will not fire until my TextInput has lost focus?
Thank you for your answers and sorry for my bad english.
You could give your listener higher priority (which you are), and stopAllPropogation in your handler. I've never tried this with an embedded swf so if it doesn't work right away, you could also try listening to the event in the capture phase (third parameter in addEventListener).
function __onKeyDown(e:KeyboardEvent):void {
e.stopImmediatePropagation();
//rest of you handler code here
}

AS3 capture keypress regardless of the current focus?

When I add a keypress event listener to the stage, it does not fire when the focus is on another object. How can I listen for any keypress event regardless of the focus?
Just setting the useCapture parameter to true does not work . I think it's because no event is bubbled up if I do not add a listener to every object on the stage.
Do I really have to add an eventlistener to every object to capture a keypress event regardless of the current focused object?
I think if you add your keyEventListener to the stage and then in functions where the focus is changed add the code:
stage.focus = stage
it might work.

Communicating to Parent SWF

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.