How to handle error dialog boxes in ActionScripts 3 - actionscript-3

I write some code to check an XML from a URL, it works when Internet connection exist, but when i manually disable internet for test an Error dialog box show, i coudn't handle it with try() catch().
try {
var myXML: XML = new XML();
var XML_URL: String = "https://drive.google.com/uc?export=download&id=0B5pkl4TJ7V0OTFBPanUyMWQxUnM";
var myXMLURL: URLRequest = new URLRequest(XML_URL);
var myLoader: URLLoader = new URLLoader(myXMLURL);
myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
function xmlLoaded(event: Event): void {
myXML = XML(myLoader.data);
trace("LOCK CODE: " + myXML.LOCK);
if (myXML.LOCK == 0) {
gotoAndStop(71);
};
}
} catch (e: Error) {
gotoAndStop(71);
trace("FAILED TO CHECK LOCK.");
} finally {
gotoAndStop(71);
trace("FAILED TO CHECK LOCK.");
}
How can i hide this dialog from user?

Add an URLLoader event listener for IOErrorEvent.IO_ERROR
e.g.
myLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
}
You should handle all events as good measure anyway (e.g. security error, etc.)
The as3 docs example provides a helpful snippet
Ideally you would not only silently trace message, but hopefully display helpful feedback or placeholder content for the user to understand what to expect.

Related

Actionscript: SWF file does not record data to txt, while the compiler does

I am trying to create a psychological experiment in ActionScript. The performance of the participants is to be stored in a separate .csv file. I have written this code (apparently, instead of "This is the text to write" there is going to be a data array, but the problem appears with this code on equal parts)
import flash.net.FileReference;
import flash.events.Event;
var hint:String = "Please give a name for the file";
var labelling:String;
experiment_label.addEventListener(KeyboardEvent.KEY_UP, enter_pressed)
function enter_pressed (event:KeyboardEvent):void{
if (event.keyCode == 13) {
labelling = experiment_label.text;
addEventListener(Event.ENTER_FRAME,saveFile);
var ss:String = "this is text to write";
var fileRef:FileReference;
fileRef = new FileReference();
function saveFile(event:Event):void
{
fileRef.save(ss, labelling+".csv");
removeEventListener(Event.ENTER_FRAME, saveFile);
}
}
}
The problem I am facing is as follows: when I run it in from under Flash, it operates perfectly, and the save window does pop up. However, if I run an .swf file separately, it just ignores saving command.
Could you kindly suggest, what can I do? Or maybe I should use a different approach to saving altogether?
You current code will give you an Error #2176 (use a try ... catch to catch it) because FileReference.save()
is not called in response to a user action, such as a mouse event or keypress event.
To avoid that, you have to remove the Event.ENTER_FRAME event listener, even you don't need it :
function on_KeyUp(event:KeyboardEvent):void
{
if (event.keyCode == 13)
{
save_File();
}
}
function save_File():void
{
try
{
var fileRef:FileReference = new FileReference();
fileRef.save('some text here', 'my_file.csv');
}
catch (e:Error)
{
trace(e.toString());
}
}
Hope that can help.

Flash object cannot connect to remote server

