4 Replies Latest reply on Feb 25, 2008 12:52 PM by Rothrock

    onPress event is occurring onLoad

    bhuber7 Level 1
      Hello. I wrote a simple function that I can call whenever a button in my movie is pressed. All of the buttons serve as links that open a new page. The problem I'm having is instead of calling the function with a button's onPress event, the URL gets opened as soon as the SWF gets loaded.

      Can anyone find what I'm doing wrong? Thanks in advance.
        • 1. onPress event is occurring onLoad
          i_am_captivate Level 1
          Hello, I believe your could should look something like the following:


          HTH,

          Doug
          • 2. Re: onPress event is occurring onLoad
            Level 7
            > Hello, I believe your could should look something like the following:

            btnStartHere.onPress = function loadPage(page) {
            GetURL(page, "_self");
            }
            // etc.

            Bingo! The reason it's executing immediately for bhuber7 is because of
            the parentheses (), which act as the trigger on that function.

            btnStartHere.onPress = LoadPage("starthere.html");

            In this case, the LoadPage() function is executed right there, because
            that's what the parentheses do. The difference in imjustabeginner's example
            is that the LoadPage() function is wrapped inside an anonymous function.
            Alternately, it could have been done like this:

            btnStartHere.onPress = LoadPage;

            ... where the parameter (an HTML document reference) is retrieved some other
            way.


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


            • 3. Re: onPress event is occurring onLoad
              bhuber7 Level 1
              Thanks for that explanation David. I never knew that parentheses cause a function to be triggered right away. That's very useful info for me in the future. I wanted to write the loadPage function so I wouldn't have to write out btnSoAndSo.onPress = function() { blah blah } over and over, but it seems like I'll have to do that anyway.

              Thanks again!
              • 4. Re: onPress event is occurring onLoad
                Rothrock Level 5
                No. That isn't what this means. It just means you can't pass argument directly to the function. Instead you can do what David hints at.

                btnStartHere.page="starthere.html";
                btnQuestions.page="help.html;

                function loadPage(){
                GetURL(this.page,"_self");
                }

                btnStartHere.onPress=loadPage;
                btnQuestions.onPress=loadPage;

                Or something like that. Each time the loadPage function is called it will be executing within the scope of the clip that was pressed. So if each instance has a dynamically created property of page, "this.page" will refer to the local page variable. I like to think of this as each instance holds its own and knows what it needs to do.