Actionscript button linking issue - actionscript-3

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);

Related

How to handle error dialog boxes in ActionScripts 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.

How do you add a link in a swf file?

I am using HTML and I want to embed it and link it to my other webpages. It is in offline mode and I've been searching a lot and found this:
MyClickTagButton.addEventListener(
MouseEvent.CLICK,
function():void {
if (root.loaderInfo.parameters.clickTAG.substr(0,5) == "http:" || root.loaderInfo.parameters.clickTAG.substr(0,6) == "https:") {
navigateToURL(
new URLRequest(root.loaderInfo.parameters.clickTAG), "_blank"
);
}``
}
);
But what I don't understand is how to put my link. Because my website links are only "person.html", "welcome.html", etc. If you know how, please help me. I'll really appreciate it.
You need to open it in Adobe Flash builder (or other swf (Shock Wave Flash) editors) and add the Actionscript 3 to create an event handler for the movie clip and add the URL:
<pre>
ACTIONSCRIPT 3 CODE:
var mySWF:String = "Your SWF movie clip name here";
mySWF.addEventListener(MouseEvent.CLICK, goToUrl);
function goToUrl:void {
var url:String = "Your URL here";
var request:URLRequest = new URLRequest(url);
try {
navigateToURL(request, '_blank');
} catch (e:Error) {
trace("Error occurred!");
}
}
</pre>

How to create two buttons: when one is pushed other goes up

How to create two buttons: one button is pushed other goes up. I tried using this:
//Create male/female button objects
var maleButton:maleButtonObejct = new maleButtonObject();
var femaleButton:femaleButtonObject = new femaleButtonObject();
//Add evnet listeners to both buttons
maleButton.addEventListener(MouseEvent.CLICK, setToMale);
femaleButton.addEventListener(MouseEvent.CLICK, setToFemale);
//Create isMale variable
var isMale:Boolean;
//Male/Female button functions
function setToMale(event:MouseEvent):void {
isMale = true;
maleButton.stop(3);
femaleButton.stop(1);
}
function setToFemale(event:MouseEvent):void {
isMale = false;
femaleButton.stop(3);
maleButton.stop(1);
}
I get runtime error: TypeError: Error #1006: stop is not a function.
at Untitled_fla::MainTimeline/setToFemale()
Rename your classes starting with a capital letter, such as MaleButton.
Here is an easy way to accomplish what you want:
var maleButton:MaleButton = new MaleButton();
var femaleButton:FemaleButton = new FemaleButton();
maleButton.addEventListener(MouseEvent.CLICK, clicked);
maleButton.addEventListener(MouseEvent.CLICK, clicked);
function clicked(event:MouseEvent):void
{
switch(event.target)
{
case maleButton:
maleButton.gotoAndStop(3);
femaleButton.gotoAndStop(1);
break;
case femaleButton:
maleButton.gotoAndStop(1);
femaleButton.gotoAndStop(3);
break;
default:
return;
}
}

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 3 drop down menu link error

I'm having issues with the following menu. I've been adapting a script from kirupa into actionscript 3.
When I get to the last level of the menu it won't link properly. It always takes the last url of the bunch as the url for all links in that branch of the tree.
Can anyone help me get it to link properly? A zip with the fla and the xml can be found at the following link.
http://www.jdviz.com/projects/xmlmenu.zip
Thanks,
There's a problem with the closures at the end of the code. The current button is not identified properly.
if (node_xml.childNodes[i].nodeName != "resource") {
//cleared the code for clarity...
} else {
curr_item.arrow.visible = false;
curr_item.addEventListener(MouseEvent.MOUSE_DOWN, function(e:MouseEvent):void {
trace(curr_item.urlLink);
});
}
change the above to:
var currentButton:MenuItem_mc = new MenuItem_mc();
function mouseOverHandler(e:MouseEvent ):void
{
currentButton = e.currentTarget as MenuItem_mc;
currentButton.addEventListener( MouseEvent.CLICK , clickHandler );
}
function clickHandler(e:MouseEvent):void
{
var btn:MenuItem_mc = event.currentTarget as MenuItem_mc;
trace( btn.urlLink );
}
if (node_xml.childNodes[i].nodeName != "resource") {
//cleared the code for clarity...
} else {
curr_item.arrow.visible = false;
curr_item.addEventListener(MouseEvent.MOUSE_DOWN, mouseOverHandler );
}