Skip navigation
jamestparker
Currently Being Moderated

is this a bad way to write this switch?

Jun 11, 2012 1:27 PM

stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);

function myKeyDown(e:KeyboardEvent):void

{

 

    switch (e.keyCode)

    {

        case Keyboard.LEFT :

            prevFrame();

            if (currentFrame < totalFrames)

            {

                nav_mc.next_btn.mouseEnabled = true;

 

                nav_mc.next_btn.alpha = 1;

 

            }

 

            if (currentFrame == 1)

            {

                nav_mc.back_btn.mouseEnabled = false;

 

                nav_mc.back_btn.alpha = .6;

 

                nav_mc.text_btn.mouseEnabled = false;

 

                nav_mc.text_btn.alpha = .6;

 

            }

 

            break;

 

        case Keyboard.RIGHT :

            nextFrame();

            if (currentFrame > 1)

            {

 

                nav_mc.back_btn.mouseEnabled = true;

 

                nav_mc.back_btn.alpha = 1;

 

                nav_mc.text_btn.mouseEnabled = true;

 

                nav_mc.text_btn.alpha = 1;

 

            }

            if (currentFrame == totalFrames)

            {

 

                nav_mc.next_btn.mouseEnabled = false;

 

                nav_mc.next_btn.alpha = .6;

 

            }

            break;

    }

}

 
Replies
  • Currently Being Moderated
    Jun 11, 2012 2:12 PM   in reply to jamestparker
    I see well the switch, maybe you could have problems with the nextFrame () and prevFrame (), but if you do not preocipan, are well

     

     

     

    stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);

     

    function myKeyDown(e:KeyboardEvent):void {

        switch (e.keyCode) {

     

            case Keyboard.LEFT :

                if (currentFrame > 1) {

                    prevFrame();

                }

                if (currentFrame < totalFrames) {

                    nav_mc.next_btn.mouseEnabled=true;

                    nav_mc.next_btn.alpha=1;

                }

     

                if (currentFrame == 1) {

                    nav_mc.back_btn.mouseEnabled=false;

                    nav_mc.back_btn.alpha=.6;

                    nav_mc.text_btn.mouseEnabled=false;

                    nav_mc.text_btn.alpha=.6;

                }

                break;

     

            case Keyboard.RIGHT :

                if (currentFrame < totalFrames) {

                    nextFrame();

                }

                if (currentFrame > 1) {

                    nav_mc.back_btn.mouseEnabled=true;

                    nav_mc.back_btn.alpha=1;

                    nav_mc.text_btn.mouseEnabled=true;

                    nav_mc.text_btn.alpha=1;

                }

     

                if (currentFrame == totalFrames) {

                    nav_mc.next_btn.mouseEnabled=false;

                    nav_mc.next_btn.alpha=.6;

                }

     

                break;

        }

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 11, 2012 10:49 PM   in reply to jamestparker

    I would recomment you to brake the code into smaller methods/functions

     

    stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
    function myKeyDown(e:KeyboardEvent):void{
     
        switch (e.keyCode){
            case Keyboard.LEFT :
                this.turnLeft();
                break;
            case Keyboard.RIGHT :
              this.turnRight();
                break;
        }
    }
     
    function turnLeft(){
                prevFrame();
                if (currentFrame < totalFrames){
                    nav_mc.next_btn.mouseEnabled = true;
                    nav_mc.next_btn.alpha = 1;
                }
                if (currentFrame == 1){
                    nav_mc.back_btn.mouseEnabled = false;
                    nav_mc.back_btn.alpha = .6;
                    nav_mc.text_btn.mouseEnabled = false;
                    nav_mc.text_btn.alpha = .6;
               }
    }
     
    function turnRight(){
                nextFrame();
                if (currentFrame > 1){
                    nav_mc.back_btn.mouseEnabled = true;
                    nav_mc.back_btn.alpha = 1;
                    nav_mc.text_btn.mouseEnabled = true;
                    nav_mc.text_btn.alpha = 1;
                }
                if (currentFrame == totalFrames){
                    nav_mc.next_btn.mouseEnabled = false;
                    nav_mc.next_btn.alpha = .6;
                }
    }
     
    

    Also, better to add states for buttons, like enabled() and disabled() methods

    Back button code example:

     

    function enable() {
         this.mouseEnabled=true;
         this.alpha=1;
    }
    function disable() {
         this.mouseEnabled=false;
         this.alpha=0;
    }
    

     

    Navigation panel code:

     

    function enableBackButton() {
         this.back_btn.enable();}
    function disableBackBtn() {
         this.back_btn.disable();
    }
     
     
     
     
    

     

    Later this will allow you to easier change UI elements behaviour, for example, when you will need to add animation for "enable" state. To make development more comfortable try to use classes, with classes you will be able to write code once and use it for all buttons or other UI elements in the movie.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points