Actionscript 3 drop down menu link error - actionscript-3

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

Related

Convert code sniplet gotoandstop to if else coondition

i just want to ask how can i make a code without needing the code sniplet gotoandstop when clicking button, i want to make if else statement where if he click this called "answerb.btn" he will go to the destined frame.
Well im thinking this code
If (answer.btn = onPress) {
gotoAndStop(2);
}
But somehow ita wrong code i dont know what code to use for pressing button. Pls enlighten me ty
You should have an event listener. Here is an example of the way you could to this:
button.addEventListener(MouseEvent.CLICK, onClick);
private function onClick(e:MouseEvent):void
{
//your logic here
this.gotoAndStop(2);
};
Here is an example from one of my projects which required text to be entered into blocks before proceeding, but it used if statements with buttons presses so you could modify it to suit your needs as you wish!
stop();
var names:String;
var id:String;
var supervisor:String;
namein.addEventListener(TextEvent.TEXT_INPUT,paramChanged);
idin.addEventListener(TextEvent.TEXT_INPUT,paramChanged);
supervisorin.addEventListener(TextEvent.TEXT_INPUT,paramChanged);
beginbutton.enabled = false;
function paramChanged(event:TextEvent):void
{
if (namein.text != "" && idin.text != "" && supervisorin.text != "" &&
namein.length >=5 &&
idin.length >=5 &&
supervisorin.length >=5)//add your other fields here
{
beginbutton.enabled = true;
beginbutton.addEventListener(MouseEvent.CLICK,
fl_ClickToGoToAndPlayFromFrame);
}
else
{
beginbutton.enabled = false;
//If something changes that means we now fail the test you will want to disable the button again
}
}
function fl_ClickToGoToAndPlayFromFrame(event:MouseEvent):void
{
names = namein.text;
id = idin.text;
supervisor = supervisorin.text;
gotoAndPlay(15);
}

Alternative array navigation hack in AS3 not running as defined :(

for some reasons, this code does not want to work; its supposed to be a convenient alternative to using "arrays" for navigation buttons whereby the clicking of one button removes the click state from the rest -
nav_mc.buttonMode=true;
nav_mc.addEventListener(MouseEvent.MOUSE_OVER, navOver);
nav_mc.addEventListener(MouseEvent.MOUSE_OUT, navOut);
nav_mc.addEventListener(MouseEvent.CLICK, navClick);
nav_mc.nav1_mc.mouseChildren=false;
nav_mc.nav2_mc.mouseChildren=false;
nav_mc.nav3_mc.mouseChildren=false;
nav_mc.nav4_mc.mouseChildren=false;
var currentNav:MovieClip;
function navOver(e:MouseEvent):void {
var navItem:MovieClip=e.target as MovieClip;
trace(navItem.name);
if (navItem!=currentNav) {
navItem.gotoAndStop(2);
}
}
function navOut(e:MouseEvent):void {
var navItem:MovieClip=e.target as MovieClip;
if (navItem!=currentNav) {
navItem.gotoAndStop(1);
}
}
function navClick(e:MouseEvent):void {
var navItem:MovieClip=e.target as MovieClip;
if (currentNav!=null) {
navItem.gotoAndStop(1);
}
currentNav=navItem;
navItem.gotoAndStop(3);
}
please, been on this for hours now, what am I missing out?
My guess is: you should change the navItem variable to the currentNav, inside of the if block here:
if (currentNav!=null) {
navItem.gotoAndStop(1);
}
It looks like you want to make some changes to the current active item (currentNav).
UPD.: My advice is: try to change the code above to the next code
if (currentNav!=null) {
currentNav.gotoAndStop(1);
}
UPD 18-11-2015, Toggle buttons:
It's hard to say if my code works or not (I don't have the whole project to test it), but it looks like it should.
function navClick(e:MouseEvent):void
{
var navItem:MovieClip=e.target as MovieClip;
if (currentNav != null)
{
currentNav.gotoAndStop(1);
}
if(currentNav == navItem)
{
currentNav = null;
}else
{
currentNav = navItem;
navItem.gotoAndStop(3);
}
}

Flash cs6 AS3 Error #2007: Parameter child must be non-null

I just implemented that last piece of code you sent over - many thanks!!
This is the FULL context of this frame, the complete code with the other buttons as well, in case that is causing the problem. The 19 errors I get with this piece of code is:
1120: Access of undefined property fl_ProLoader_01
stop();
//home button
mythbutt_home.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_01_1,false,0,true);
function fl_ClickToLoadUnloadSWF_01_1(event:MouseEvent):void
{
removeChild(fl_ProLoader_01);
fl_ProLoader_01.unloadAndStop();
fl_ProLoader_01 = null;
}
mythbutt_home.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_01_1,false,0,true);
function fl_ClickToGoToAndStopAtFrame_01_1(event:MouseEvent):void
{
removeChild(fl_ProLoader_01);
}
mythbutt_home.addEventListener(MouseEvent.CLICK, fl_ClickToStopAllSounds_01_1,false,0,true);
function fl_ClickToStopAllSounds_01_1(event:MouseEvent):void
{
SoundMixer.stopAll();
}
mythbutt_home.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_01_2,false,0,true);
function fl_ClickToGoToAndStopAtFrame_01_2(event:MouseEvent):void
{
gotoAndStop(1);
}
//other buttons at the bottom
mythbutt_aboriginal_culture.addEventListener(MouseEvent.CLICK, fl_ClickToGoToWebPage_01_1);
function fl_ClickToGoToWebPage_01_1(event:MouseEvent):void
{
navigateToURL(new URLRequest("http://www.bigmyth.com/fullversion/password033/download/ABORIGINAL_CULTURE.pdf"), "_blank");
}
mythbutt_aboriginal_pantheon.addEventListener(MouseEvent.CLICK, fl_ClickToGoToWebPage_01_2);
function fl_ClickToGoToWebPage_01_2(event:MouseEvent):void
{
navigateToURL(new URLRequest("http://www.bigmyth.com/fullversion/password033/download/ABORIGINAL_PANTHEON.pdf"), "_blank");
}
mythbutt_aboriginal_exercises.addEventListener( MouseEvent.CLICK, fl_ClickToGoToWebPage_01_3);
function fl_ClickToGoToWebPage_01_3(event:MouseEvent):void
{
navigateToURL(new URLRequest( "http://www.bigmyth.com/fullversion/password033/download/ABORIGINAL_EXERCISES.pdf"), "_blank");
}
//start button
//Change your event handler function.
start_button_aboriginal.addEventListener(MouseEvent.CLICK,fl_ClickToLoadSWF_01_2);
function fl_ClickToLoadSWF_01_2(event:MouseEvent):void {
fl_ProLoader_01=new ProLoader ;
fl_ProLoader_01.load(new URLRequest("myths/myth_aboriginal.swf"));
fl_ProLoader_01.contentLoaderInfo.addEventListener(Event.COMPLETE,
//Using closure callback instead of *onComplete_1* function
function( e : Event ) {
e.currentTarget.content.addEventListener( Event.ENTER_FRAME, OEF_01);
});
addChild(fl_ProLoader_01);
fl_ProLoader_01.x=323;
fl_ProLoader_01.y=41;
//Swap the event handlers,no need for flag,clear code blocks
start_button_aboriginal.removeEventListener(MouseEvent.CLICK,fl_ClickToLoadSWF_01_2);
start_button_aboriginal.addEventListener(MouseEvent.CLICK,fl_ClickToUnLoadSWF_01_2);
}
function fl_ClickToUnLoadSWF_01_2(event:MouseEvent):void {
fl_ProLoader_01.removeEventListener(Event.ENTER_FRAME,OEF_01);
removeChild(fl_ProLoader_01);
fl_ProLoader_01.unloadAndStop();
fl_ProLoader_01=null;
start_button_aboriginal.removeEventListener(MouseEvent.CLICK,fl_ClickToUnLoadSWF_01_2);
}
function OEF_01(e:Event):void {
if (e.currentTarget.currentFrame==e.currentTarget.totalFrames) {
e.currentTarget.stop();
fl_ClickToUnLoadSWF_01_2(null);
}
}
I think #GarryWong is right. If you click the start button after the external swf has been loaded and before his timeline has finished you will get an error.
I've changed your implementation, please try this.
No need to check for null object.
UPDATE
//Change your event handler function.
startbutton.addEventListener(MouseEvent.CLICK,fl_ClickToLoadSWF_1_2);
function fl_ClickToLoadSWF_1_2(event:MouseEvent):void {
fl_ProLoader_1=new ProLoader();
fl_ProLoader_1.load(new URLRequest("myths/myth_aboriginal.swf"));
fl_ProLoader_1.contentLoaderInfo.addEventListener(Event.COMPLETE,
//Using closure callback instead of *onComplete_1* function
function( e : Event ) {
e.currentTarget.content.addEventListener(Event.ENTER_FRAME, OEF_1);
});
addChild(fl_ProLoader_1);
fl_ProLoader_1.x=207;
fl_ProLoader_1.y=41;
//Swap the event handlers,no need for flag,clear code blocks
startbutton.removeEventListener(MouseEvent.CLICK,fl_ClickToLoadSWF_1_2);
startbutton.addEventListener(MouseEvent.CLICK,fl_ClickToUnLoadSWF_1_2);
}
function fl_ClickToUnLoadSWF_1_2(event:MouseEvent):void {
fl_ProLoader_1.removeEventListener(Event.ENTER_FRAME,OEF_1);
removeChild(fl_ProLoader_1);
fl_ProLoader_1.unloadAndStop();
fl_ProLoader_1=null;
startbutton.removeEventListener(MouseEvent.CLICK,fl_ClickToUnLoadSWF_1_2);
//Use the below line only if you want to repeat the procedure of loading the *Proloader*, otherwise omit it.
startbutton.addEventListener(MouseEvent.CLICK,fl_ClickToLoadSWF_1_2);
}
function OEF_1(e:Event):void {
if (e.currentTarget.currentFrame==e.currentTarget.totalFrames) {
e.currentTarget.stop();
fl_ClickToUnLoadSWF_1_2(null);
}
}
Sorry but I haven't Flash installed in my PC right now so maybe there will be some mistakes in my code. You made me boot my Windows partition!

manually set check box selected in as3

I have a CheckBox in as3 that i need to set as checked with code. Do I dispatch an event? If so, how do I create an event, and give it a target (target is read only). Or is there another way to do it? thanks.
my_checkbox.selected = true;
And of course:
my_checkbox.selected = false;
To handle the box being checked/unchecked:
my_checkbox.addEventListener(MouseEvent.CLICK, _selected);
function _selected(e:MouseEvent):void
{
var bool:Boolean = e.target.selected;
if(bool)
{
trace("was checked");
// more code
}
else
{
trace("was unchecked");
// more code
}
}

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