6 Replies Latest reply on Mar 7, 2008 1:10 PM by cartoonme91

    Simple AS 1.0 ques

    cartoonme91
      AS 1.0
      I have this code in a keyframe on the main timeline:

      var score = 0;
      if (score == 2500){trace("winner")}

      Othe stage in the maintimeline i have a dynamic text field I labeled "score" in the var box. The instance name is "pts" but i wasnt sure if i needed an instance name anyway here. The score box works fine as far as keeping score but when it reaches 2500, I dont get my trace. What am I missing? Thanks

      -Chris
        • 1. Re: Simple AS 1.0 ques
          Level 7
          Chris,

          > The score box works fine as far as keeping score

          Yup, and that comes from the association with the variable (via the Var
          field of the Property inspector). You don't need the instance name for
          that, but it's often helpful to think of movie clips in terms of the
          MovieClip class, in which case, practically speaking, the instance name
          functions very much like a variable that you would use for any other sort of
          instance.

          For example ...

          var myArray = new Array();

          ... gives you a variable (myArray), that contains all the functionality
          defined by the Array class. Things that object can do are called methods,
          things it can react to are called events, and characteristics it has are
          called properties -- collectively called class members. Any of these class
          members can be refrence by way of the variable. If you want to add
          something to the array, you use the Array.push() method ...

          myArray.push("something");

          In the same way, movie clip symbols -- with instance names -- are used
          in the same way to access members of the MovieClip class. For example,
          this:

          myClip._x = 400;

          ... puts the movie clip with the instance name myClip at 400 pixels in from
          the left of its parent timeline.

          But I'm rambling. :-p

          > but when it reaches 2500, I dont get my trace.
          > What am I missing? Thanks

          That's one of the dangers of using the Var field, only because it
          potentially gives you the idea that things can be wired up, then left to
          operate on their own -- which isn't always the case. In your code ...

          if (score == 2500){trace("winner")}

          The trace will indeed trace "winner" when the score variable reaches
          2,500 -- but this will only happen if the condition is met *when* that line
          of code is executed. If that line appears in frame 100 -- totally making up
          a number -- and if score is 2,500 by the time the playhead reaches that
          frame, then you'll see the trace. If that line is on frame 1, when the
          value of score is presumably 0, then it's executed and done with. It won't
          be revisited until you run that same line of code again. Does that make
          sense?


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


          • 2. Re: Simple AS 1.0 ques
            cartoonme91 Level 1
            I think so, but my maintimeline is only 1 frame long so isnt that line of code being run over and over?
            • 3. Re: Simple AS 1.0 ques
              Level 7
              cartoonme91,

              > my maintimeline is only 1 frame long so isnt that line
              > of code being run over and over?

              It isn't. You can prove it by creating a new FLA file and entering the
              following single line in frame 1:

              trace("testing");

              Test your SWF and see how often you see the word "testing" in the Output
              panel. Just once!

              So to continuously run that line, you could wrap it in a function and
              trigger it repeatedly with setInterval(). That's one approach.

              function checkScore() {
              if (score == 2500){trace("winner")}
              }

              setInterval(checkScore, 50);

              In the above few lines, the setInterval() function triggers the
              checkScore() function every 50 milliseconds. That should do it, but it
              means you're triggering the checkScore() incessantly, which may not be
              necessary -- and, done often enough, with enough various functions, can
              potentially bog down your SWF. Much better, if you can, to call the
              checkScore() function as part of the routine that updates your score
              variable in the first place. That way, whenever the score updates, you
              respond based on its new value (and only then).


              David Stiller
              Contributor, How to Cheat in Flash CS3
              http://tinyurl.com/2cp6na
              "Luck is the residue of good design."


              • 4. Re: Simple AS 1.0 ques
                cartoonme91 Level 1
                I just added your code and did indeed get the winner trace over and over. I then tried what you said (i think) and called the checkScore function in the keyframe of the mc where the code is written that tells the score to increase. For some reason it doesn't work. I created a very basic way to get it to work for now but I'd still like to figure it out for future ref and for my own sanity. so in the maintimeline keyframe, i have

                score = 0;
                function checkScore() {
                if (score == 2500){trace("winner")}
                }


                then in the mc keyframe where the score is changed I have:

                score += 500;
                checkScore();


                Am i way off? Thanks alot for your help.
                • 5. Re: Simple AS 1.0 ques
                  Level 7
                  cartoonme91,

                  > I then tried what you said (i think) and called the checkScore
                  > function in the keyframe of the mc where the code is written
                  > that tells the score to increase.

                  Okay.

                  > For some reason it doesn't work.

                  Everything depends on positioning. You can easily reach for the
                  softdrink sitting next to your computer, because it's right there. If you
                  try to reach for the softdrink that's in the fridge, you actually have to
                  get up and reference the fridge before picking up your drink.

                  In code, it's similar. The keyframe inside that movie clip (where the
                  code is written to increase the score) may not "see" the function from it's
                  point of view. You might need to reference the function by preceding it
                  with a prefix of some kind, such as _parent.checkScore(), or
                  instanceNameOfClipThatContainsTheCode.checkScore() -- if that makes sense.

                  > I created a very basic way to get it to work for now but I'd still
                  > like to figure it out for future ref and for my own sanity.

                  Sure thing. :)

                  > so in the maintimeline keyframe, i have
                  >
                  > score = 0;
                  > function checkScore() {
                  > if (score == 2500){trace("winner")}
                  > }

                  Right.

                  > then in the mc keyframe where the score is changed I have:
                  >
                  > score += 500;
                  > checkScore();

                  If that works, then you're perfectly fine. In fact, that's what I was
                  mentioning before: you could just reference the checkScore() function from
                  wherever you increase the score variable. Sounds like you got it! :)


                  David Stiller
                  Adobe Community Expert
                  Dev blog, http://www.quip.net/blog/
                  "Luck is the residue of good design."


                  • 6. Re: Simple AS 1.0 ques
                    cartoonme91 Level 1
                    Thats just it, I thought that that would work but it doesn't. The code I posted last won't trace unless I do as you mentioned and have it constantly tracing winner over and over. The basic solution I was refering to was that I added a mc and targeted its timeline to advance a frame each time the user clicks, which also causes the score to increase by 500 but the 2 are unrelated. It traces on the mc's last frame which also happens to be when score reaches 2500. Its a convenient solution because the score incriments dont vary so im able to pull it off. But I was surprised that

                    "score += 500;
                    checkScore();"

                    when attatched to mc didnt trace winner when it reached 2500. I know its difficult to diagnose without the fla. I think I covered everything. Could it be an issue with AS 1.0? Here is what I have:

                    http://qa.mspub1.com/GiftItAmerica/Guitar-Hero-3/Banners/Fretboard/FLA/ARG/728x90_ghetto4. html


                    Each of those falling notes have a timeline of 44 frames and are told gotoAndPlay(1); when they reach frame 40. When the hittest returns true, it skips stright to frame 41 where the score increases by means of:

                    "score += 500;

                    This is where I also have:


                    checkScore();"

                    And in a keyframe in the maintimeline i have:

                    > score = 0;
                    > function checkScore() {
                    > if (score == 2500){trace("winner")}
                    > }

                    I dont know if that helps at all, but Ill stop bugging you with this one unless you think you might see where i went wrong based on what Ive told you. I wish I understood this stuff as well as you guys. Id be a far better designer. thanks again.