ActionScript 3 External Interface callback is not working? - actionscript-3

I would highly appreciate any help in getting this issue resolved. I have been at it for two days now and have not been able to resolve this issue. I am trying to call an AS3 function from JavaScript via ExternalInterface API which is simply not working at all. If I to call a JavaScript function form html loaded swf, it works beautifully.
I am following this tutorial:
http://www.viget.com/inspire/bi-directional-actionscript-javascript-communication/
I am using swfobject to load the swf into a div.
JavaScript code:
<script type="text/javascript">
swfobject.embedSWF("testRun.swf", "myContent", "550", "400", "9.0.0");
function getFlashMovie(movieName)
{
var isIE = navigator.appName.indexOf("Microsoft") != -1;
return (isIE) ? window[movieName] : document[movieName];
}
function callFlash(flash)
{
//alert(flash);
getFlashMovie(flash).sendToFlash();
}
</script>
AS3 Code:
import flash.external.*;
import flash.events.*;
import flash.text.*;
JAVASCRIPT WILL CALL SENDTOFLASH WHICH THEN INVOKES THE INIT FUNCTION
ExternalInterface.addCallback("sendToFlash", init);
function init():void
{
var txt:TextField = txtField();
txt.appendText("LOOK AT ME!!!");
addChild(txt);
}
function txtField():TextField
{
var txt:TextField = new TextField();
txt.x = 25;
txt.y = 25;
txt.width = 200;
txt.height = 150;
txt.border = true;
txt.text = "INIT ";
return txt;
}

After hours and hours, I have managed to resolved the issue. There was nothing wrong with my code, the culprit was flash player security settings on my local machine. Instead of just adding the swf to my trusted sites with the flash play settings, I included the entire folder, and now it works like a charm.
This is how I changed the settings:
You can go about two ways of changing the settings:
1. when you open the html file, flash player global security box pops open
2. Open the html file, right click and select the 'Global Settings...' option from your context menu.
Once the setting panel is open, you will see four tabs:
Storage | Camer and Mic | Playback | Advanced
Go to 'Advanced' tab
Click on "Trusted Local Settings..."
Click on "Add..."
Click on "Add Folder..."
Locate the folder where you files such as the html and swf are.
Click on "Confirm"
Click on "Close"
And then close the Flash Player Global Settings panel
Now open your html file, if it is already opened then refresh your browser. It all should work now

Related

button to save as the video in the desktop

I use Adobe Flash Professional CS6 + as3 and I have a button to download or save as the video in the desktop but does not execute it? Why ? Where is the mistake?
import fl.video.FLVPlayback;
import flash.display.StageAlign;
import flash.events.Event;
import flash.display.MovieClip;
import flash.display.StageScaleMode;
import flash.media.SoundMixer;
myVideoPlayer.source = "";
addChild (myVideoPlayer);
function loadMyVideo (url:String):void
{
myVideoPlayer.source = url;
myVideoPlayer.visible = true;
}
videosaveas.addEventListener (MouseEvent.CLICK, videosaveas2);
function videosaveas2 (e:Event):void
{
file = new FileReference();
var fileReference:FileReference=new FileReference();
file.save (ByteData, "video.mp4");
}
I don't know if you know the difference between a Flash Player app and an AIR one but I'll put an example for an offline Flash Player app to download a file which is in the same directory as the SWF.
To load the file, I'll use an URLLoader object and then save it using a FileReference one.
Here, don't forget that FileReference.save() is allowed only after a user action (like a mouse click or keypress).
For the example, I'll use the same button for both operations (the user should press the button twice to get the saving file dialog).
var url_loader:URLLoader;
// at the first, we use the button to load the file
btn_download.addEventListener(MouseEvent.CLICK, load_the_file);
function load_the_file(e:MouseEvent): void {
url_loader = new URLLoader();
url_loader.dataFormat = URLLoaderDataFormat.BINARY;
url_loader.addEventListener(Event.COMPLETE, function(e:Event){
btn_download.removeEventListener(MouseEvent.CLICK, load_the_file);
// then we use the same button to download the file
btn_download.addEventListener(MouseEvent.CLICK, download_the_file);
})
url_loader.load(new URLRequest('file_path.ext'));
}
function download_the_file(e:MouseEvent): void {
var file_reference:FileReference = new FileReference()
file_reference.save(url_loader.data, 'default_file_name.ext');
btn_download.removeEventListener(MouseEvent.CLICK, download_the_file);
}
Here, using one button is just an example so you can, for example, show a loading message, use another way to load the file, use two buttons, ...
Then for an online app (a web app), you can use, for example, FileReference.download() :
var file_ref:FileReference = new FileReference();
file_ref.download(new URLRequest('http://www.example.com/file_path.ext'));
For AIR, you have a lot of possibilities using the File, FileStream and the FileReference classes, and you have a lot of example on the net ...
Hope that can help.

