5 Replies Latest reply on Apr 16, 2009 2:54 PM by David Stiller

    Can a Boolean declaration be abbreviated?

    shintashi Level 1

      I know this doesn't work, but basically, I'm looking at something like this:

       

      robot_mc.skullplate_mc._visible = false;
      $skull = String(robot_mc.skullplate_mc._visible = false);

       

       

       

      because I know I can abbreviate stuff like

       

      yellow = Key.KeyisDown(Key.RIGHT);

      blue = Key.KeyisDown(Key.UP);

      green = yellow && blue;

       

      if(green){ball_mc._x += 5; ball_mc._y -= 5;}

       

      and abreviating booleans would be really useful.

        • 1. Re: Can a Boolean declaration be abbreviated?
          David Stiller Level 2

          shintashi,

           

               Your abbreviations are a common and potentially useful workflow, and you can certainly accomplish what you're after here -- if I understand your question correctly -- but it's important you realize that these aren't actually abbrevations; not in the common sense of the word.  What you're doing is declaring a variable to hold a value.  That value is either true or false, just like the longer expression that evaluates to the same thing.

           

               So, as you've already discovered, it can be convenient to refer to something shorter (quicker/easier to type), like yellow, than a full expression such as Key.KeyisDown(Key.RIGHT);.  That's a bit different from what I think you're asking about here ...

           

          robot_mc.skullplate_mc._visible = false;
          $skull = String(robot_mc.skullplate_mc._visible = false);


               When I see that, it looks to me like you're hoping to encode a whole expression as a string.  That's the part that confuses me (just not sure what you're aiming for) ... so I'm wondering if you could do something more along the lines of this:

           

          var skull = robot_mc.skullplate_mc;

           

          That way, you can simply refer to the "abbreviated" robot_mc.skullplate_mc reference in order to turn that inner movie clip's visibility on or off:

           

          skull._visible = true;

          skull._visible = false;

           

               Let me know if I'm way off base.

           

           

          David Stiller
          Adobe Community Expert
          Dev blog, http://www.quip.net/blog/
          "Luck is the residue of good design."

          • 2. Re: Can a Boolean declaration be abbreviated?
            shintashi Level 1

            basically I had a rotating head from side scroller view, which requires a lot of separate images to be displayed, and was going to switch them out so only one would be visible. It was a layering problem (in terms of something above in certain frames and below in others, like avatar layering) that could have been resolved by having only one view visible at a time, switching the others on/off accordingly. Unfortunately, the code of switching all others off looked like a small paragraph, and it had to be repeated for each direction, and then again an entirely different set, turning into a monster of repetition. I had hoped to cut it down to 4 words per line.

             

            I could also see a lot of potential use for abbreviating declarations elsewhere.

             

            Thank you for your reply. ^_^

            • 3. Re: Can a Boolean declaration be abbreviated?
              David Stiller Level 2

              shintashi,

               

                   If you want, you can show that paragraph of code.    For all we now, it might be something that can be written more efficiently.

               

               

              David Stiller
              Adobe Community Expert
              Dev blog, http://www.quip.net/blog/
              "Luck is the residue of good design."

              • 4. Re: Can a Boolean declaration be abbreviated?
                shintashi Level 1

                it wasn't anything fancy; just your basic

                 

                    if(Key.isDown(Key.UP)){robot_mc._y -= bit + speed; robot_mc.gotoAndStop("frame2"); robot_mc.skullplate_mc._visible = true; robot_mc.faceplate_mc._visible = !true; robot_mc.leftface_mc._visible = !true; robot_mc.rightface_mc._visible = !true;}
                        if(Key.isDown(Key.DOWN)){robot_mc._y += bit + speed; robot_mc.gotoAndStop("frame1"); robot_mc.skullplate_mc._visible = !true; robot_mc.faceplate_mc._visible = true; robot_mc.leftface_mc._visible = !true; robot_mc.rightface_mc._visible = !true;}
                        if(Key.isDown(Key.LEFT)){robot_mc._x -= bit + speed; robot_mc.gotoAndStop("frame4"); robot_mc.skullplate_mc._visible = !true; robot_mc.faceplate_mc._visible = !true; robot_mc.leftface_mc._visible = true; robot_mc.rightface_mc._visible = !true;}
                        if(Key.isDown(Key.RIGHT)){robot_mc._x += bit + speed; robot_mc.gotoAndStop("frame3"); robot_mc.skullplate_mc._visible = !true; robot_mc.faceplate_mc._visible = !true; robot_mc.leftface_mc._visible = !true; robot_mc.rightface_mc._visible = true;}

                 

                I got a headache when trying to add the faceplate lines to the automated section (which is much more messy than the above).

                • 5. Re: Can a Boolean declaration be abbreviated?
                  David Stiller Level 2

                       Gotcha.  Yeah ... sometimes coding is just tedious.  That said, you could break up your code in different ways, and sometimes that helps consolidate things a bit.  Here are a couple quick examples.  NOTE:  Neither of these is especially useful in this particular case, but they might help you brainstorm different ways of approaching what you've done.

                   

                       The first thing that occurs to me, for example, is the repetition involved with all the _visible settings.  In all cases, you're turning most of the movie clips off, except for one.  It might be worth writing a quick function that turns them all off ... then override the exception.  Again, in this case it doesn't really reduce the overall line count by much.

                   

                       I assume you're running this routine inside a Key.onKeyDown event handler.  So the first think you could do is call your custom hideParts() function, then use your if() statements to turn the relevant clip back on:

                   

                  function hideParts(mc:MovieClip):Void {
                     with (robot_mc) {
                       skullplate_mc._visible = false;
                       faceplate_mc._visible = false;
                       leftface_mc._visible = false;
                       rightface_mc._visible = false;
                     }
                  };

                  hideParts();
                  if (Key.isDown(Key.UP)) {
                    robot_mc._y -= bit + speed;
                    robot_mc.gotoAndStop("frame2");
                    robot_mc.skullplate_mc._visible = true;
                  }
                  if (Key.isDown(Key.DOWN)) {
                    robot_mc._y += bit + speed;
                    robot_mc.gotoAndStop("frame1");
                    robot_mc.faceplate_mc._visible = true;
                  }
                  if (Key.isDown(Key.LEFT)) {
                    robot_mc._x -= bit + speed;
                    robot_mc.gotoAndStop("frame4");
                    robot_mc.leftface_mc._visible = true;
                  }
                  if (Key.isDown(Key.RIGHT)) {
                    robot_mc._x += bit + speed;
                    robot_mc.gotoAndStop("frame3");
                    robot_mc.rightface_mc._visible = true;
                  }

                   

                       I also notice repetition in the line that sends robot_mc's playhead to a certain frame.  You could take advantage of your frame labeling (the fact that each uses a number) and wrap the rest of the repetition up in yet another function.  For grins, I'm using a switch() statement as an alternative to if()s:

                   

                  if (Key.isDown(Key.UP)) {
                    updateRobot(2);
                  }
                  if (Key.isDown(Key.DOWN)) {
                    updateRobot(1);
                  }
                  if (Key.isDown(Key.LEFT)) {
                    updateRobot(4);
                  }
                  if (Key.isDown(Key.RIGHT)) {
                    updateRobot(3);
                  }

                  function updateRobot(num:Number):Void {
                    hideParts();
                    switch(num) {
                      case 1:
                        robot_mc._y += bit + speed;
                        robot_mc.faceplate_mc._visible = true;
                        break;
                      case 2:
                        robot_mc._y -= bit + speed;
                        robot_mc.skullplate_mc._visible = true;
                        break;
                      case 3:
                        robot_mc._x += bit + speed;
                        robot_mc.rightface_mc._visible = true;
                        break;
                      case 4:
                        robot_mc._x -= bit + speed;
                        robot_mc.leftface_mc._visible = true;
                        break;
                    }
                    robot_mc.gotoAndStop("frame" + num);
                  };

                  function hideParts(mc:MovieClip):Void {
                     with (robot_mc) {
                       skullplate_mc._visible = false;
                       faceplate_mc._visible = false;
                       leftface_mc._visible = false;
                       rightface_mc._visible = false;
                     }
                  };

                   

                       As before, this new function, updateRobo(), calls on hideParts() right off the bat.  The last line inside updateRobot() contatenates the string "frame" with the relevant number to avoid repeating that gotoAndStop() line more often than necessary.

                   

                       If you had considerably more body parts than these four, one of these other approaches might help.  It's really just the same approach you were taking, except instead of abbreviating individual expressions, you're looking for whole blocks of repeated code that be accounted for in a different way.

                   

                       The cool thing is, ActionScript is flexible enough to handle any of the above (or, probably, half a dozen other approaces), so use the one that makes the most sense to you. 

                   

                   

                   

                  David Stiller
                  Adobe Community Expert
                  Dev blog, http://www.quip.net/blog/
                  "Luck is the residue of good design."

                  1 person found this helpful