You must be logged in to post Login Register


Search 

adding MOUSE.EVENT actions to Advanced XML Menu with ActionScript 3

User Post

1:39 pm
May 4, 2009


gregtpp

Member


posts 4

1

Hello

The Advanced XML menu tutorial is wonderful. However I'm trying (and failing) to figure out how to add MOUSE.EVENT actions to the Sub Menu. I've tried adding menuItems.addEventListener(MouseEvent.CLICK, menuItemsClicked); and then defining the function menuItemsClicked. Can each individual link (for instance the 1.1/1.2 subnav links off of 1) be given mouse actions? Has anyone successfully done this. Sorry, I'm new to Actionscript, but these tutorials are certainly helping a lot.


4:49 am
May 5, 2009


smonte

Admin


posts 27

2

Post edited 11:50 am – May 5, 2009 by smonte
Post edited 10:05 am – May 11, 2009 by smonte
Post edited 6:33 pm – May 14, 2009 by smonte
Post edited 6:39 pm – May 14, 2009 by smonte
Post edited 6:42 pm – May 14, 2009 by smonte


Hey,

If you want to add individual links to each button you could do something like this.

First add a link attribute to each item (give some URL…)




1.2
1.2


Then in the createMenu() function add a mouse click listener to each menu item.

//This function creates a single menu (= one vertical menu).
//It returns all the menu items which belong to the created menu.
function createMenu(menu:XML):Array {


//Create an array which contains all the items in this menu
var menuItems:Array = new Array();

//Loop through the items found in the menu
for each (var item:XML in menu.item) {

//Create a new menu item
var menuItem:MenuItem = new MenuItem();

//Set the menuItem to have no mouseChildres
menuItem.mouseChildren = false;

//Set the item text
menuItem.menuText.text = item.toString();

//Add a link variable (the URL where this item should link to)
menuItem.link =

//Add a click listener
menuItem.addEventListener(MouseEvent.CLICK, itemClicked);

//Add the item to the menuArray
menuItems.push(menuItem);
}
}


And then create the function itemClicked() and navigate to the specified URL.


function itemClicked(e:Event):void {


//Get the item that dispatched the event
var item:MenuItem = e.target as MenuItem;

//Navigate to the URL that's assigned to the menu item
var urlRequest:URLRequest = new URLRequest(item.link);
navigateToURL(urlRequest);
}


Hope that gets you started!

Regards,

smonte

Regards, smonte

9:57 am
May 6, 2009


gregtpp

Member


posts 4

3

thank you! that's a great start. 

9:48 pm
May 10, 2009


CCRunner524

Member


posts 3

4

Post edited 5:06 am – May 11, 2009 by CCRunner524


When i try this i get,

TypeError: Error #1090: XML parser failure: element is malformed.
 at Button4_fla::MainTimeline/xmlLoaded()
 at flash.events::EventDispatcher/dispatchEventFunction()
 at flash.events::EventDispatcher/dispatchEvent()
 at flash.net::URLLoader/onComplete()


and its because of the link=”http://…”> added to the xml. any idea what i did wrong?Confused


Eventually it stopped giving me that error and i get a new one.


TypeError: Error #2007: Parameter url must be non-null.
 at global/flash.net::navigateToURL()
 at Button4_fla::MainTimeline/itemClicked()

I put a link to all of the items but still get nothing poping up. what should i do?

thanks

2:56 am
May 11, 2009


smonte

Admin


posts 27

5

Hey,


When you have a xml node with an attribute e.g.


1.1

you have to reference it like this:

item.@link

Maybe that was the problem?


(I updated my first post also to make this more clear)


Regards,

smonte

Regards, smonte

11:22 am
May 11, 2009


CCRunner524

Member


posts 3

6

ok so i tried it and got


ReferenceError: Error #1081: Property @link not found on MenuItem and there is no default value.
at Button4_fla::MainTimeline/itemClicked()


I tried it without the @ in the @link and didnt get an error but my browser wouldnt open anything. The link value did fix the original error though.

3:49 am
May 12, 2009


smonte

Admin


posts 27

7

Post edited 10:51 am – May 12, 2009 by smonte


Could you send me your FLA and XML file (smonte(a)flashmymind.com)?

Regards, smonte

9:01 am
May 14, 2009


gregtpp

Member


posts 4

8

I get the same error as well.

ReferenceError: Error #1081: Property @link not found on @link and there is no default value.
at openinganimation_fla::MainTimeline/menuitemClicked()

9:16 am
May 14, 2009


smonte

Admin


posts 27

9

Hey,

I have received your FLA file and I will look at it tomorrow or on Saturday. I first have to finish my exam week!

I will post the solution + .fla file then (if I figure it out Wink).

Regards, smonte

11:33 am
May 14, 2009


smonte

Admin


posts 27

10

Hey,

I have now fixed the code. The problem was in the itemClicked() function. So change the function to look like this:


function itemClicked(e:Event):void {


//Get the item that dispatched the event
var item:MenuItem = e.target as MenuItem;

//Navigate to the URL that’s assigned to the menu item
var urlRequest:URLRequest = new URLRequest(item.link);
(navigateToURL(urlRequest),’_self’);
}


As you can see, I only changed the urlRequest line. The older version (which was causing the problem) was like this:


var urlRequest:URLRequest = new URLRequest(item.@link);


The property “item.@link” doesn’t exist, we only use the “@link” name when we retrive the value from the XML file.

I have also updated my previous post to correct this error. Hope everything is OK now :)

Regards, smonte

11:03 pm
June 5, 2009


jclegg

New Member


posts 1

11

Hi…Love this tutorial! Thanks so much for posting it.


I got mine to work, but I wanted to add links to the main menu items. So I added an event listener for a click on the main menu, but can't figure out how to make it go to a url. I got the ones to work in the drop down menu, but not the main menu.


I was just trying to copy the other and change it to the main menu item insted of menu item but it's not working

//This function is called when a menu's mainItem is Clicked
function mainItemClicked(e:Event):void{
        //Get the item that dispatched the event
        var mainitem:MenuItem = e.target as MenuItem;
       
        //Navigate to the URL that is assigned to the menu item
        var urlRequest:URLRequest = new URLRequest(mainitem.link);
        navigateToURL(urlRequest);
    }

this is my xml


    

Can I not add a link in the menu part of the xml file? I didn't want a drop down on all of them.


Thanks so much for the help!


Topic RSS 
Search