3 Replies Latest reply on Jan 21, 2009 6:00 AM by Newsgroup_User

    if var change then... ?

    shintashi Level 1
      I'm trying to figure out how to set up a program so that if a particular variable is being edited (such as hp loss to a spaceship icon) an event (such as flashing lights) occurs. Since I've got a bunch of different ways of changing the variable, it would take hundreds if not thousands of lines of code to insert specifics within each hitTest. Instead, I was hoping to blanket the whole by saying something like

      if hp (goes down) then (flashy lights) else (no flashy lights);

      but I've never actually seen a command that pings when a variable changes indiscriminately. Does such code exist?
        • 1. Re: if var change then... ?
          clbeech Level 3
          yes shintashi - you can do this using the Object.watch() method.
          • 2. if var change then... ?
            Level 1
            Hi,

            ususally for those case ( set variable = do something ) i use the "set"statement. If you dont know about set and get statement, here is a link that explain it:

            set
            http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/statements.html#set

            get
            http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/statements.html#get

            set and get statement are pretty usefull for numerous reason.

            To give an example, just say we want to call a function called shootName when a variable userName is set with a new value.

            private var _userName:String;

            public function set userName( str:String ):void{
            _userName = str;
            shootName();
            }

            private function shootName():void{
            trace( _userName );
            }

            // --o here we set a value to userName; the function shootName will be called and the ouput panel will trace "bob"
            userName = "bob";
            • 3. Re: if var change then... ?
              Level 7
              shintashi,

              In butcho's reply, butcho mentions get and set, which are special
              modifiers inside a custom class that change how a method (basically, a
              function of that class) can be accessed. The assumption here is that you're
              writing a custom class, so that instead of your hp variable being a timeline
              variable, it would be a property of your class.

              One of the reasons get and set are useful is that they allow you to run
              potentially complex routines when the property they represent is changed.
              Setting hp to some other value would require a reference to your class
              (sounds like it's a spaceship, so let's assume you've written a custom
              SpaceShip class). Maybe something like this, where myShip is an instance of
              the SpaceShip class:

              myShip.hp = "down";

              I have no idea, of course, what you've been setting hp to. Maybe it's
              Boolean (true/false), maybe it's a string, like what I just showed. In
              principle, it doesn't matter, because the operative magic is what goes on
              inside the SpaceShip class. To allow hp to be a directly gettable/settable
              property, the relevant portion of your class might look like this, where _hp
              is a private variable inside the class (the internal value of the hp
              concept):

              public function get hp():String {
              return _hp;
              };
              public function set hp(val:String):Void {
              switch(val) {
              case "down":
              // do the down thing
              break;
              case "up":
              // do the up thing
              break;
              case "etc":
              // do some other thing
              break;
              default:
              // do this by default
              }
              dispatchEvent({type:"flashLights"});
              _hp = val;
              };

              All the switch() jibber jabber is optional -- again, I have no idea what
              your existing code looks like -- but the point is that your set function can
              do much more than simply set the private version of this variable: it's a
              function, after all, so it can do whatever you like, including dispatching
              an event. That's an example of the EventDispatcher class in AS2, which is
              briefly discussed here (and includes a link to an article on Adobe by
              Kenneth Toley):

              http://www.quip.net/blog/2006/flash/actionscript-20/how-to-raise-events-eventdispatcher

              So ... this is one of the ways to accomplish what you're after, and
              again, it assumes you're comfortable with writing custom classes -- which
              you may well be. If so, give the get/set approach a shot. You'll also have
              to create an event listener for your custom flashLights event, and that
              listener will trigger another function that causes your SpaceShip instance
              to flash its lights.

              If this game is written in timeline code, you may well want to consider
              redoing it as a series of classes. Class files provide considerably better
              options for organizing and reusing your code. If you're new to the concept,
              check out these articles by Joey Lott:

              http://web.archive.org/web/20051221101424/www.person13.com/articles/

              NOTE: I would have linked directly to his site, but someone seems to have
              hacked it at the moment I'm writing this (three cheers for the Wayback
              Machine!).

              If you're well underway with this game and don't have time to learn
              about custom classes, or simply prefer not to, check out the watch() method
              of the Object class. There's an example here (this is my blog, again),
              which, while different from your example, should at least get you started

              http://www.quip.net/blog/2008/flash/actionscript-20/how-to-play-timeline-backwards


              David Stiller
              Co-author, Foundation Flash CS4 for Designers
              http://tinyurl.com/5j55cv
              "Luck is the residue of good design."