Run function right before the exit of the app [AS3] - actionscript-3

i have a question. If i have my flash application on a website, is it possible to run a function right before the person closes the website window? Some help would be useful :) thanks in advance.

You can use javascript to catch event when window is closing and invoke a function in actionscript through ExternalInterface.
Check this answer to see how to catch event :
How to capture the browser window close event?
Check this to see how to invoke function in AS3 from JS :
Javascript call AS3 function

Related

Why is the motion event not triggered?

I have a problem while trying to get the accelerometer data using the HTML 5 interface.
I declare this handler event:
window.addEventListener("devicemotion",getMontionData,true);
to get the data provided by the accelerometer sensor. On the other hand, I have a function called as getMontionData:
getMontionData = function (e)
{
xAcceleration = e.acceleration.x;
yAcceleration = e.acceleration.y;
zAcceleration = e.acceleration.z;
}
to get the accelerometer data. The problem that I have is that the getMontionData function is not called when the device is locked or the screen is on black.
I'm doing this test on a smart watch using Tizen IDE and it is set this property
Could anybody explain me how to enable the application to get data even though the device is locked or the screen is on black?
While the application main window is displayed, the devicemotion event should be called since the getMontionData function is called. The problem happens, when the device screen is on black
Thank you so much
Try adding this line to test if event gets triggered, if it does - find out why it's not being fired.
$(document).trigger('devicemotion');
Try adding the following setting to your config.xml as described in the documentation:
<tizen:setting background-support="enable" />
Without this the application can't run in the background, so the event doesn't work.

Actionscript works when tested in flash, but not on html page?

I am trying to create an ad for a website. When someone clicks on the ad, it is supposed to redirect them to a website, and register the click with google analytics.
I have done this with the following script:
import flash.external.ExternalInterface;
movieClip_3.addEventListener(MouseEvent.CLICK, onClick);
function onClick(event:MouseEvent):void {
trace("hi");
ExternalInterface.call("console.log", "test");
//ExternalInterface.call("_gaq._trackPageview", "/vpv/annoncer/[firmanavn.dk]");
navigateToURL(new URLRequest("http://www.google.com"), "_blank");
}
When i run this using preview->flash and i click on the surface, (where there is a big red square called movieClip_3) It opens the webpage. However when i try to publish as html, the big red square shows, but nothing happens on click. Not even console.log. I have tried setting allowscriptaccess = always but that does not change anything.
Can you guys help me? Any help is appreciated.
Security problems?
Developers should validate all URLs before passing them to this
function.
For local content running in a browser, calls to the navigateToURL()
method that specify a "javascript:" pseudo-protocol (via a URLRequest
object passed as the first parameter) are only permitted if the SWF
file and the containing web page (if there is one) are in the
local-trusted security sandbox. Some browsers do not support using the
javascript protocol with the navigateToURL() method. Instead, consider
using the call() method of the ExternalInterface API to invoke
JavaScript methods within the enclosing HTML page.
source: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/package.html#navigateToURL()
EDIT:
Since javascript is not permitted out of the sandbox, you can try with ExternalInterface:
ExternalInterface.call("javascript_functionname", "mypage.html");
In the parameters for publishing:
'allowScriptAccess', 'always',
You can only test this on your server not locally.
I'd suggest double checking the security settings (right click on flash container->Global Settings-> Advanced -> Trusted Location Settings). Also make sure your html file contains the javascript function you're trying to execute and look for blocked pop-up notifications in the browser. Maybe you just don't allow pop-ups to run.

Seek function don´t work on flex videoPlayer after the video finish

The video seeks normally when first playing but when it finishes i can´t seek the video again. I used a listener to call a function after the event TimeEvent.COMPLETE occurs(video finishes). In the function i call the function player.seek(20) for example but it doesn´t work. The video keeps in the end of it. Have anyone faced this problem?
what type of object are you attaching the listener event to? "Complete" works differently on different objects so you may have to catch the event sooner

Simulate mouse click AS3

I'm having trouble simulating a click call to a button(displayObject) thats generated via an API call( youtube as3 API). I have not seen any mention of security reasons as to why I can not simulate a click as long as something is registered with a click handler.
Basically I checked to make sure the button made is listening to a mouse click event with:
trace(generatedButton.hasEventListener(MouseEvent.CLICK)) which returns true
I proceed to than call this:
generatedButton.dispatchEvent( new MouseEvent(MouseEvent.CLICK, true) );
And nothing happens yet if I physically click the button it works. Is there some security measure that prevents something from being fake clicked unless its origin is strictly from the system mouse?
I even set a timeout call on the click function and moved my cursor over the button and let it fire in case it was an issue of the mouse having to over the object but still nothing. I am kind of stumped at this point.
Any help would be appreciated!
Ok well I made a quick FLA and wrote this code and the dispatch Mouse event works perfectly..
import flash.events.MouseEvent;
myButton.addEventListener(MouseEvent.CLICK, myClickHandler);
function myClickHandler(e:MouseEvent):void
{
trace("clicked");
}
setTimeout(function()
{
myButton.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
},2000);
YouTube api must block against something like this
The following code works fine in my local sandbox.
btn.addEventListener( MouseEvent.CLICK, test )
function test( e:Event ):void{
trace('asdf');
}
btn.dispatchEvent( new MouseEvent( MouseEvent.CLICK ) );
So I can only come to 2 conclusions.
First is a security issue. Flash has some security issues with click events triggering the FileReference class where it can only be used if the stack has a user click in it. This issue may carry over to any artificial dispatching of that event. It works in my sandbox because the restriction doesn't apply here.
The second one is you are dispatching the event to soon and the buttons listener hasn't been added to the button from the api.
In this case try calling the dispatch after the stage render event has been called.
stage.addEventListener( Event.RENDER, test )
function test( e:Event ):void{
btn.dispatchEvent( new MouseEvent( MouseEvent.CLICK ) );
}
Just my guess anyway.
instead of doing a dispatchEvent, which will eventually call a method anyway, just call the method and since its expecting a MouseEvent, do yourClickMethod(null)
You can't simulate click events on a server, it is because security issues. Instead of dispatchingEvent you can call method directly. I ll look for the description but its illegal. You can check firefox firebug logs for error description.

Double right click mouse event in AS3?

It it possible to catch a double right click event in Actionscript 3?
It is with AIR. You can't capture RIGHT_CLICK in regular AS3, unfortunately.
There is, however, always the possibility of capturing right click events in JavaScript, and using ExternalInterface to call an event handler in the Flash program. See this blog, for example.
Actually you can catch RIGHT_CLICK at the moment :
You need player >11.2
Add additional compiler options -swf-version=19
Use this code :
stage.doubleClickEnabled = true;
stage.addEventListener(MouseEvent.RIGHT_CLICK, handler);
In handler you can save time of previous click, compare with second click and if it`s shorter than 0.5 seconds dispatch your own RIGHT_DOUBLE_CLICK event.