5 Replies Latest reply on May 17, 2006 5:46 AM by Newsgroup_User

    So aggravating

    Briancapo
      Hi guys, im trying to do some code, and it will not work.
      Im trying two things neither of which seem to be working for me.
      1. I am trying to make a button, on the first click it dynamically loads a message. On the second click, i want the message to unload. If anyone could help me with this code i would be greatly appreciative, however, my second idea is the one which i would prefer.

      2. When a hit test occurs between 2 movie clips, lets call them a and b, AND the key "enter" is hit, the message pops up. And, when enter is hit again (or the hit test is no longer occuring) the message goes away. If this doesnt make sense, it helps to think of old gameboy games, such as Zelda or Pokemon, when your talking to an npc.

      Thanks alot guys. I hope you can help.
        • 1. Re: So aggravating
          Level 7
          Briancapo,

          > Hi guys, im trying to do some code, and it will not work.

          Ehh, sure it will. Just got to do it right.

          > Im trying two things neither of which seem to be working
          > for me.

          Let's step through it, then.

          > 1. I am trying to make a button, on the first click it
          > dynamically loads a message. On the second click, i want
          > the message to unload.

          Show us what you've done so far. Based on your description, it sounds
          like you already have a button symbol (or maybe a movie clip symbol, which
          can act like a button). Presumably, you've made a few attempts to wire this
          button up.

          So let's see what you have so far. Not trying to rake you through the
          coals, I promise ... just want to see what approach you've taken, so we can
          step through what went wrong.

          > 2. When a hit test occurs between 2 movie clips, lets call
          > them a and b, AND the key "enter" is hit, the message
          > pops up.

          Sure. That's a bit more complicated, but we can do that, too.

          > And, when enter is hit again (or the hit test is no longer
          > occuring) the message goes away.

          You can solve just about anything when you break it down into small
          steps. Let's start with your first goal and move from there. We'll manage
          both #1 and #2, but let's just start at the beginning.

          Lemme know what you've got.


          David
          stiller (at) quip (dot) net
          Dev essays: http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: So aggravating
            Briancapo Level 1
            Thank you very much for the reply, with the help of a friend i got number one, but number two is what i really want, and what i really cant even contemplate what i am doing wrong. My coding is very basic, it runs me into alot of problems but here it goes. For number 2:
            box is the rectangular hittest
            SPACE= spacebar?
            (on the movieClip hittest)
            onClipEvent(enterFrame){
            if(this.hitTest(_root.char))
            if(key.isDown(65)){
            _root.message.loadMovie("message.swf");
            }else{

            and thats where i cant get to (even though im sure the rest is wrong) the closest ive gotten would be when you step on the movie clip, and hti a, it loads once you step off. Thanks again for your help, i realize this is terrible coding, but we only learned the basics in my class.
            • 3. Re: So aggravating
              Level 7
              Briancapo,

              > number two is what i really want, and what i really
              > cant even contemplate what i am doing wrong.

              Here's hoping we can fix that. Once you learn the ropes with
              ActionScript, you'll have a much easier time.

              > For number 2:
              > box is the rectangular hittest

              > SPACE= spacebar?

              This ... I'm not even sure what this is. It seems like you're declaring
              a variable, but I can't tell if that's what you're after. To declare a
              variable, use the var statement.

              var variableName = value;

              But in your example, I don't see where you're using the SPACE variable,
              if that's what that is.

              > onClipEvent(enterFrame){

              Okay, this is the onClipEvent handler, which means your code is
              literally attached to a movie clip, rather than sitting in a frame.
              Personally, I prefer to give my movie clips instance names and put my code
              in frames. For one, it's all in one place (all in a few frames of a script
              layer). on() and onClipEvent() is Flash 5-era. Not saying it's wrong to
              use, but we're three versions of Flash beyond that.

              In any case, the onClipEvent handler is a kind of "sandwich", comprised
              of the bread ...

              onClipEvent(eventHere) {
              ... and the other half ...
              }

              Inside those brackets is where you put your instructions.

              > if(this.hitTest(_root.char))

              So here's your first instruction, an if() statement. You're using the
              MovieClip.hitTest() method to test if this movie clip is overlapping with
              another, presumably instance-named char, in the _root. What you're missing
              here are the same kind of brackets the onClipEvent() handler has. You need
              to follow the if() statement with { ... more code here ...}.

              > if(key.isDown(65)){

              Now we have yet another if() statement, and this one correctly has the
              brackets. Your problem is that you're using the Key class incorrectly.
              There is no object named key in ActionScript, but there is a Key
              (capitalized) class.

              Objects in ActionScript are defined by their namesake classes. Movie
              clip symbols on the Stage are defined by the MovieClip class. Dynamic and
              input text fields are defined by the TextField class. These are examples of
              classes that can be instantiated (that is, have an instance made of them).
              A movie clip on the Stage is an instance of (an object of) the MovieClip
              class. So when you give that clip an instance name, you may use that name
              to summon any of the functionality described by the class. Things the
              object can do are called methods; things it can react to are called events;
              characteristics the object has are called properties -- you'll see these
              summaries in all class entries of the ActionScript 2.0 Language Reference,
              available via the F1 key.

              The Key class is an example of a static class, which means you don't
              instantiate it. You just call it directly. Again, any methods, events, and
              properties that class has are listed in its entry in the AS Language
              Reference. The Key.isDown() method checks if a certain key is being held
              down.

              According to the Key.isDown() method entry, you must supply a number as
              a parameter. You've supplied 65, which is the letter A.

              > _root.message.loadMovie("message.swf");

              So, at this point, if MovieClip.hitTest(), as invoked against the "this"
              property (which means the movie clip we're attached to), returns true AND if
              the A key is currently held down, then the MovieClip.loadMovie() method is
              invoked against the a MovieClip instance by the instance name of message in
              the _root.

              > }else{

              And if not, well ... nothing.

              > and thats where i cant get to (even though im sure the
              > rest is wrong) the closest ive gotten would be when you
              > step on the movie clip, and hti a, it loads once you step
              > off.

              I think you're getting ahead of yourself, honestly. You'll do *much*
              better to break down this goal into small, bite-sized parts. By combining
              two if() statements, you already have two points of failure. I would start
              with a brand new FLA, create two quick movie clips, then run the
              onClipEvent(enterFrame) handler to make sure you've mastered
              MovieClip.hitTest() first.

              Personally, I would skip onClipEvent() and go with an instance name
              approach.

              clipAInstanceName.onEnterFrame = function() {
              if (this.hitTest(clipBInstanceName) {
              trace("hit!");
              }
              }

              (Code goes in a frame of the timeline that holds both movie clips.)

              Once you really "get" MovieClip.hitTest(), save that FLA and start a new
              one, and in that one put together *just* a Key.isDown() example, until you
              master that. When you've mastered both, combine them.


              David
              stiller (at) quip (dot) net
              Dev essays: http://www.quip.net/blog/
              "Luck is the residue of good design."


              • 4. Re: So aggravating
                Briancapo Level 1
                Thanks again for helpin me out, still not getting it though.

                For the movie clip part, i did know how to do that (in the old fashion onClipEvent way) but i typed it before school and obviously made some errors

                However, the Key.isDown is still causing me trouble.

                I know how to properly use this code with the arrow keys ( i currently have my main character moving via the arrow keys) but i still cant get it so the trace thing works. Here is what i have:

                if (Key.isDown(65)){
                trace("hit!");
                }

                This doesn't work. Also, how do i correctly make a double if statement (such as combining the hittest and the Key.isDown)
                Finally, how would i make it so that the movieclip unloads if the the hitTest is not occuring(i want it to unload if the hitTest is not occuring, but it shouldnt unload if "a" is not being pressed) ? Thanks again for your help!!!
                • 5. Re: So aggravating
                  Level 7
                  Briancapo,

                  > I know how to properly use this code with the arrow keys
                  > ( i currently have my main character moving via the arrow
                  > keys) but i still cant get it so the trace thing works. Here is
                  > what i have:
                  >
                  > if (Key.isDown(65)){
                  > trace("hit!");
                  > }
                  >
                  > This doesn't work.

                  What? Sure it works! If I type that code exactly into a new FLA
                  and test the SWF file (while holding down the A key, of course), I see the
                  trace in my Output panel. Of course, in the above example, you have to be
                  holding down the A key exactly as the if() statement is executed. Maybe in
                  your own tests, the if() is executed before you hold down A? Put the above
                  into a frame loop and see if that makes a difference.

                  > Also, how do i correctly make a double if statement (such
                  > as combining the hittest and the Key.isDown)

                  Use the logical AND operator, &&.

                  if (Key.isDown(65) && someClip.hitTest(someOtherClip)) { ... }

                  You may also be interested in the logical OR operator, ||, which you can
                  combine with &&.

                  if (condition1 || condition 2) { ... }
                  if (condition1 || condition 2 || condition3) { ... }
                  if (condition1 && condition 2 || condition 3) { ... }
                  // etc.

                  > Finally, how would i make it so that the movieclip unloads
                  > if the the hitTest is not occuring (i want it to unload if the
                  > hitTest is not occuring, but it shouldnt unload if "a" is not
                  > being pressed) ? Thanks again for your help!!!

                  Unload should occur if A) hitTest() is not occurring and B) "A" is not
                  being pressed. That's a logical AND in combination with a logical NOT, !.

                  if (!Key.isDown(65) && !someClip.hitTest(someOtherClip)) { ... }

                  But in your case, you're saying you want the load with both conditions
                  are true and the unload if both conditions are not true.

                  if (Key.isDown(65) && someClip.hitTest(someOtherClip)) {
                  someClip.loadMovie();
                  // whatever else
                  } else {
                  someClip.unloadMovie();
                  // whatever else
                  }


                  David
                  stiller (at) quip (dot) net
                  Dev essays: http://www.quip.net/blog/
                  "Luck is the residue of good design."