2 Replies Latest reply on Feb 14, 2014 12:43 AM by prodigalmaster

    How can I create an 'if' event that works?

    EliteAlphaX

      Hi from a newbie, (using CS5) well I have been trying to create a particular event for my game during all last week with no sucess (and no online tutorials have seem to help). The event is supposed to be that when you collect a key in the room you are able to open the door in the room (and proceed to another scene).

       

      So I create a variable for the key (called 'key') which created in self on frame 1 of the scene. And a 'on release' event on the 'key' object which changed it's variable from 'false' to 'true' (i.e. the player has collected the key). I know all of this works because I created a dynamtic textbox to show the changing 'key' variable, but my problem is when you clicked on the door.

       

      here is my code on the door:

       

      on (release){

      if (key = true){

      gotoAndPlay ("Scene 8", 1);

      }

       

      if (key = false){

      gotoAndPlay ("Scene 1", 1);

      }

      }

       

      What happens is that you click on the door it will take you next stage (scene 8) regardless of  whether key = true or false,

       

      But when I changed my code to this (differences are in red):

       

      on (release){

      if ("key = true"){

      gotoAndPlay ("Scene 8", 1);

      }

      if ("key = false"){

      gotoAndPlay ("Scene 1", 1);

      }

      }

       

      What happens is that when you click on the door it will keep you in the same room (scene 1) regardless of whether key = true or false, and it will change 'key' to false.

       

      so if anyone can please help me solve my problem i would be very grateful.

        • 1. Re: How can I create an 'if' event that works?
          kglad Adobe Community Professional & MVP

          first, you should not attach code to objects.  assign your objects instance names and use those in your code.

           

          that would not only start a good practice for you, but would also solve your current problem which is almost certainly do to a "scope" issue.  ie, your door is probably a movieclip and that on(release) is checking for a variable name key within the scope of your door movieclip.

           

          second, you should not use the goto functions/you should not use scenes for navigation in as2.  use the goto methods and frame labels.

           

          for example, if you assigned an instance name to your door (eg, door_mc) and labelled those frame in the obvious way, your code would look like:

           

          door_mc.onRelease=function(){

          if(key_{

          _root.gotoAndPlay("scene8_frame1");

          } else {

          _root.gotoAndPlay("scene1_frame1");

          }

          • 2. Re: How can I create an 'if' event that works?
            prodigalmaster Level 2

            Kglad gives a lot of relevant information but I think the problem is you are using the wrong operator.

             

            if (key = false){

            gotoAndPlay ("Scene 1", 1);

            }

             

            should be

             

            if (key == false){

            gotoAndPlay ("Scene 1", 1);

            }

             

            because using '=' is an assignment basically putting 'false' into the key variable, whereas '==' is an equality operator.

             

            So for example if 'false' was the value inside 'key' and this was equal to the right side of the operator it would do the if statement ( it is basically going; if false is equal to false then gotoAndPlay... unless key was 'true' in which case it would skip the if statement.