2 Replies Latest reply on Jul 28, 2007 5:39 PM by SummerLongSince

    basic AS3

    SummerLongSince
      Hi,
      I'm trying to learn AS3. I was very comfortable with AS2, but now even the most basic things seem impossible for me. If someone could give me a hand with this I would really appreciate it. This is a basic AS2 example that I want to be able to replicate in AS3 and from them learn more about the logic behind it:

      In AS2:
      I could have a movie clip act like a button, and add properties to it to be called later.

      // myClip is a movie clip on the stage
      // create a new property for it: temperature
      myClip.temperature = '57F';
      // have the clip trace the value of temperature once it is pressed
      myClip.onPress = function(){
      trace (this.temperature);
      }


      so the question is, how do you do something like this in AS3. I have tried many different ways and none seem to work.

      Thanks so much for any help.
        • 1. Re: basic AS3
          Level 7
          SummerLongSince,

          > I'm trying to learn AS3. I was very comfortable with AS2,
          > but now even the most basic things seem impossible for me.

          Have you read any of the migration articles published in Adobe's Dev
          Center? These may likely give you a good, general purpose start with the
          new stuff.

          > In AS2:
          > I could have a movie clip act like a button, and add properties
          > to it to be called later.

          This hasn't changed in AS3; not in principle, anyway. In your example
          code, the event handling syntax has changed.

          > myClip.temperature = '57F';
          > myClip.onPress = function(){
          > trace (this.temperature);
          > }

          In AS3, that could be rendered like this:

          myClip.buttonMode = true;
          myClip.temperature = "57F";
          myClip.addEventListener(
          MouseEvent.MOUSE_DOWN,
          function(evt:MouseEvent):void {
          trace(evt.target.temperature);
          }
          )

          Let's take a quick look at the differences. The first line instructions
          Flash Player to show the finger cursor when the mouse is rolled over the
          myClip instance. Without that line, the event handling still takes place,
          but because this is a movie clip symbol and not a button, the cursor does
          not automatically change from the default arrow shape.

          The second line is identical to the first line in your example. I used
          double quotes, but it doesn't make any difference in this case.

          The remaining lines show the biggest difference. The structure of AS3
          event handling goes like this:

          object.addEventListener(eventToListenFor, functionToPerform);

          There are additional parameters (optional) you may provide after these
          first two, but the ones shown are the basic necessities: what am I
          listening for? what should I do when I encountered the dispatched event.

          As you can see, I split these two parameters over a number of lines, but
          the principle is the same (and honestly, not unlike the principle in the AS2
          version ... even there, the same concept applies: what am I listening for?
          what should I do when that thing occurs?).

          AS3 event handling is similar to one of the *four* (yes, four!) ways
          event handling was possible in AS2. There was the on()/onClipEvent()
          approach, the newer dot notation approach (as used in your example), the
          addListener() method, and finally (for v2 Components) the addEventListener()
          method. In AS3 -- barring a teensy example or two in video programming --
          event handling is managed the same way. This consistency, alone, marks a
          significant improvement to me. Once you know it, you know it. :)

          Not that instead of the global "this" property, the AS3 version's
          trace() statement refers to the expression evt.target. The variable evt
          (short for "event") is found between the parentheses of the function
          literal. The name evt is arbitrary, but it refers to the MouseEvent
          instance passed into the function when the MouseEvent.MOUSE_DOWN event is
          dispatched. The MouseEvent instance features a target property, and in AS3,
          that property refers to the object that dispatched the event in the first
          place, which makes the expression "evt.target" equivalent to "this" in this
          case. An alternative would simply be to replace avt.target.temperature with
          myClip.temperature in the trace() statement.


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


          • 2. Re: basic AS3
            SummerLongSince Level 1
            David,
            Thank you so much for your kind and useful reply. You answered many of the unknowns that had been frustrating me in the past days. I will definitely take a look at your book too.
            Best of All,
            Mauricio