开发者

How do I know which Object I clicked?

Here's the deal: I'm working on a personal portfolio in AS3 and I've run into a problem which I can't seem to find a logical answer to. I want everything (well, most of it) to be editable with an XML file, including my menu. My menu is just a Sprite with some text on it and a Tweener-tween, no big deal. But, I forgot to think of a way how I can determine which menu-item I have clicked.

This is in my Main.as

private function xmlLoaded(e:Event):void {
    xml = e.target.xml;
    menu = new Menu(xml);
    menu.x = 0;
    menu.y = stage.stageHeight / 2 - menu.height / 2;
    addChild(menu);
}

In Menu.as

public function Menu(xml:XML) {
    for each (var eachMenuItem:XML in xml.menu.item) {
        menuItem = new MenuItem(eachMenuItem);
        menuItem.y += yPos;
        addChild(menuItem);
        yPos += menuItem.height + 3;
    }
}

and in my MenuItem.as, everything works - I have a fancy tween when I hover over it, but when I click a menu-item, I want something to appear ofcourse. How do I know which one I clicked? I've tried with pushing everything in an array, but that didn't work out well (or maybe I'm doing it wrong). Also tried a global counter, but that's not working either because the value will always be amount of items in my XML file. Also t开发者_开发知识库ried e.currentTarget in my click-function, but when I trace that, all of them are "Object Sprite".. I need something so I can give each a unique "name"?

Thanks in advance!


you can just set a name variable in the MenuItem class and use that. Then, in the event handler you can use (e.currentTarget as MenuItem).name (or just plain e.currentTarget.name)

By the way, you do know that for each .. in does not guarantee to traverse through the xml in order right?


You might also look into using the Dictionary class - I find myself using it more and more for stuff like this. Basically it acts sort of like an Array, but instead of associating your values with index keys it allows you to map object references to other object references. So for instance, you could set up something like this:

// create your buttons
var button1:MyButton = new MyButton();
var button2:MyButton = new MyButton();
var button3:MyButton = new MyButton();

// declare and instantiate a dictionary object
var links:Dictionary = new Dictionary();

// populate your dictionary object, mapping values to your buttons.
links[button1] = "http://www.button1URL.com";
links[button2] = "http://www.button2URL.com";
links[button3] = "http://www.button3URL.com";

// group buttons into an array for convenience
var buttons:Array = [button1, button2, button3];

// add event listener to your button
for each (var b:MyButton in buttons) {
     b.addEventListener(MouseEvent.CLICK, clickHandler);
}

// handle the button click by getting the relevant information for the clicked button from
// your dictionary object
function clickHandler(e:MouseEvent):void {
     navigateToUrl(new URLRequest(links[e.currentTarget]));
}

Your example is obviously a bit more complex - you'll need to include your URL value or whatever into your XML and then process it that way. Depending on what you're doing, you may not need the dictionary at all - e.currentTarget.MyValue may work just fine for you if the data is included within the button itself.

Hope that helps!


I've got a similar answer but I would use a hard cast MenuItem( e.currentTarget ) instead of a soft cast using the as keyword. The reason being that if you accidentally cast to the wrong type a soft cast will only return null where as a hard cast will give you a proper run-time error and a line number in your code where the problem is.

I'd also suggest adding a public function to MenuItem to get a unique reference or index so you can do something like this in the MenuItem's CLICK event handler

private function onMenuItemClick( event : MouseEvent ) : void{

    // this could be a int, ie. the node index in the XML
    var uniqueSectionReference : String = MenuItem( event.currentTarget ).uniqueReference;

    dispatchEvent( new MenuEvent( MenuEvent.CHANGE, false, false, uniqueSectionReference ) );

    // or if you have a reference in Menu for whatever it is thats controlling your content you could do this

    contentHolder.showSection( uniqueSectionReference );

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