6 Replies Latest reply on Aug 19, 2009 10:12 PM by killer1390

    Is there a better system for this?

    killer1390

      Hi, I have a cheat code system for my game and currently this is the code:

       

      on (press) {
           var thingy:Boolean = false;
      }
      on (release) {
           if (_root.mf.codes == twoly) {
                _root.twolock = false;
                this.thingy = true;
           } else {
                if (_root.mf.codes == twreee) {
                     _root.twolock = false;
                     _root.threelock = false;
                     this.thingy = true;
                } else {
                     if (_root.mf.codes == fourt) {
                          _root.twolock = false;
                          _root.threelock = false;
                          _root.fourlock = false;
                          this.thingy = true;
                     } else {
                          if (_root.mf.codes == five5) {
                               _root.twolock = false;
                               _root.threelock = false;
                               _root.fourlock = false;
                               _root.fivelock = false;
                               this.thingy = true;
                          } else {
                               if (_root.mf.codes == toosixy) {
                                    _root.twolock = false;
                                    _root.threelock = false;
                                    _root.fourlock = false;
                                    _root.fivelock = false;
                                    _root.sixlock = false;
                                    this.thingy = true;
                               } else {
                                    if (_root.mf.codes == sevenyearsyoung) {
                                         _root.twolock = false;
                                         _root.threelock = false;
                                         _root.fourlock = false;
                                         _root.fivelock = false;
                                         _root.sixlock = false;
                                         _root.sevenlock = false;
                                         this.thingy = true;
                                    } else {
                                         if (_root.mf.codes == eight1) {
                                              _root.twolock = false;
                                              _root.threelock = false;
                                              _root.fourlock = false;
                                              _root.fivelock = false;
                                              _root.sixlock = false;
                                              _root.sevenlock = false;
                                              _root.eightlock = false;
                                              this.thingy = true;
                                         } else {
                                              if (_root.mf.codes == ninet) {
                                                   _root.twolock = false;
                                                   _root.threelock = false;
                                                   _root.fourlock = false;
                                                   _root.fivelock = false;
                                                   _root.sixlock = false;
                                                   _root.sevenlock = false;
                                                   _root.eightlock = false;
                                                   _root.ninelock = false;
                                                   this.thingy = true;
                                              } else {
                                                   if (_root.mf.codes == tendyone) {
                                                        _root.twolock = false;
                                                        _root.threelock = false;
                                                        _root.fourlock = false;
                                                        _root.fivelock = false;
                                                        _root.sixlock = false;
                                                        _root.sevenlock = false;
                                                        _root.eightlock = false;
                                                        _root.ninelock = false;
                                                        _root.tenlock = false;
                                                        this.thingy = true;
                                                   } else {
                                                        if (_root.mf.codes == unlockable) {
                                                             _root.twolock = false;
                                                             _root.threelock = false;
                                                             _root.fourlock = false;
                                                             _root.fivelock = false;
                                                             _root.sixlock = false;
                                                             _root.sevenlock = false;
                                                             _root.eightlock = false;
                                                             _root.ninelock = false;
                                                             _root.tenlock = false;
                                                             _root.Locked = false;
                                                             this.thingy = true;
                                                        } else {
                                                             this.thingy = false;
                                                        }
                                                   }
                                              }
                                         }
                                    }
                               }
                          }
                     }
                }
           }
           if (this.thingy == true) {
                _root.txtx = "It worked";
           }
           if (this.thingy == false) {
                _root.txtx = "Wrong code";
           }
      }
      

      I am wondering if there is a way to make this more condense or not.

        • 1. Re: Is there a better system for this?
          Rothrock Level 5

          I would probably use a switch. Not a lot shorter, but a bit easier to read and to manage.

           

          switch(_root.mf.codes){

           

          case twoly:

          // your code here

          break;

           

          case twreee:

          // your code here

          break;

           

          // and so on....

           

          default:

          // what to do if there is no match

          this.thingy=false;

           

          }

           

          As for the part about setting several things to false. There is probably a better way to do that. I would probably do that with an array or a by using a binary system. But without know exactly what that is all about it is hard to give advice.

           

          However there is something that I would recommend you not do. And that is use on(event) type code placed on clips. Unless you are publishing to Flash 5 I would recommend using frame code and more modern event handlers.

           

          See this blog: http://www.quip.net/blog/2006/flash/museum-pieces-on-and-onclipevent

          1 person found this helpful
          • 2. Re: Is there a better system for this?
            NSurveyor Level 2

            II think this should do the trick.

            var codes = [twoly, twreee, fourt, five5, toosixy, sevenyearsyoung, eight1, nine1, tendyone, unlockable];
            var lock = ['twolock', 'threelock', 'fourlock', 'fivelock', 'sixlock', 'sevenlock', 'eightlock', 'ninelock', 'tenlock', 'Locked'];
            var thingy = false;
            for(var i=0;i<=9;i++){
             if(_root.mf.codes == codes[i]){
              for(var j=0;j<=i;j++){
               _root[lock[j]] = false;
              }
              this.thingy = true;
              break;
             }
            }
            _root.txtx = this.thingy ? "It worked" : "Wrong code";
            

            In the future I would stay away from such variable names. For this kind of thing, I think it would be easier to manage an array "lock", using lock[0] through lock[9] instead of twolock through Locked, and similarly an array for the codes as I have made.

             

            Instead of just asking for someone to compress your code, it would be more helpful to describe what the code is doing. What I'm guessing the purpose of this code is, is to check an inputted code, and then unlock all prior levels, followed by a confirmation that it was indeed a valid code.

            • 3. Re: Is there a better system for this?
              killer1390 Level 1

              Thanks for the tip on asking for help. And Thanks a lot for the help.

              • 5. Re: Is there a better system for this?
                NSurveyor Level 2

                You're welcome. I made some last minute edits which I did not carry out completely (changing thingy into a local variable). And just for the record, the following should work well:

                 

                 

                var codes = [twoly, twreee, fourt, five5, toosixy, sevenyearsyoung, eight1, nine1, tendyone, unlockable];
                var lock = ['twolock', 'threelock', 'fourlock', 'fivelock', 'sixlock', 'sevenlock', 'eightlock', 'ninelock', 'tenlock', 'Locked'];
                var thingy = false;
                for(var i=0;i<=9;i++){
                 if(_root.mf.codes == codes[i]){
                  for(var j=0;j<=i;j++){
                   _root[lock[j]] = false;
                  }
                  thingy = true;
                  break;
                 }
                }
                _root.txtx = thingy ? "It worked" : "Wrong code";
                
                1 person found this helpful
                • 6. Re: Is there a better system for this?
                  killer1390 Level 1

                  Wow, you are really good at action scripting. Thanks a lot.