A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: [RESOLVED] help optimizing code

  1. #1
    ...dishing dimes on an .fla! pointguard's Avatar
    Join Date
    Dec 2001
    Location
    Cambridgeshire, England
    Posts
    1,017

    resolved [RESOLVED] help optimizing code

    Hi...

    Bit stumped as for a better way to write this. I'd be grateful if someone could offer soem advice...
    PHP Code:
    //set up listeners...
    btn1.addEventListener(MouseEvent.ROLL_OVERonRollOverHandler);
    btn1.addEventListener(MouseEvent.ROLL_OUTonRollOutHandler);
    btn1.addEventListener(MouseEvent.CLICKonClickHandler);
    btn1.addEventListener(MouseEvent.MOUSE_UPonReleaseHandler);
    btn2.addEventListener(MouseEvent.ROLL_OVERonRollOverHandler);
    btn2.addEventListener(MouseEvent.ROLL_OUTonRollOutHandler);
    btn2.addEventListener(MouseEvent.CLICKonClickHandler);
    btn2.addEventListener(MouseEvent.MOUSE_UPonReleaseHandler);
    btn3.addEventListener(MouseEvent.ROLL_OVERonRollOverHandler);
    btn3.addEventListener(MouseEvent.ROLL_OUTonRollOutHandler);
    btn3.addEventListener(MouseEvent.CLICKonClickHandler);
    btn3.addEventListener(MouseEvent.MOUSE_UPonReleaseHandler);
    btn4.addEventListener(MouseEvent.ROLL_OVERonRollOverHandler);
    btn4.addEventListener(MouseEvent.ROLL_OUTonRollOutHandler);
    btn4.addEventListener(MouseEvent.CLICKonClickHandler);
    btn4.addEventListener(MouseEvent.MOUSE_UPonReleaseHandler);
    btn5.addEventListener(MouseEvent.ROLL_OVERonRollOverHandler);
    btn5.addEventListener(MouseEvent.ROLL_OUTonRollOutHandler);
    btn5.addEventListener(MouseEvent.CLICKonClickHandler);
    btn5.addEventListener(MouseEvent.MOUSE_UPonReleaseHandler);
    btn6.addEventListener(MouseEvent.ROLL_OVERonRollOverHandler);
    btn6.addEventListener(MouseEvent.ROLL_OUTonRollOutHandler);
    btn6.addEventListener(MouseEvent.CLICKonClickHandler);
    btn6.addEventListener(MouseEvent.MOUSE_UPonReleaseHandler);
    btn7.addEventListener(MouseEvent.ROLL_OVERonRollOverHandler);
    btn7.addEventListener(MouseEvent.ROLL_OUTonRollOutHandler);
    btn7.addEventListener(MouseEvent.CLICKonClickHandler);
    btn7.addEventListener(MouseEvent.MOUSE_UPonReleaseHandler);
    btn8.addEventListener(MouseEvent.ROLL_OVERonRollOverHandler);
    btn8.addEventListener(MouseEvent.ROLL_OUTonRollOutHandler);
    btn8.addEventListener(MouseEvent.CLICKonClickHandler);
    btn8.addEventListener(MouseEvent.MOUSE_UPonReleaseHandler);

    // if you want a hand cursor...
    btn1.useHandCursor true;
    btn2.useHandCursor true;
    btn3.useHandCursor true;
    btn4.useHandCursor true;
    btn5.useHandCursor true;
    btn6.useHandCursor true;
    btn7.useHandCursor true;
    btn8.useHandCursor true;
    //set up button modes...
    btn1.buttonmode true;
    btn2.buttonmode true;
    btn3.buttonmode true;
    btn4.buttonmode true;
    btn5.buttonmode true;
    btn6.buttonmode true;
    btn7.buttonmode true;
    btn8.buttonmode true
    thanks in advance...
    Jeff

  2. #2
    Teacosy is coming ranoka's Avatar
    Join Date
    Jun 2003
    Location
    UK
    Posts
    123
    Hey, here's an example of what I'd do with my current knowledge (it's more efficient than what you're doing).

    Basically put all your buttons inside a MovieClip and then add the listeners to that clip and let the event bubble through the buttons, and use event.target to control the buttons:

    Code:
    myMenu.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler, false, 0, true);
    myMenu.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler, false, 0, true);
    myMenu.addEventListener(MouseEvent.CLICK, onClickHandler, false, 0, true);
    
    myMenu.useHandCursor = true;
    myMenu.buttonMode = true;
    
    myMenu.menuGB.useHandCursor = false;
    myMenu.menuGB.buttonMode = false;
    
    function onClickHandler(event:MouseEvent):void
    {
    	trace("clicked: " + event.target.name);
    	
    	switch (event.target.name)
    	{
    		case "button1" : trace ("doing button1 stuff");
    		break;
    		case "button2" : trace ("doing button2 stuff");
    		break;
    		case "button3" : trace ("doing button3 stuff");
    		break;
    		//etc
    	}
    	
    }
    
    function onRollOutHandler(event:MouseEvent):void
    {
    	trace("out: " + event.target.name);
    	
    	//use event for stuff that is the same on all MCs
    	//event.target.gotoAndPlay("rollOut");
    	
    }
    
    function onRollOverHandler(event:MouseEvent):void
    {
    	trace("over: " + event.target.name);
    	
    	//use event for stuff that is the same on all MCs
    	//event.target.gotoAndPlay("rollOver");
    }
    I've attached an example with this code.
    Hope that helps.
    Worth reading for in depth:
    http://www.adobe.com/devnet/actionsc...dling_as3.html
    Attached Files Attached Files
    Last edited by ranoka; 11-04-2008 at 04:48 AM.

  3. #3
    Senior Member
    Join Date
    Apr 2006
    Posts
    129
    try this:
    PHP Code:
    var btns:Array = [btn1btn2btn3btn4btn5btn6btn7btn8];

    for (var 
    i:uint 0btns.lengthi++) {
        
    btn[i].addEventListener(MouseEvent.ROLL_OVERonRollOverHandler);
        
    btn[i].addEventListener(MouseEvent.ROLL_OUTonRollOutHandler);
        
    btn[i].addEventListener(MouseEvent.CLICKonClickHandler);
        
    btn[i].addEventListener(MouseEvent.MOUSE_UPonReleaseHandler);
        
    btn[i].useHandCursor true;
        
    btn[i].buttonMode true;


  4. #4
    Member
    Join Date
    May 2008
    Location
    Ohio, USA
    Posts
    63
    ranoka's idea is the way to go, but the code is a little off. First, create an empty Movie Clip called myMenu_mc. Then take all of your buttons off the stage and put them in myMenu_mc. Put myMenu_mc on the stage and give it the instance name myMenu. Then put this code in:

    PHP Code:
    //The false, 0, true arguments aren't required here but if you often forget to
    //remove Event Listeners when you're done with them then you can add them in
    myMenu.addEventListener(MouseEvent.MOUSE_OVERonMouseOver);
    myMenu.addEventListener(MouseEvent.MOUSE_OUTonMouseOut);
    myMenu.addEventListener(MouseEvent.CLICKonClick);

    //useHandCursor is true by default so you only need to set buttonMode to true
    myMenu.buttonMode true;

    //In all of these functions, evt.target points to myMenu and its children
    //Since the buttons are nested in myMenu, they get included
    function onMouseOver(evt:MouseEvent):void {
        
    //This will make any button you mouse over semi-transparent
        
    evt.target.alpha 0.5;
    }

    function 
    onMouseOut(evt:MouseEvent):void {
        
    //This will make the button solid again when you mouse out
        
    evt.target.alpha 1;
    }

    function 
    onClick(evt:MouseEvent):void {
        switch (
    evt.target.name) {
            case 
    "button1" :
                
    //Execute button1 code
                
    break;
            case 
    "button2" :
                
    //Execute button2 code
                
    break;
            default :
                
    //This will execute when you click a button whose name isn't listed
                //in this switch statement
                
    trace("Check your code for errors");
                break;
        }

    Most of the code is pretty similar to ranoka's - the major difference here is I'm using MOUSE_OVER instead of ROLL_OVER. ROLL_OVER works, but counts the entire container movie clip, so when you roll over individual buttons it doesn't do anything. MOUSE_OVER counts each individual nested button. Each has its own uses, depending on whether you want to deal only with the parent movie clip or with the parent clip and all of its children.

    Here's an example I was playing around with yesterday that uses a set of movie clips instead of buttons:
    Attached Files Attached Files
    Last edited by DoomOfTheLiving; 11-04-2008 at 07:18 AM.

  5. #5
    ...dishing dimes on an .fla! pointguard's Avatar
    Join Date
    Dec 2001
    Location
    Cambridgeshire, England
    Posts
    1,017
    Hi, many thanks for the tips...

    I have a movie clip which has different symbols / icons on each key frame, called 'mc_symbols'. This mc is nested inside the btn movieclip which is duplicated to make btn1, btn2, btn3, etc...

    I have a gotoAndStop code which makes each button's mc_symbols clip, go to a different frame with a different symbol on.

    Problem is, with the nested symbols clips, they keep acting funny. When you click on them instead of another part of the button, for some reason, the symbols clip goes to frame 1, displaying the wrong symbol...

    Is this something to do with the EventListeners for my buttons?

    thanks
    Jeff

  6. #6
    Member
    Join Date
    May 2008
    Location
    Ohio, USA
    Posts
    63
    I would get rid of the buttons altogether - just place multiple instances of mc_symbols inside the myMenu movie clip. When you set myMenu.buttonMode to true, it treats all the nested movie clips as buttons as well. This way you can just click the movie clips themselves instead of nesting them inside buttons.

  7. #7
    ...dishing dimes on an .fla! pointguard's Avatar
    Join Date
    Dec 2001
    Location
    Cambridgeshire, England
    Posts
    1,017
    I don't want to be able to click these nested clips, I just want them to be a static graphic / icon. However, I understand that by setting the btn1, btn2, etc buttonMode to true, it sets those nested clips to buttons as well. Is there a way to keep the nested movie clips as movie clips. Is this what casting is?
    I've tried using that casting method but can't seem to get it to work...

  8. #8
    Teacosy is coming ranoka's Avatar
    Join Date
    Jun 2003
    Location
    UK
    Posts
    123
    Thanks DoomOfTheLiving,
    I quickly rushed the example before leaving for work. I only used ROLL_OVER because it was in the pointguard's original code. Would have changed it over if had more time. Thanks for expanding and explaining the code better too.

  9. #9
    Member
    Join Date
    May 2008
    Location
    Ohio, USA
    Posts
    63
    Quote Originally Posted by ranoka
    Thanks DoomOfTheLiving,
    I quickly rushed the example before leaving for work. I only used ROLL_OVER because it was in the pointguard's original code. Would have changed it over if had more time. Thanks for expanding and explaining the code better too.
    No problem, I was just studying this type of thing a few days ago so it's one of the few things in AS3 I have a pretty good grasp on so far.

    Quote Originally Posted by pointguard
    I don't want to be able to click these nested clips, I just want them to be a static graphic / icon. However, I understand that by setting the btn1, btn2, etc buttonMode to true, it sets those nested clips to buttons as well. Is there a way to keep the nested movie clips as movie clips. Is this what casting is?
    I've tried using that casting method but can't seem to get it to work...
    So you have a button with a nested movie clip, and in each frame of the movie clip you store a different graphic. When you click anywhere on the button, you want the nested movie clip to move to the next frame, right? If that's the case, you can just get rid of the buttons altogether, create a movie clip that acts as a button, and add your graphics to that.

    Rather than explain the whole process I'll just attach an example. I made two different movie clips, one that just cycles through 3 different symbols when clicked, and one that emulates a button with an over, up, and down state that cycles through the same 3 symbols when clicked.

    Also, to answer your question, casting is when you convert an object from one type to another. Here's an example:

    PHP Code:
    var firstNum:String "17";
    var 
    secondNum:String "29";
    trace(firstNum secondNum); // 1729
    trace(Number(firstNum) + Number(secondNum)); // 46 
    Since firstNum and secondNum are defined as Strings, trace(firstNum + secondNum) gives you 1729 ("17" + "29" = "1729"). Once you convert them from Strings to Numbers, they can be added together, and you get 46.
    Attached Files Attached Files

  10. #10
    ...dishing dimes on an .fla! pointguard's Avatar
    Join Date
    Dec 2001
    Location
    Cambridgeshire, England
    Posts
    1,017
    It's not exactly wat I have...

    My movie clip goes to a certain frame, showing a certain graphic at the start, before anything is clicked...(see attached)

    I ca't really see a way around this, other than having new movie clips in my library, for each button, and then just having a static graphic of each symbol / icon on a different layer inside those...

    unless there is a way around it other than that?
    Last edited by pointguard; 11-05-2008 at 08:37 AM.

  11. #11
    Teacosy is coming ranoka's Avatar
    Join Date
    Jun 2003
    Location
    UK
    Posts
    123
    Hey Pointguard,

    I've played around with your example and managed to get it working!

    Note that the fix is using event.currentTarget instead of event.target
    Also, I changed CLICK to MOUSE_DOWN 'cos you can't use MOUSE_UP and CLICK together (they overlap).
    I commented out the stopPropagations because they aren't needed, but might help in other situations.

    If you trace the event.target for the click event you'll see that the icons are picking it up and that's why they're changing.

    Here's the code:
    Code:
    // Set up buttons for menu...
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    
    //set up buttons array...
    var _buttons:Array = [btn1, btn2];
    for each (var $button in _buttons) {
    	$button.buttonMode = true;
    	$button.addEventListener(MouseEvent.ROLL_OVER, onRollOverHandler, false, 0, true);
    	$button.addEventListener(MouseEvent.ROLL_OUT, onRollOutHandler, false, 0, true);
    	$button.addEventListener(MouseEvent.MOUSE_DOWN, onClickHandler, false, 0, true);
    	$button.addEventListener(MouseEvent.MOUSE_UP, onReleaseHandler, false, 0, true);
    }
    
    //set up symbols...
    btn1.mc_symbols.gotoAndStop(1);
    btn2.mc_symbols.gotoAndStop(2);
    
    //set up functions for mouse events...
    function onRollOverHandler(myEvent:MouseEvent) {
    	//myEvent.stopPropagation();
    	myEvent.currentTarget.gotoAndPlay(11);
    }
    
    function onRollOutHandler(myEvent:MouseEvent) {
    	//myEvent.stopPropagation();
    	myEvent.currentTarget.gotoAndPlay(1);
    }
    
    function onClickHandler(myEvent:MouseEvent) {
    	//myEvent.stopPropagation();
    	myEvent.currentTarget.gotoAndPlay(21);
    }
    
    function onReleaseHandler(myEvent:MouseEvent) {
    	//myEvent.stopPropagation();	
    	myEvent.currentTarget.gotoAndPlay(1);
    }
    
    stop();

  12. #12
    ...dishing dimes on an .fla! pointguard's Avatar
    Join Date
    Dec 2001
    Location
    Cambridgeshire, England
    Posts
    1,017
    ahhhhhhhhhh....thanks very much

  13. #13
    Teacosy is coming ranoka's Avatar
    Join Date
    Jun 2003
    Location
    UK
    Posts
    123
    No problem, I learned stuff myself by solving your problem. Will help me when I start doing my portfolio site.

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