A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Simple Array/Loop Question

  1. #1
    Junior Member
    Join Date
    May 2008
    Posts
    17

    Simple Array/Loop Question

    I am trying to have my loop assign all three actions; over, out, and down, to my buttons. The problem is that the button's down functions are different than one another. So I am trying to assign it the functions name + the number it is in the loop. I would think it would look something like this.

    code:
    		var array:Array = [contentWrap.pete_email,contentWrap.wayne_email,con  tentWrap.info_email];
    for (var i:int = 0; i < array.length;i++){
    array[i].stop();
    array[i].addEventListener(MouseEvent.MOUSE_OVER, btnOver);
    array[i].addEventListener(MouseEvent.MOUSE_OUT, btnOut);
    array[i].addEventListener(MouseEvent.MOUSE_DOWN, btnDown+"[i]");
    }

    private function btnDown0(e:MouseEvent):void {
    trace("pete");
    }
    private function btnDown1(e:MouseEvent):void {
    trace("wayne");
    }
    private function btnDown2(e:MouseEvent):void {
    trace("info");
    }


  2. #2
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    for (var i:Number = 0; i < array.length; i++) {

    array[i].stop();

    array[i].addEventListener(MouseEvent.MOUSE_OVER, btnOver);

    array[i].addEventListener(MouseEvent.MOUSE_OUT, btnOut);

    array[i].addEventListener(MouseEvent.MOUSE_DOWN, this["btnDown"+i]);

    }

    Use that for your loop and it'll work. Let me know if you need anything else.

  3. #3
    Junior Member
    Join Date
    May 2008
    Posts
    17
    That worked perfect, thanks so much.

  4. #4
    Senior Member jweeks123's Avatar
    Join Date
    Mar 2006
    Posts
    1,124
    No problem, happy to help.

  5. #5
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    The code's still a bit dirty for a couple of reasons. For one thing, you don't need to call separate functions for the click on each button. And another nicer way of doing it would be to make all the event listeners point to the same function:
    Code:
    array[i].name = String(i); //you'll see why I added this later.
    array[i].addEventListener(MouseEvent.MOUSE_OVER, btnFn);
    array[i].addEventListener(MouseEvent.MOUSE_OUT, btnFn);
    array[i].addEventListener(MouseEvent.MOUSE_DOWN, btnFn);
    Okay, now they all call btnFn().
    Code:
    function btnFn(evt:MouseEvent):void {
      switch (evt.type) {
          case "mouseOver": 
                //do my mouseOver on evt.currentTarget (the button)
                break;
          case "mouseOut":
                ///etc.
                break;
          case "mouseDown":
                //here, evt.currentTarget is the button that was clicked,
                //so we can use it however we want. If it has a .name parameter, 
    ////////////we could check for that. Like:
                if (evt.currentTarget.name == 0) {
                    //whatever you want for that button.
                }
                break;
          }
    }

  6. #6
    Junior Member
    Join Date
    May 2008
    Posts
    17
    That's a pretty cool way for simple buttons. But what if you wanted to make a toggle type of menu where you would disable a button after it was clicked and make it stay in it's highlighted position by removing it's roll out listener?

  7. #7
    Junior Member
    Join Date
    Jun 2008
    Posts
    1

    evt.currentTarget.

    Thanks, I spent hours trying to figure this one out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center