My company design digital signage solutions for companies. The animated widgets are typically produced in Flash.
In a current project I'm experiencing a bit of an odd issue. The screen is split into 3 areas: Video, RSS Ticker, and Weather. The RSS and Weather objects are created using Flash. Each object is it's own Flash file.
Both Flash object utilize the same method to connect to the remote servers:
function loadWeatherFromUrl():void
{
var urlRequest:URLRequest = new URLRequest("MY URL");
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, completeHandler);
try{
urlLoader.load(urlRequest);
} catch (error:Error) {
trace("Cannot load : " + error.message);
}
}
function completeHandler(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
//Do stuff with the data...
}
When testing in the IDE, both the Weather object and the RSS object work fine, as seen below:
Once deployed to the signage software it doesn't work anymore. However, the Weather object works as expected. The Flash object doesn't encounter a URL loading error either. Compare pic #2 and pic #3 to see how I view errors.
If I try to preview both the Weather object and the RSS object locally in a web-browser (IE, Firefox), I do get local permission errors, as expected.
So any ideas why one would work and the other wouldn't? If it had something to do with CORS (Cross Origin Resource Sharing) wouldn't both fail? I have tried both options (local only / network only) in the Publish -> Playback settings.
The weather object is powered by Yahoo:
Yahoo Weather
and the RSS object is powered by CTV News (Canadian Television Network):
CTV RSS Feed
EDIT:
For the RSS feed (above I used the Weather object's methods, they're identical except the function name and error output) I have the following:
At run time
error_msg.text = "Loading...";
Then the URL loading:
function loadFeedFromUrl():void
{
var urlRequest:URLRequest = new URLRequest("http://ottawa.ctvnews.ca/rss/ctv-news-ottawa-1.1245493");
error_msg.text = "";
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, completeHandler);
try{
urlLoader.load(urlRequest);
} catch (error:Error) {
trace("Cannot load : " + error.message);
error_msg.text = error.message;
}
}
EDIT #2
Here is the UPDATED loading function, as well as the basic error handling functions.
function loadFeedFromUrl():void
{
var urlRequest:URLRequest = new URLRequest("http://ottawa.ctvnews.ca/rss/ctv-news-ottawa-1.1245493");
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, completeHandler);
try{
urlLoader.load(urlRequest);
} catch (error:Error) {
trace("Cannot load : " + error.message);
error_msg.text = error.message;
}
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandlerFunction);
urlLoader.addEventListener(IOErrorEvent.NETWORK_ERROR, ioErrorHandlerFunction);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandlerFunction);
}
function ioErrorHandlerFunction() {
error_msg.text = "IO Error!";
}
function securityErrorHandlerFunction() {
error_msg.text = "Security Error!";
}
The error_msg textfield stays "Loading...", so it doesn't appear any of those error types are being received.

How do I use this URLRequest script?

I started learning ActionScript 3 a week ago and have stumbled across a huge learning curve. I found this script on the internet:
var _loader:URLLoader;
var _request:URLRequest;
function loadData():void {
_loader = new URLLoader();
_request = new URLRequest("http://www.travoid.com/game/Purchase.php?gid=1");
_request.method = URLRequestMethod.POST;
_loader.addEventListener(Event.COMPLETE, onLoadData);
_loader.addEventListener(IOErrorEvent.IO_ERROR, onDataFailedToLoad);
_loader.addEventListener(IOErrorEvent.NETWORK_ERROR, onDataFailedToLoad);
_loader.addEventListener(IOErrorEvent.VERIFY_ERROR, onDataFailedToLoad);
_loader.addEventListener(IOErrorEvent.DISK_ERROR, onDataFailedToLoad);
_loader.load(_request);
}
function onLoadData(e:Event):void {
trace("onLoadData",e.target.data);
}
function onDataFailedToLoad(e:IOErrorEvent):void {
trace("onDataFailedToLoad:",e.text);
}
This all seems to work and is generating no errors or output, however my issue comes about when I use this next part of code (which I made)
function vpBuy(e:MouseEvent):void{
loadData();
if (e.target.data == "false") {
inf_a.visible = true;
inf_b.visible = true;
inf_c.visible = true;
inf_d.visible = true;
btn_ok.visible = true;
}
}
I get this error:
ReferenceError: Error #1069: Property data not found on
flash.display.SimpleButton and there is no default value. at
travoid_fla::MainTimeline/vpBuy() onLoadData
The part that is probably throwing this is:
if (e.target.data == "false") {
I was hoping e.target.data was what stored the value on the web page (which displays as false) but apparently not. With the code I found on the internet, what stores the information on the web page?
Thanks,
Ethan Webster.
The URLLoader load method is asynchronous, you have to wait the server response before triyng to get the result.
The functions onLoadData and onDataFailedToLoad are there to do that. When the response is well received the function onLoadData is called and you can get the data in e.target.data or _loader.data
The error in your function vpBuy is you try to access the data property on the object that triggered the MouseEvent (maybe a Button) and that object don't have such variable.
Try the following:
/** button clicked load the datas from the server **/
function vpBuy(e:MouseEvent):void
{
// load the datas from the server
loadData();
}
/** the datas are well loaded i can access them **/
function onLoadData(e:Event):void
{
trace("onLoadData",e.target.data);
if( e.target.data == "false" )
{
inf_a.visible = true;
inf_b.visible = true;
inf_c.visible = true;
inf_d.visible = true;
btn_ok.visible = true;
}
}
Hope this could help you :)

