1 Reply Latest reply on Mar 1, 2011 4:50 PM by epdomingos

    How to set movie clip position relative to stage with classes?

    epdomingos

      Hi mates!

       

      I´m developing my first flash app. i´m trying to use classes beacause they'll be very important in my future app.

      I´m getting an annoying problem that I can´s solve so far

       

      I created a simple movieclip with a star. However, instead of putting it directly on the stage, I´m calling it from library, using the addChild method to place the star in the stage.

      To do that, I simple right click over the movieclip icon->properties->check export to action script->and I give a name to the class (for example: myMC)

      Then, inside the class i wrote:

      package 
      {
           import flash.display.*;
           import flash.events.*;
           public class myMC extends MovieClip
           {
                var star:myMC=new myMC();
                public function myMC()
                {
                     // constructor code
                     stage.addChild(star);
                     star.x = 250;
                     star.y = 200;
                   }
           }
      }
      

      The result is a start on the stage in the correct position.

       

      But now, imagine that i want to drag the star , and when I drop it I want to place it again on x=250 and y=200.

      So, I created a new class, and I called it DragDrop. I will not annoy you with the class details, however I will post only the lines of interest:

      package 
      {
           import flash.display.*;
           import flash.events.*;
           public class DragDrop extends MovieClip
           {
                var origin_x:Number;
                var origin_y:Number;
                public function DragDrop()
                {
                            origin_x = this.x;
                            origin_y = this.y;
                      }
              }
      }
      
      

      As I´m placing the two variables origin_x and origin_y inside the constructor, I´m expecting to get 250 and 200 respectively. After drop the star, I want to send it to it´s original place, and that´s why I need to keep this values.

      And here´s the problem, origin_x and origin_y are both ZERO, instead of 250 and 200... The value is not relative to stage

      I managed a little bit and I found this post : http://stackoverflow.com/questions/338988/flash-as3-get-set-absolute-position-of-movieclip

      Unfortunately I tried to replace by the suggested code, but both values remain zero.

       

      An interesting thing, is that if I put the star directly in the stage instead of calling it from the library as I´m doing, I get correct values in origin_x and origin_y.

       

      Sorry for the long post, but I really need to understand what is happening here!

       

      Could you help me?

       

      Thanks in advance,

      Eduardo