Adobe Flash: How to join several separate .swf files into one .exe file?

Noob ask:
I have many separate .swf files, they are like stairs..
for example:
menu.swf load scene1.swf
scene1.swf load scene2.swf
and so on...
There is a button in menu.swf to load scene1.swf and there is a button in scene1.swf to load scene2.swf and so on...
*every .swf file has it own code, I use Adobe Flash CS6 and Actionscript 3.0
I use loader to load those .swf file
var myLoader:Loader = new Loader();
var intro:URLRequest = new URLRequest("Intro.swf");
myLoader.load(intro);
addChild(myLoader);
My problem:
When I create project on menu.swf to menu.exe... press the button... it can't load any .swf file.. T-T"
so.. what should I do..?
here is sample of my project:
https://drive.google.com/folderview?id=0B7S5VF_EUl_dbEg1WmNCYVBfQTA&usp=sharing
*based on my project Little Red Riding Hood.swf is the main menu. It works fine when I run it with .swf
Edit:
This is my full code of the menu, perhaps anyone can help me out.. ^^
*this code works fine when I run it with .swf
btnRead.addEventListener(MouseEvent.CLICK, read);
btnPlay.addEventListener(MouseEvent.CLICK, read);
function read(event:MouseEvent):void {
var myScene1:Loader = new Loader();
var scene1:URLRequest = new URLRequest("scene1.swf");
myScene1.load(scene1);
addChild(myScene1);
}
btnAbout.addEventListener(MouseEvent.CLICK, about);
function about(event:MouseEvent):void {
gotoAndStop(4);
}
btnScene.addEventListener(MouseEvent.CLICK, scene);
btnScene1.addEventListener(MouseEvent.CLICK, scene);
function scene(event:MouseEvent):void {
gotoAndStop(5);
}
import flash.system.fscommand;
btnQuit.addEventListener(MouseEvent.MOUSE_DOWN, closeApp);
function closeApp(event:MouseEvent):void {
fscommand("quit");
}

AS3 Preloader doesn't work locally, loaderInfo ProgressEvent.PROGRESS event does not fire

Making a custom AS3 preloader, I noticed that when my SWF is executed locally (file:///) the preloader gets stuck in the loading screen when previewed in a web browser like Chrome.
When executed from a remote server or through the standalone Flash Player, then it works. I noticed other SWF that have preloaders do not have this issue. What I need to change?
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, preloaderProgress);
function preloaderProgress(event:ProgressEvent):void {
var loadedPercent:Number = event.bytesLoaded/event.bytesTotal*100;
if (loadedPercent == 100){
this.gotoAndStop(2);
}
}
Loading locally goes very fast. Not only you should check if the file is completely loaded using the Event.COMPLETE and not the ProgessEvent but you should also make sure to register your listeners before actually calling load or the file might end up completely loaded before you register your listeners.
Following the example at http://stephenscholtz.com/201110/single-movie-flash-preloading-as3
It seems that there was no ProgressEvent.COMPLETE, but there is a Event.COMPLETE instead, a bit confusing.
I changed my code to the following, (also including some tweaks for right click menu to prohibit the user from right clicking and pressing Play before movie has loaded and such)
//Initialize any variables
var loadedPercent:Number = 0;
//Remove all items from right click Flash Player menu (except Quality, and the mandatory About... & Settings...)
var cxMenu:ContextMenu = new ContextMenu();
cxMenu.hideBuiltInItems();
cxMenu.builtInItems.quality = true;
this.contextMenu = cxMenu;
/* or disable right click menu completely (Flash Player 11.2.202.228 and over) */
/* this.addEventListener(MouseEvent.RIGHT_CLICK, onMouseRightClick);
function onMouseRightClick(event:MouseEvent)
{
return false;
} */
//Disable shortcut keys and window menubar when played from desktop Standalone Flash Player app
if (Capabilities.playerType == "StandAlone" || Capabilities.playerType == "External") {
fscommand("showmenu", "false");
fscommand("trapallkeys", "true");
}
/* Preloader: begin */
//Update loading bar and percent text as SWF loads
function onProgress(e:Event) {
//Get the amount of bytes that have been loaded / bytes total to load
loadedPercent = this.loaderInfo.bytesLoaded/this.loaderInfo.bytesTotal*100;
//Update the text of _preloaderProgress movieclip
_preloaderProgress.text = int(loadedPercent)+"%";
//Update the _preloaderBar amount by scaling horizontally as it loads
_preloaderBar.scaleX = loadedPercent/100;
}
//Go to next frame after everything loads
function init(e:Event) {
gotoAndStop(2);
}
//Attach the events to the stage
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
this.loaderInfo.addEventListener(Event.COMPLETE, init);
/* Preloader: end */
//Stop to 1st frame and wait until it is loaded
stop();
This will allow the movie to play both remotely and locally with no problems.

