AS3简单事件问题

时间:2010-08-08 16:58:54

标签: flex flash actionscript-3 events click

我正在尝试获取MC的百分比x坐标。例如,My MC宽500像素,高20像素。当我点击我的MC部分时,我想得到我的MC'x坐标的百分比......(20%将是我的MC的100px ......)..谁知道怎么做?感谢..

我的代码

progressBa.addEventListener(MouseEvent.CLICK, barClick);

  private function barClick(event:MouseEvent):void{
    perct=this.mouseX/progressBa.Width;  //not sure how to do it....
   }

1 个答案:

答案 0 :(得分:1)

我对Actionscript不是很熟悉,但我使用trace(event)函数来查看事件中的内容:

[MouseEvent type="click" bubbles=true cancelable=false eventPhase=2 localX=202 localY=5 stageX=296 stageY=88 relatedObject=null ctrlKey=false altKey=false shiftKey=false buttonDown=false delta=0]

如您所见,MouseEvent对象有一个名为localX的属性,这是您需要的属性。因此,movieclip的实际类是:

package 
{
  import flash.display.MovieClip;
  import flash.events.*;

  public class progressBa extends MovieClip
  {
    public function progressBa()
    {
      // Add a mouse event to this, the movieclip called progressBa
      this.addEventListener(MouseEvent.CLICK, clickBar);
    }

    private function clickBar(e:MouseEvent):void
    {
      // Get click location's x-coordinate in percentages
      var percent = 100 * e.localX / this.width;
      trace(percent);
    }
  }
}

令我惊讶的是,我能够点击100.018 ...%,可能是因为反别名。