Actionscript 3 Sound ioError Problem

i am having trouble with sound. i am loading it dynamically, url comes from flashvars.
App is working actually but stil gives error, unhandled ioError. but i already handled it.
`
var sound:Sound = new Sound()
try{
sound.load(new URLRequest(req));
} catch(e:IOError){
trace("catch ioerror");
}
sound.addEventListener(IOErrorEvent.IO_ERROR, function(evt:IOErrorEvent):void { trace("error:",evt) } );
sound.addEventListener(Event.COMPLETE, function(e:Event):void{
channel = sound.play(0,int.MAX_VALUE);
});`
Just to rule it out, try commenting out the line channel = sound.play(0,int.MAX_VALUE); perhaps it is this that is throwing the error and not the load, and you have no catch around this.
Try a neater approach in terms of code, might just have issues with your layout and such:
var request:URLRequest = new URLRequest(req);
sound.load(request);
sound.addEventListener(IOErrorEvent.IO_ERROR, _ioError);
sound.addEventListener(Event.COMPLETE, _complete);
function _ioError(e:IOErrorEvent):void
{
trace("File was not found");
_removeListeners();
}
function _complete(e:Event):void
{
channel = sound.play(0,int.MAX_VALUE);
_removeListeners();
}
function _removeListeners():void
{
sound.removeEventListener(IOErrorEvent.IO_ERROR, _ioError);
sound.removeEventListener(Event.COMPLETE, _complete);
}

Actionscript button linking issue

So this doesn't make any sense. I have actionscript in a flash-based button menu, and one of the buttons is linking to the wrong page, and i cannot figure out why. Here is the actionscript:
var myURL1:URLRequest = new URLRequest ("home.html");
home_btn.addEventListener(MouseEvent.CLICK, home_btnEventHandler);
function home_btnEventHandler(event:MouseEvent):void
{
navigateToURL(myURL1, "_self");
}
var myURL2:URLRequest = new URLRequest ("featuredwork.html");
work_btn.addEventListener(MouseEvent.CLICK, work_btnEventHandler);
function work_btnEventHandler(event:MouseEvent):void
{
navigateToURL(myURL2, "_self");
}
var myURL3:URLRequest = new URLRequest ("featuredartist.html");.
artist_btn.addEventListener(MouseEvent.CLICK, artist_btnEventHandler);
function artist_btnEventHandler(event:MouseEvent):void
{
navigateToURL(myURL3, "_self");
}
var myURL4:URLRequest = new URLRequest ("artists.html");
members_btn.addEventListener(MouseEvent.CLICK, members_btnEventHandler);
function members_btnEventHandler(event:MouseEvent):void
{
navigateToURL(myURL4, "_self");
}
var myURL5:URLRequest = new URLRequest ("events.html");
events_btn.addEventListener(MouseEvent.CLICK, events_btnEventHandler);
function events_btnEventHandler(event:MouseEvent):void
{
navigateToURL(myURL5, "_self");
}
var myURL6:URLRequest = new URLRequest ("/blog/index.php");
blog_btn.addEventListener(MouseEvent.CLICK, events_btnEventHandler);
function blog_btnEventHandler(event:MouseEvent):void
{
navigateToURL(myURL6, "_self");
}
Now, when I click on blog_btn, it is sending me to the "events" page. It makes no sense. Does anybody have any idea?
Fairly easy to spot: you have
blog_btn.addEventListener(MouseEvent.CLICK, events_btnEventHandler);
when you mean
blog_btn.addEventListener(MouseEvent.CLICK, blog_btnEventHandler);
notice the second parameter.
You've bound the events handler to the blog_btn click - change the last block to point to the correct handler:
blog_btn.addEventListener(MouseEvent.CLICK, blog_btnEventHandler);