AS3 navigateToURL only works when testing in Flash CC

I've tried searching stackoverflow to find an answer to this question but so far haven't found anything.
I have a swf file with several movie clips on the stage. When I've managed to create an event where the movie clips swap out to a new movie clip when the user hovers. The problem that I am having is when you click that new movie clip it's supposed to open a URL in a browser window. That happens when I test the movie within Flash CC but when I test it in a browser nothing happens.
Any help for this would be greatly appreciated. I apologize if this question has been asked elsewhere on the site but, as I said, I did a search earlier and didn't find anything.
import flash.events.MouseEvent;
addEventListener(MouseEvent.MOUSE_OUT, gobackAnimation);
function gobackAnimation(event:MouseEvent):void
{
MovieClip(parent).gotoAndStop(1)
}
addEventListener(MouseEvent.CLICK, openURL);
function openURL(event:MouseEvent):void
{
ExternalInterface.call("open", "URL I'm trying to open");
MovieClip(parent).gotoAndStop(1)
}
To open a URL you have to use the navigateToURL function as documented here.
It appears that you are using the following code to trigger a URL to open:
function openURL(event:MouseEvent):void
{
ExternalInterface.call("open", "URL I'm trying to open");
MovieClip(parent).gotoAndStop(1)
}
However, I don't know where or what this ExternalInterface is and how the call function is built.
If you want to open a URL though, you should be doing something along the following lines:
function openURL(event:MouseEvent):void
{
var myURL:String = "http://your.url.goes/here"; // set your url here
var window:String = "_blank"; // you can set this to be whatever you need based on how you want the window opened
var request:URLRequest = new URLRequest(myURL);
request.data = variables;
try {
navigateToURL(request,window);
}
catch (e:Error) {
// handle error here
}
MovieClip(parent).gotoAndStop(1)
}

AS3: use buttons in external swf

I am making a website in Adobe Flash that imports it´s navigation buttons from an external swf-file.
The problem is how my Main FLA-file will know which of the navigation buttons the users has pressed, since the buttons and it's eventListeners are in the external swf-file.
With other word: can I make my external swf-file return a Number to my website FLA-file, to determine which button that have been pressed? If so, how?
You can set up a connection between the external swf and main swf using the LocalConnection()
In main.swf
var sending_lc:LocalConnection;
sending_lc = new LocalConnection();
function send_it(evt:MouseEvent):void
{
sending_lc.send("connectionName", "functionName", "myData");
//params are (connection name, function to execute in the receiving swf, the data to pass through)
}
my_btn.addEventListener(MouseEvent.MOUSE_UP, send_it);
In exetrnal.swf
var receiving_lc:LocalConnection;
receiving_lc = new LocalConnection();
receiving_lc.connect("connectionName");
receiving_lc.client = this;
function functionName(data:String):void {
trace(data);
}