11 Replies Latest reply on Apr 11, 2007 10:05 AM by Phoenix00017

    If statement and buttons

    timbronze
      I have some actionscript like below

      if (button1_btn.onPress = true)
      {
      buttonNum = button1_btn;
      titleNum = titleList_txt.title_1_txt;
      textName = TEXT1_txt;
      } // end if
      else if (button2_btn.onPress = true)
      {
      buttonNum = button2_btn;
      titleNum = titleList_txt.title_2_txt;
      textName = TEXT2_txt;
      } // end if
      else if (button3_btn.onPress = true)
      {
      buttonNum = button3_btn;
      titleNum = titleList_txt.title_3_txt;
      textName = TEXT3_txt;
      } // end if
      else{}

      when i perform the code on say button1_btn nothing happens, then button2_btn again nothing happens... And finnaly when i press button3_btn it works and what i want to happen happens...

      Why would it not work on the first two.

      Its rather strange and annoying me some what. Any help would be greatly appreciated.

      Kind regards

      Tim
        • 1. If statement and buttons
          Phoenix00017
          Couple of comments. When doing a comparison in an "if" statement, you need to use a double = sign. Something like:

          if ((1+1) == 2) trace("You're good at math!");

          A single = is assignment, == is comparison.

          However, if what you're doing is trying to set up functions to be called when you press a button, this isn't how you usually want to do it. I generally use a format like this:

          button1_btn.onPress = function(){
          buttonNum = button1_btn;
          titleNum = titleList_txt.title_1_txt;
          textName = TEXT1_txt;
          }

          onPress is an event handler, not a state of the button. Thus, you need to set it to a function, like above (or to a named function). What this is saying is "when the button is pressed, call the function assigned to onPress". I'm not sure how to check the current state of the button by hand. Hope this helps.
          • 2. Re: If statement and buttons
            timbronze Level 1
            I have decided to change my method... Thanks for the above tho.

            The reason i was doing the above is because of the below code

            _root.buttonNum.onPress = _root.titleNum.onPress = function ()
            {
            trace ("Text to display : " + textName);
            trace ("Button Number : " + buttonNum);
            trace ("TitleNum : " + titleNum);
            _root.textName.gotoAndPlay(2);
            for (i = 1; i < 10; i++)
            {
            titleList_txt._visible = _root["button" + i]._visible = _root["button" + i + "_btn"]._visible = false;
            } // end of for
            };

            I was trying to assign the values so they would get picked up in the function.

            I am a bit of a noob and am probably doing it all wrong.

            Cheers

            Tim
            • 3. If statement and buttons
              Phoenix00017 Level 1
              Well, I think what I suggested would work fine with what you want to do. Whenever you press a button, it would call the appropriate function and set the variables accordingly. This will work as long as the variables are declared in a higher scope. In other words, start out your actionscript in this frame with:

              var testName:String = new String(); // (or = "";)

              Then, you can set testName to whatever with the onPress events and you can access them in this part of the function.

              What you have looks fine at a glance, though I think I would reconsider one thing for easier readability and editing:

              _root.buttonNum.onPress = showStuff();
              _root.titleNum.onPress = showStuff();

              function showStuff()
              {
              trace("Text to...)
              .
              .
              .
              }

              Feel free to post more questions if this wasn't clear.
              • 4. Re: If statement and buttons
                timbronze Level 1
                how would be best to assign a value to a button, for this?
                • 5. If statement and buttons
                  timbronze Level 1
                  sorry just saw your post.... will take a look and try it out...

                  Cheers for the help, it is much appreciated.

                  Tim
                  • 6. If statement and buttons
                    timbronze Level 1
                    var textName:String = "padded_txt";
                    var titleNum:String = "titleList_txt.title_1_txt";

                    _root.button1_btn.onPress = showText();

                    function showText()
                    {
                    trace ("Text to display : " + textName);
                    trace ("Button Number : " + buttonNum);
                    trace ("TitleNum : " + titleNum);

                    _root.textName.gotoAndPlay(2);
                    for (i = 1; i < 10; i++)
                    {
                    titleList_txt._visible = _root["button" + i]._visible = _root["button" + i + "_btn"]._visible = false;
                    } // end of for
                    }


                    I know have something more like this...
                    However it seems to run before i even press anything making everything _visible = false too early... also on each button I have i want to assign different values to the variables.

                    Regards

                    Tim
                    • 7. If statement and buttons
                      Phoenix00017 Level 1
                      This may have been my mistake - I actually just hit the same bump in my own programming. I think I was wrong about being able to say:

                      _root.button1_btn.onPress = showText();

                      ...for events. Instead, use:

                      _root.button1_btn.onPress = function()
                      {
                      trace ("Text to display : " + textName);
                      trace ("Button Number : " + buttonNum);
                      trace ("TitleNum : " + titleNum);
                      .
                      .
                      .
                      }

                      That should fix it up. I'm not sure why the other way doesn't work - it should (IMHO), but for some reason Flash doesn't seem to like it. Lemme know if that doesn't work.
                      • 8. Re: If statement and buttons
                        timbronze Level 1
                        Cheers have done as you suggested...

                        _root.button1_btn.onPress = _root.titleList_txt.title_1_txt.onPress = function()
                        {
                        _root.padded_txt.gotoAndPlay(2);
                        for (i = 1; i < 10; i++)
                        {
                        titleList_txt._visible = _root["button" + i]._visible = _root["button" + i + "_btn"]._visible = false;
                        } // end of for
                        }

                        _root.button2_btn.onPress = function()
                        {
                        _root.paddle_txt.gotoAndPlay(2);
                        for (i = 1; i < 10; i++)
                        {
                        titleList_txt._visible = _root["button" + i]._visible = _root["button" + i + "_btn"]._visible = false;
                        } // end of for
                        }
                        like above, the reason i was doing it like before was because i wanted to limit the number to just one function and simply include the variables passed fromteh button.

                        Cheers for the help.
                        • 9. Re: If statement and buttons
                          Phoenix00017 Level 1
                          I'm confused now...did the solution work or are you still having problems?
                          • 10. Re: If statement and buttons
                            timbronze Level 1
                            Cheers Phoenix..

                            It all worked well.

                            Tim
                            • 11. Re: If statement and buttons
                              Phoenix00017 Level 1
                              Fantastic, glad to hear it. Best of luck to you. :-)