4 Replies Latest reply on Mar 2, 2007 7:33 AM by Johnny_Rodgers

    [Flash 8 Actionscript] _x property of dynamically created movieclip

    Johnny_Rodgers
      Hi everyone.

      I'm having trouble with how Flash sets the _x property of a dynamically created movieclip.

      If I create a rectangle on the stage using the Drawing API:

      //for the purposes of this example, assign a value to i
      var i:Number = 0;

      //assign var squareName
      var squareName:String = "square" + i;

      //create an empty movieclip named [squareName] at depth 1
      this.createEmptyMovieClip([squareName], 1);

      //draw a 100 x 100 pixel rectangle originating at coordinates (100, 100)
      this[squareName].beginFill("0xFFFFFF", 100);
      this[squareName].moveTo(100, 100);
      this[squareName].lineTo(200, 100);
      this[squareName].lineTo(200, 200);
      this[squareName].lineTo(100, 200);
      this[squareName].lineTo(100, 100);
      this[squareName].endFill(); // stop the fill

      //trace the _x property of this[squareName]
      trace("this[squareName]._x: " + this[squareName]._x); //RETURNS 0: shouldn't this be 100?

      The _x property of the dynamically created movieclip is relative to itself (ie. 0...the upper left corner of the movieclip). I know I can get the global _x value of the movieclip by creating a bounds object and calling targetBounds.xMin using something like this:

      //trace the global x coordinate for the movieclip
      var targetBounds:Object = this[squareName].getBounds(_root);
      trace ("targetBounds.xMin: " + targetBounds.xMin);

      But how do I create a movieclip so that it's _x property will be relative to the Stage in the first place, and not itself? It's important, because if I then want to move the movieclip using something like this:

      this[squareName]._x = 200;
      trace("this[squareName]._x: " + this[squareName]._x); //RETURNS 200: shouldn't this be 300?

      Then the square moves 200 to the right within itself. This is clearly not what I want to do. I want to set the value of _x to the coordinate 200 (not 300, as this does). I have tried doing the same using the setProperty function:

      setProperty(this[squareName], _x, 200);

      But with the same result. This example code is highly simplified from the working file, but shows the problem.

      I'm not new to Flash by any stretch of the imagination, but I have never come up against this problem before. For context, I'm actually getting my feet wet with the Tween class, and am having trouble animating dynamically created movieclips due to this confusion with coordinates. When I tween a dynamically created clip from one set of coordinates to another, they are offset by the movieclip's _x and _y properties starting at 0, 0, rather than their actual position on stage.

      ANY help is greatly appreciated, as I've been fighting with this for hours.

      Cheers! JR.
        • 1. Re: [Flash 8 Actionscript] _x property of dynamically created movieclip
          NoNickNeeded Level 1
          I'm thinking that your movieclip gets placed at 0,0. Then, you create a rectangle INSIDE of that movie clip, at 100,100. So, tracing the _x value of the movieclip, 0 would be the correct value. Changing _x to 200 will move the movieclip 200 pixels to the right. Visually, it would appear as though the rectangle is now 300 pixels from the left side of the stage - 200 for the parent movieclip, plus another 100 based on where you started the rectangle.

          You could just start the rectangle at 0,0 inside the movieclip, then move the clip to where ever you wanted.
          • 2. Re: [Flash 8 Actionscript] _x property of dynamically created movieclip
            kglad Adobe Community Professional & MVP
            the moveTo() method doesn't move the movieclip. it moves the drawing position.
            • 3. Re: [Flash 8 Actionscript] _x property of dynamically created movieclip
              Johnny_Rodgers Level 1
              Thanks very much NoNickNeeded and kglad. It's a simple thing, but that makes sense to me now. I got my head around it thanks to TheSpork's post over at Kirupa: http://www.kirupa.com/forum/showthread.php?t=252026

              if I modify my original code to this:


              //for the purposes of this example, assign a value to i
              var i:Number = 0;

              //assign var squareName
              var squareName:String = "square" + i;

              //create an empty movieclip named [squareName] at depth 1
              this.createEmptyMovieClip([squareName], 1);

              this[squareName]._x = 100; //set the _x property of movieclip
              this[squareName]._y = 100; //set the _y property of movieclip

              //draw a 100 x 100 pixel rectangle
              this[squareName].beginFill("0xFFFFFF", 100);
              this[squareName].moveTo(0, 0);
              this[squareName].lineTo(100, 0);
              this[squareName].lineTo(100, 100);
              this[squareName].lineTo(0, 100);
              this[squareName].lineTo(0, 0);
              this[squareName].endFill(); // stop the fill

              //trace the _x property of this[squareName]
              trace("this[squareName]._x: " + this[squareName]._x); //RETURNS 100


              Then I've got what I want! Thanks very much for your help. Cured my headache.
              • 4. Re: [Flash 8 Actionscript] _x property of dynamically created movieclip
                Johnny_Rodgers Level 1
                Thanks very much NoNickNeeded and kglad. It's a simple thing, but that makes sense to me now. I got my head around it thanks to TheSpork's post over at Kirupa: http://www.kirupa.com/forum/showthread.php?t=252026

                if I modify my original code to this:


                //for the purposes of this example, assign a value to i
                var i:Number = 0;

                //assign var squareName
                var squareName:String = "square" + i;

                //create an empty movieclip named [squareName] at depth 1
                this.createEmptyMovieClip([squareName], 1);

                this[squareName]._x = 100; //set the _x property of movieclip
                this[squareName]._y = 100; //set the _y property of movieclip

                //draw a 100 x 100 pixel rectangle
                this[squareName].beginFill("0xFFFFFF", 100);
                this[squareName].moveTo(0, 0);
                this[squareName].lineTo(100, 0);
                this[squareName].lineTo(100, 100);
                this[squareName].lineTo(0, 100);
                this[squareName].lineTo(0, 0);
                this[squareName].endFill(); // stop the fill

                //trace the _x property of this[squareName]
                trace("this[squareName]._x: " + this[squareName]._x); //RETURNS 100


                Then I've got what I want! Thanks very much for your help. Cured my headache.