闪存/ AS3;获取/设置MovieClip的绝对位置?

时间:2021-07-13 20:28:56

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

如何在Flash / AS3中获取/设置MovieClip的绝对位置?绝对地,我指的是它相对于舞台的起源的位置。

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 have a feeling it requires that the Stage is left aligned.

这似乎有效,但我有一种感觉,它需要舞台左对齐。

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

但是,我没有工作的吸气剂。这不起作用:

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

I'm aiming for a solution that works, and works with all Stage alignments, but it's tricky. I'm using this on a Stage which is relative to the browser's window size.

我的目标是一个有效的解决方案,并适用于所有Stage对齐,但它很棘手。我在舞台上使用它,这相对于浏览器的窗口大小。

EDIT: This works for a top-left aligned stage; 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;
}

4 个解决方案

#1


9  

Two things:

Why the substractions?

为什么要减法?

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

should give the proper result already. If the parent clip is scaled, your calculation will be off by the scaling factor...

应该给出正确的结果。如果缩放父剪辑,则缩放因子将使您的计算失效...

Just a shot in the dark, but you could add a globalToLocal(this.stage) for compensating the alignment issues?

只是在黑暗中拍摄,但你可以添加一个globalToLocal(this.stage)来补偿对齐问题?

#2


4  

THANK YOU SO MUCH!!!!

非常感谢!!!!

TO GET POSITION OF OBJECT RELATVE TO STAGE USE:

获取对象的位置与舞台使用相关:

  • (object as DisplayObject).localToGlobal(new Point()).x;
  • (作为DisplayObject的对象).localToGlobal(new Point())。x;

  • (object as DisplayObject).localToGlobal(new Point()).y;
  • (作为DisplayObject的对象).localToGlobal(new Point())。y;

#3


2  

Agree with moritzstefaner that you don't need the subtraction stage, however for your setter I actually think you should use globalToLocal, and use localToGlobal for your getter. These will take care of scaling and rotation as well as position.

同意moritzstefaner你不需要减法阶段,但对于你的setter我实际上认为你应该使用globalToLocal,并使用localToGlobal作为你的getter。这些将负责缩放和旋转以及位置。

#4


0  

i couldnt use localToGlobal so as an alternative solution is to get the position of the mouse in the scope you want :

我不能使用localToGlobal,因此替代解决方案是将鼠标的位置放在您想要的范围内:

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)
}

#1


9  

Two things:

Why the substractions?

为什么要减法?

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

should give the proper result already. If the parent clip is scaled, your calculation will be off by the scaling factor...

应该给出正确的结果。如果缩放父剪辑,则缩放因子将使您的计算失效...

Just a shot in the dark, but you could add a globalToLocal(this.stage) for compensating the alignment issues?

只是在黑暗中拍摄,但你可以添加一个globalToLocal(this.stage)来补偿对齐问题?

#2


4  

THANK YOU SO MUCH!!!!

非常感谢!!!!

TO GET POSITION OF OBJECT RELATVE TO STAGE USE:

获取对象的位置与舞台使用相关:

  • (object as DisplayObject).localToGlobal(new Point()).x;
  • (作为DisplayObject的对象).localToGlobal(new Point())。x;

  • (object as DisplayObject).localToGlobal(new Point()).y;
  • (作为DisplayObject的对象).localToGlobal(new Point())。y;

#3


2  

Agree with moritzstefaner that you don't need the subtraction stage, however for your setter I actually think you should use globalToLocal, and use localToGlobal for your getter. These will take care of scaling and rotation as well as position.

同意moritzstefaner你不需要减法阶段,但对于你的setter我实际上认为你应该使用globalToLocal,并使用localToGlobal作为你的getter。这些将负责缩放和旋转以及位置。

#4


0  

i couldnt use localToGlobal so as an alternative solution is to get the position of the mouse in the scope you want :

我不能使用localToGlobal,因此替代解决方案是将鼠标的位置放在您想要的范围内:

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)
}