Welcome, Guest
  • Author Topic: extending movieclip object  (Read 3164 times)

    Billy T

    • Server what's that
    • *
    • Posts: 4
      • View Profile
      • Email
    extending movieclip object
    « on: 10/04/03, 07:37 »
    Hey all  ;)

    I'm trying get my head around AS2 etc so I was playing around with trying to make a slider class that extends the movieclip object.

    So far I have

    class tests.Slider extends MovieClip {
       private var _nTargX:Number;
       private var _nTargY:Number;
       private var _nSpeed:Number;
       function Slider() {
          _nTargX = _x;
          _nTargY = _y;
          _nSpeed = 5;

          onEnterFrame = slide;
       }
       public function get getTargX():Number {
          return _nTargX;
       }
       public function get getTargY():Number {
          return _nTargY;
       }
       public function get getSpeed():Number {
          return _nSpeed;
       }
       public function set setTargX(nTargX:Number):Void {
          _nTargX = nTargX;
          trace("and yep");
       }
       public function set setTargY(nTargY:Number):Void {
          _nTargY = nTargY;
       }
       public function setTarg(nTargX:Number, nTargY:Number):Void {
          this.setTargX(nTargX);
          this.setTargY(nTargY);
       }
       public function set setSpeed(nSpeed:Number):Void {
          _nSpeed = nSpeed;
       }
       public function slide():Void {
          _x -= (_x-_nTargX)/_nSpeed;
          _y -= (_y-_nTargY)/_nSpeed;
       }
    }

    and then in my fla I made a ball mc and in its linkage set its class to 'Slider'

    this works in that the ball gets the new properties and the enterframe script but when I try to move the ball with

    but2_btn.onPress = function() {
       ball_mc.setTargX(Math.random()*Stage.width);
    };

    then nothing happens - anyone know what I'm doing wrong?

    Originally I was hoping to be able to do it with nothing on the stage and just make a new one with

    mySlider=new Slider(some vars, "a graphic to attach");

    but I didn't get anywhere with that either

    Any help would be much appreciated

    Thanks

    Jorge Solis

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 14616
      • View Profile
    Re:extending movieclip object
    « Reply #1 on: 10/04/03, 11:27 »
    This modified version works fine:

    [script]
    class Slider extends MovieClip {
      private var _nTargX:Number;
      private var _nTargY:Number;
      private var _nSpeed:Number;
      function Slider() {
         this._nTargX = this._x;
         this._nTargY = this._y;
         this._nSpeed = 5;

         this.onEnterFrame = slide;
      }
      public function getTargX():Number {
         return this._nTargX;
      }
      public function getTargY():Number {
         return this._nTargY;
      }
      public function getSpeed():Number {
         return this._nSpeed;
      }
      public function setTargX(nTargX:Number):Void {
         this._nTargX = nTargX;
         trace("and yep");
      }
      public function setTargY(nTargY:Number):Void {
         this._nTargY = nTargY;
      }
      public function setTarg(nTargX:Number, nTargY:Number):Void {
         this.setTargX(nTargX);
         this.setTargY(nTargY);
      }
      public function setSpeed(nSpeed:Number):Void {
         this._nSpeed = nSpeed;
      }
      public function slide():Void {
         this._x -= (this._x-this._nTargX)/this._nSpeed;
         this._y -= (this._y-this._nTargY)/this._nSpeed;
      }
    }
    [/script]

    Remove get and set keywords before the function name. Also use this keyword, since each instance use his own set of variables

    Jorge

    Billy T

    • Server what's that
    • *
    • Posts: 4
      • View Profile
      • Email
    Re:extending movieclip object
    « Reply #2 on: 10/04/03, 20:50 »
    nice one thanks Jorge!

    the 'this' I understand but why were get and set causing problems?

    Thanks again

    Jorge Solis

    • Global Moderator
    • Systems Administrator
    • *****
    • Posts: 14616
      • View Profile
    Re:extending movieclip object
    « Reply #3 on: 10/06/03, 11:51 »
    It gives me compile errors. Anyway, set and get keywords are for implicit (not explicit) set/get methods. So you can use

    function getSpeed():Number {
         return this._nSpeed;
    }

    or

    function get Speed():Number {
         return this._nSpeed;
    }

    But using get getSpeed is redundant. Check Implicit get/set methods in the Help panel

    Jorge

    Billy T

    • Server what's that
    • *
    • Posts: 4
      • View Profile
      • Email
    Re:extending movieclip object
    « Reply #4 on: 10/06/03, 16:11 »

    Check Implicit get/set methods in the Help panel

    Jorge



    will do.

    Thanks again mate