11 Replies Latest reply on Jun 17, 2009 10:26 AM by Rothrock

    Switching a boolean

    Yakamazi Level 1

      var cave:Boolean;

      cave = true;

       

      if (cave == true) { trace ("true");}

      if (cave == false) { trace ("false");}

       

      cave_hit_area.onRelease =  function () {

           cave = false;

      }

       

      Hi i'm having trouble getting this to work.

      All i need to do is make flash realise that the boolean has changed from true to false so that i can make other magic happen.

       

      I was hoping that simply changing it would make it work... but now i'm thinking that i'm probably missing something, or i'm going the wrong way about it... but i dunno.. hense why i need assistance on this one

       

      Any help on this would be awesome.

       

      Cheers

        • 1. Re: Switching a boolean
          Ned Murphy Adobe Community Professional & MVP

          First, the code can be simplified a bit...

           

          var cave:Boolean = true;

           

          if (cave) {

            trace ("true");

          } else {

            trace ("false");

          }

           

          // all of that conditional above could be reduced to... trace(cave);

           

          cave_hit_area.onRelease =  function () {

               cave = false;

          }

           

          What and where are you trying to get the change to affect something?  The correct place would be where the change occurs, which in this case is inside the function.

          • 2. Re: Switching a boolean
            Rothrock Level 5

            As Ned points out, the easiest thing to do would be to do whatever action you want when you change the value.

             

            But if you need to perform some action when the value changes you could use the watch() function.

             

            It allows you to "watch" a variable/property and when its value is assigned a function will be called. That function can then determine if the value has changed (or remained the same) and perform various bits of code based on that result.

            • 3. Re: Switching a boolean
              Yakamazi Level 1

              Cheers for the quick reply

               

              That load of code was like the filtered down version to focus on the confusing bits of code. Cheers for the compressing tips though

               

              I need the variable to remember it's current boolean.

              The reason for this is that it will control how a movie clip will be working.

              When it shows true.. the movie clip that it's linked to will take you somewhere when it's clicked.

              When it shows false.. it will take you somewhere else.

               

              There is a seperate item (cave_hit_area) which will switch the boolean (as well as doing other stuff that helps you see what's going on).

               

              I think that because i declare that 'cave' as 'true' in the second line... it's showing it up in the trace output area.

              The issue of course is that the boolean is not changing when i tell it to be changed.

               

              if for example i write:

               

              cave_hit_area.onRelease =  function () {
                   cave = false;

                   trace (cave);
              }

               

              It tells me in the output that cave is false....

              but in reality it has not changed... because when cave = false it needs to do other stuff... like:

               

              if (cave == false) {

                   cave_mc._visibility = false;

              }

               

              sort of thing...

               

              because its not doing this i'm thinking that i've messed up or i'm naming something incorrectly or just doing it wrong :S

               

              I've never used the watch() function before... (and i'm blatently no pro when it comes to this) but it's sounds like it's more than likely what i may be missing...

               

              I get the impression that i need to refresh the state of the code after each action has been made... or something like that...

               

              Does that help any more?

               

              • 4. Re: Switching a boolean
                Yakamazi Level 1

                if you like i can throw on the actual code that i'm working with?

                I just thought getting rid of it all would make it easier to understand...

                • 5. Re: Switching a boolean
                  Yakamazi Level 1

                  var cave:Boolean;

                  _root.cave = true;

                   

                  if (_root.cave == true) {

                  _root.cave_mc.onRelease =  function () {

                  _root.cave_star_mc._visible = false;

                  _root.cave = false;

                  trace (cave);

                  }

                  }

                   

                  if (_root.cave == false) {

                  _root.cave_mc.onRelease =  function () {

                  _root.cave_star_mc._visible = true;

                  _root.cave = true;

                  trace (cave);

                  }

                  }

                  Ok here's the actual code.
                  In the beginning the cave has a star over it.
                  You click the cave and the star disappears and the cave should become 'false' as i have a huge list of caves (that all start off 'true') that when the user gets to the end of the set there will be a load left 'true' and a load left 'false'. At this stage i will be doing things with the list that i have left. This is only graphicly shown at this stage with a star... but i have something else planned for later.
                  This appears to work fine because the trace says "false" and the star has gone.. but it hasn't sorted itself because if you click on it again... it just says 'false' again and the star doesn't appear

                  • 6. Re: Switching a boolean
                    Rothrock Level 5

                    Not sure I understand your confusion. But I think you are making it more complex than it needs to be. I think you only need to define one event handler and then have that handler do different things based upon the state of the cave. Here are some things that might help.

                     

                    You say you have many caves. If those caves are each a movie clip and each one has a star inside the clip think of that star as a property of the clip. The movieclip class is dynamic so you can add your own properties to each movieclip so it can keep track of things.

                     

                    So if you had a bunch of caves and named them something like cave0, cave1, cave2, etc. maybe up to 10, you could do this:

                     

                    var numCaves:Number=10;

                     

                    for(var i=0;i<numCaves;i++){

                    var curCave:MovieClip=this["cave"+i];

                    curCave.starClip._visible=true;

                    curCave.isStar=true;

                    curCave.onRelease=caveClicked;

                    }

                     

                    function caveClicked(){

                    this.isStar = !this.isStar;

                    this.starClip._visible=this.isStar;

                    if(this.isStar){

                    //do something if isStar is true

                    } else {

                    //do something else if isStar is false

                    }

                    }

                     

                    It sounds like you are making some kind of adventure game or something where the user has to visit certain places and collect something or put together the pieces. So I'm using my imagination for what I would do if that was my project. If I'm off, please let me know and I"ll see what I can figure out.

                    • 7. Re: Switching a boolean
                      Yakamazi Level 1

                      I'm sure that would work in some way.. but i'm not sure how that stores any information  but i have no idea how to be honest mate... as i say i'm stuck with basics here because i'm not that fluent with this.

                       

                      My code might be long winded but at this moment it's what's making sense to me.

                       

                      I'm stripping the file down to a basic one object thing.. and i'll pop that on here so you can see.

                       

                      when you run this as a swf you get a picture with a spinning star and a faded yellow area (showing the hit area of cave_mc). It doesn't trace it at this stage but the object has been set to true (as shown in line 2 of the action script)

                      When you hover over the area the cursor changes which is right

                      When you click it the star disapears... which is right.... but the output does not say false... which tells me that a change has not been registered.

                      After this if you hover over the hit area the cursor still changes which is good... but when you click it, it does noting. It still says true in the output, and the star doesn't come back. This is where the confusion sets in :S

                       

                      I realise that there are other ways to make this effect happen.. but i must work in this way splitting the movieclips up as when the user is done clicking on all the objects that they wish to click on, i hope to have a list of objects set to true or false.

                       

                      I feel that the code that i have must be correct (as there's nothing to it)... but i may be missing a line that will make sure that the objects change their status once the hit has happened.

                       

                      The idea is not to create a new set of code which will look the same. All i need is the bit that may be missing to get 'this' current code to work in the way that i need it to.

                       

                      My explanation skills aren't great but i'm trying, and you're trying to descipher it so thanks for the help so far

                       

                      Cheers

                       

                      ok it wont let me add the file for some reason :S

                      • 8. Re: Switching a boolean
                        Yakamazi Level 1

                        yeah it's not letting me add the flv file.. either way the code says:

                         

                        /* this is the important bit that doesn't change when i ask it to*/

                        var cave:Boolean;

                        _root.cave = true;

                         

                         

                        if (_root.cave == true) {

                        _root.cave_mc.onRelease =  function () {

                        _root.cave_star_mc._visible = false;

                        _root.cave.value = false;

                        trace (cave);

                        }

                         

                        }

                         

                        if (_root.cave == false) {

                        _root.cave_mc.onRelease =  function () {

                        _root.cave_star_mc._visible = true;

                        _root.cave.value = true;

                        trace (cave);

                        }

                        }

                        and on the stage is a star (cave_star_mc)
                        and a hit area (cave_mc)

                        • 9. Re: Switching a boolean
                          Rothrock Level 5

                          Whether this is the way that you are doing it makes sense to you or not, that way is not working. So you need to change your thinking a bit. Your code is getting more and more complex (not in a good way) and getting harder and harder to decipher and remember what means what and what is where. And by using things like _root you are making it so that it will be much harder to make changes in the future.

                           

                          So forget what I said earlier about how to handle a bunch of these, but if you do have a bunch of these come back to it later. You will be much happier.

                           

                          The part I said about only having one event handler is what is important. Don't try and assign two event handlers. Just assign one that handles both cases. So let's just make this as simple as possible. Forget all the other things you might want to be doing. Let's just make the star show and hide when cave_mc is clicked.

                           

                          First if your cave or cave.value (not sure since you seem to keep adding properties) is just to keep track of whether the star or visible or not why do you need that? You can just ask:

                           

                          trace(cave_star_mc._visible);

                           

                          That will trace true if the star is visible and false if it isn't. Don't try and track something with another variable if you don't need to do so. So we aren't going to make any cave property.

                           

                          So the simplest version of the code would probably be:

                           

                          cave_mc.onRelease=function(){

                            cave_star_mc._visible = !cave_star_mc._visible;

                          }

                           

                          The ! means to take the opposite of what the current value is. So if the star is visible it will now be invisible and if it is invisible it will be visible.

                           

                          If you do need the variable for something else and it might have a value different than the star's visability then by all means do make a new varaible. But give it a better name.

                           

                          cave and cave.value aren't really very descriptive. And the code you are showing seems to mix them up. Or maybe it doesn't?

                           

                          If I ask someone "Cave?" and they reply "True." That doesn't make sense. But if I as somebody "isVisited?" or "isActive?" and they reply "True." That makes more sense.

                          • 10. Re: Switching a boolean
                            Yakamazi Level 1

                            Thank you very much for this Rothrock

                            The '_root' stuff and '!' will come in very handy and will simplify the code which is great.

                             

                            I was up all last night messing around and found that i had just put the functions in the wrong place. :S

                             

                            So my code now looks like this and it works how i need it to

                            /******************************************

                            var cave:Boolean;

                            cave = true;

                             

                            cave_mc.onRelease = function () {

                                 if (cave == true) {

                                      cave_star_mc._visible = false;

                                      cave= false;

                                      trace (cave);

                                      } else {

                                      cave_star_mc._visible = true;

                                      cave= true;

                                      trace (cave);

                                 }

                            }                   

                            *****************************************/

                             

                            I understand what you mean about going a step too far aswell. Instead of having the variable 'cave' there's no reason why i shouldn't be able to make everything happen off the back of the star... so 'if the star is on... initiate the following functions.' rather than 'if the star is on.. turn cave on... and if cave is on... initiate the following functions.'

                             

                            So i'm going to go through and clean this all up (ensuring that it all still works) before going ahead and completing the project.

                             

                            Thank you very much for your help

                            • 11. Re: Switching a boolean
                              Rothrock Level 5

                              Cool. Glad it is starting to come together for you. There are a lot of tricks and don't expect to learn them all over night as it were.

                               

                              I like your phrase, "..make everything happen off the back of the star..." and you are spot on there. So think about that trick for the future.

                               

                              Here is one more little trick about conditional if statements.

                               

                              So you have something like this:

                               

                              if(something == somethingElse)

                               

                              Well the part inside the parenthesis is evaluated by Flash and it will either come out true or false, there is not other result, just

                               

                              if(true)

                               

                              or

                               

                              if(false)

                               

                              So when you are using a Boolean variable like cave in your example, since it already holds the value of either true or false you can just shorten that condtional up to this:

                               

                              if(cave){

                               

                              }

                               

                              That means if cave is true, do the following. If you wanted to only do something if cave was false you could use the ! to turn it around.

                               

                              if(!cave){

                               

                              }

                               

                              That would be read as "if not cave do the following." (Not cave means if false.)

                               

                              It is a simple thing, but it makes your code look cooler to those in the know.