Flash / AS3; get / set the absolute position of the MovieClip?

How do you get / set the absolute position of a MovieClip in Flash / AS3? Absolutely, I mean its position relative to the origo stage.

I currently have this setter:

class MyMovieClip extends MovieClip
{
  function set xAbs(var x:Number):void
  {
    this.x = -(this.parent.localToGlobal(new Point()).x) + x;
  } 
}

      

This seems to work, but I feel like it requires the Stage to be aligned.

However, I don't have a working getter. This does not work:

public function get xAbs():Number 
{
  return -(this.parent.localToGlobal(new Point()).x) + this.x; // Doesn't work
}       

      

I am aiming for a solution that works and works with all Stage alignments, but it is tricky. I use this in a step that depends on the size of the browser window.

EDIT: This works for left alignment; not sure about others:

public function get AbsX():Number 
{
    return this.localToGlobal(new Point(0, 0)).x;
}       
public function get AbsY():Number 
{
    return this.localToGlobal(new Point(0, 0)).y;
}       
public function set AbsX(x:Number):void
{
    this.x = x - this.parent.localToGlobal(new Point(0, 0)).x;
}
public function set AbsY(y:Number):void
{
    this.y = y - this.parent.localToGlobal(new Point(0, 0)).y;
}

      

+1


source to share


4 answers


Two things:

Why substitutions?

var x=this.parent.localToGlobal(new Point(this.x,0)).x; 

      



should give the correct result. If the parent clip is scaled, your calculation will be disabled by the scaling factor ...

Just in the dark, but can you add globalToLocal(this.stage)

to compensate for alignment problems?

+9


source


THANKS LOOKING MORE !!!!

GET THE POSITION OF THE OBJECT RELATED TO USING THE STEP:



  • (object as DisplayObject) .localToGlobal (new point ()). x;
  • (object as DisplayObject) .localToGlobal (new point ()). y;
+4


source


Agree with moritzstefaner that you don't need a subtraction step, however for your setter, I really think you should use globalToLocal and use localToGlobal for your receiver. They will take care of scaling and rotation as well as position.

+2


source


I couldn't use localToGlobal as an alternative solution is to get the mouse position in the area you want:

mynestesprite.addEventListener (MouseEvent.MOUSE_OVER, myover)
function myover(e:MouseEvent){
    // e.target.parent.parent ....
     trace ( e.target.parent.parent.mouseX, e.target.parent.parent.mouseY)
}

      

0


source







All Articles