Flash:获取e.currentTarget的绝对位置

时间:2015-05-25 16:09:52

标签: actionscript-3 flash

好的,不确定标题。 但这就是我想要实现的目标:

基本上我正在尝试做一些测验,你可以将答案拖放到一个字段中。如果它们是相关的,它应该捕捉答案字段位置。

应该是

if(myobject.hitTestObject(targetField) && isCorrectAnswer()) {
   myobject.x = targetField.x;
   myobject.y = targetField.y;
}

但它并没有真正发挥作用。

所以这就是我所拥有的:

/**
* Generating dragable answer fields based on an array.
**/

function generateAnswer():void {

// creating text format
var myFormat:TextFormat = new TextFormat(); 

myFormat.color = 0x0066FF;    
myFormat.size = 24;  
myFormat.align = TextFormatAlign.CENTER 

// reference array to store all textfields
var referenceArray:Array = new Array();

// iterate through all answers in vocabListItems and generate textfields
var i:int; 

for (i = 0; i < vocabListItems.length; i++) { 
    var answerField:TextField = new TextField();   
    // Setting text to current answer
    answerField.text = vocabListItems[i];    
    answerField.width = 140;    
    answerField.height = 40;    
    answerField.x = 60+ i*150;    
    answerField.y = 410;  
    answerField.background = true;
    answerField.backgroundColor = 0xffffff;
    answerField.setTextFormat(myFormat);  
    answerField.selectable = false;
    answerField.type = TextFieldType.DYNAMIC

    // store the textfield in a container so drag and drop
    // will work
    var textContainer:Sprite = new Sprite();
    textContainer.addChild(answerField);
    addChild(textContainer);
    referenceArray.push(textContainer);
}

for each (var item in referenceArray) {
    item.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
    item.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
}
}

然后我开始拖动

var start_x:Number;
var start_y:Number;

function startDragging(e: MouseEvent):void {

  var object = e.currentTarget;
  start_x = e.currentTarget.x;
  start_y = e.currentTarget.y;
  object.startDrag();
}

我停止拖动

function stopDragging(e:MouseEvent):void {
e.currentTarget.stopDrag();

if (e.currentTarget.hitTestObject(targetField)) {
    e.currentTarget.x = targetField.x;
    e.currentTarget.y = targetField.y;

} else {
    e.currentTarget.x = start_x;
    e.currentTarget.y = start_y;
}
}

所以问题是,e.currentTarget.x从0开始。而不是从屏幕上的绝对位置开始。它总是一个相对值,所以如果我把它拖到targetField它的x和y就像-100,-40

如果我将它设置为targetField x和y,它会在屏幕的必杀技中消失。

在这种情况下,

targetField只是在舞台上绘制的矩形,x和y为160

如何将其定位到绝对x和y?

这是一个截图

enter image description here

所以top字段是targetField,它只是一个x16和y191的矩形。

底部的字段是可拖动字段,即e.currentTarget。但currentTarget.x始终为0.

1 个答案:

答案 0 :(得分:1)

修改

您的e.currentTarget将成为textContainer,您尚未设置x/y,因此它自然为0。

在创建容器时,移动容器似乎更有意义,而不是实际的文本字段,如下所示:

for (i = 0; i < vocabListItems.length; i++) { 
    var answerField:TextField = new TextField();   
    // Setting text to current answer
    answerField.text = vocabListItems[i];    
    answerField.width = 140;    
    answerField.height = 40;    
    //answerField.x = 60+ i*150;      //don't move the text field, move the container later
    //answerField.y = 410;  
    answerField.background = true;
    answerField.backgroundColor = 0xffffff;
    answerField.setTextFormat(myFormat);  
    answerField.selectable = false;
    answerField.type = TextFieldType.DYNAMIC

    // store the textfield in a container so drag and drop
    // will work
    var textContainer:Sprite = new Sprite();
    textContainer.x = 60+ i*150;
    textContainer.y = 410;

    textContainer.addChild(answerField);
    addChild(textContainer);
    referenceArray.push(textContainer);

    //Also, as an aside, there is no reason to loop through the array after this, just add the listeners here
    textContainer.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
    textContainer.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
}

结束修改

要将坐标从一个对象转换为另一个对象,您可以使用显示对象的localToGlobalglobalToLocal方法。

这样的事情:

var globalPoint:Point = targetField.localToGlobal(new Point());
var destinationLocalPoint:Point = e.currentTarget.parent.globalToLocal(globalPoint);

e.currentTarget.x = destinationLocalPoint.x;
e.currentTarget.y = destinationLocalPoint.y;

我在这里做的是首先获得targetField的全局坐标。所以它相对于targetField取一个点(0,0)并将其相对于stage进行翻译。

然后我创建一个新的Point对象,该对象接受全局coordnate集,并将其转换为e.currentTarget的父级。