9 Replies Latest reply on May 8, 2007 3:16 AM by ggshow

    If/Else - Is there a cleaner code?

    MShetler
      Hello all,

      I am currently working on a flash file and I'm an intermediate user. My actionscript abilities are probably close to beginner, but I'm a quick learner (most of what I have learned with actionscript has been self taught).

      In my flash file I have an animation of a rotating globe. Each frame of the rotation is a separate raster image - I don't know how you would represent a rotating globe otherwise, not really something that can be tweened (suggestions are welcome).

      I have different locations off to the side of the globe. When the user mouses over a location, the globe spins to that location. When they roll out, the globe spins back to a static starting point.

      I wanted the animation of the globe to be as smooth as possible, so, I have a }else{ statement for every possible frame. IE - if the globe animation is rotating back from a position and the user mouses over a new location before the animation stops, it doesn't jump back to the starting static position, it seemlessly flows from the position it is currently at to the new position the user has moused over.

      Make sense?

      It works flawlessly as is. I've spent quite a bit of time testing it. However, the code is bulky in appearance and according to the size report bulky in bytes as well.

      Is there a differnent way to set this up to cut down on size - mainly byte size if possible?

      I will include a bit of code so you can see what I'm talking about:

      on (rollOver) {
      if (_root.EarthClip._currentframe == 1) {
      _root.EarthClip.gotoAndPlay(140);
      } else { if (_root.EarthClip._currentframe == 160) {
      _root.EarthClip.gotoAndPlay(156);
      } else { if (_root.EarthClip._currentframe == 161) {
      _root.EarthClip.gotoAndPlay(155);
      } else { if (_root.EarthClip._currentframe == 161) {
      _root.EarthClip.gotoAndPlay(154);
      } else { if (_root.EarthClip._currentframe == 163) {
      _root.EarthClip.gotoAndPlay(153);

      and so on, and so on . . .


      Any help would be greatly appreciated.

      Thank you in advance.

      -MShetler
        • 1. Re: If/Else - Is there a cleaner code?
          dr_ross Level 1
          That just looks far too epic for an if/else of switch statement by the sounds of it

          The best way would be to do it mathmatically if you could based on the position of the movieclip that is being moused over. Or have some functionality whereby when the user mouses over a country, it calls a function passing it the target frame to stop on. Then apply an onEnterFrame handler to the globe to keep spinning until it reaches the target frame.


          • 2. Re: If/Else - Is there a cleaner code?
            MShetler Level 1
            The globe actually stays static until a continent is moused over (and returns to static when the mouse rolls out of continent). And from a designer's standpoint I think having the globe constently rotating would not fit.

            As for being epic - it is to some degree, BUT maybe not as much as the code above would have you believe. The longest animation from any one location to another is only 45 frames long. They average around 20 frames.

            I'm not sure I understand what you mean by "do it matchmatically if you could based on the position of the movieclip being moused over".

            Do you mean call out the coordinates of the positions being moused over to determine where to rotate the globe? If so, that seems similar to what I'm already doing.

            -MShetler
            • 3. Re: If/Else - Is there a cleaner code?
              Level 7
              >>on (rollOver) {
              if (_root.EarthClip._currentframe == 1) {
              _root.EarthClip.gotoAndPlay(140);
              } else { if (_root.EarthClip._currentframe == 160) {
              _root.EarthClip.gotoAndPlay(156);


              How about this?

              on (rollOver) {
              if (_root.EarthClip._currentframe == 1) {
              _root.EarthClip.gotoAndPlay(140);
              } else {
              _root.EarthClip.gotoAndPlay(156 - (_root.earthClip._currentframe -
              160));
              }
              }


              --
              Dave -
              Head Developer
              http://www.blurredistinction.com
              Adobe Community Expert
              http://www.adobe.com/communities/experts/


              • 4. Re: If/Else - Is there a cleaner code?
                MShetler Level 1
                Dave, thanks for the reply.

                I'm not entirely sure I follow though. What exactly does the else statement do? Wouldn't I have to input each of the corresponding frames still, like how you related 156 to 160? If so, is it just a matter of the bytes being less and the code running smoother (not to suggest that the code doesn't already run smooth - as I stated before it works perfectly, I'm just a little nervous about trying to get the .fla file, which is a little over 2 MB, deployed on a website).

                Thanks in advance for your help!

                -MShetler



                • 5. Re: If/Else - Is there a cleaner code?
                  MShetler Level 1
                  Additionally, I have several buttons that use the exact same actionscript code. Is there a way to consolidate this? Right now I have it copied and pasted on to each button. Can they all just call on the same actionscript stored somewhere else?

                  Thanks again!

                  -MShetler
                  • 6. Re: If/Else - Is there a cleaner code?
                    MShetler Level 1
                    Anymore ideas?
                    • 7. Re: If/Else - Is there a cleaner code?
                      Level 7
                      MShetler wrote:
                      > Anymore ideas?
                      You need an algorithm.

                      Implement this in an actionscript class:
                      For each frame when you have a destination:
                      Move one step towards it if you are not already there.

                      Basically curentframe+=movement needed.
                      • 8. Re: If/Else - Is there a cleaner code?
                        Optikalefx Level 1
                        orrrr use the wonderful switch statement. its faster. Thats because with if else flash has to go thru the conditions till it finds a true one, then its done, with switch it jumps right to the correct condition.

                        switch(_root.EarthClip._currentframe) {
                        case 1:
                        _root.EarthClip.gotoAndPlay(140);
                        break;
                        case 160:
                        _root.EarthClip.gotoAndPlay(156);
                        break;
                        case 161
                        _root.EarthClip.gotoAndPlay(155)
                        break;

                        ect..

                        default:
                        do something if none of the cases happens
                        break;
                        }
                        • 9. If/Else - Is there a cleaner code?
                          ggshow Level 2
                          try this: