不提供全局坐标时如何修复 localToGlobal?

How to fix localToGlobal when not providing global coordinates?

我在舞台上动态创建了 5 个并排堆叠的盒子。第一个位于 (0,0),下一个位于 (100,0),第三个位于 (200,0),依此类推。

我遇到的问题 是每次我单击每个单独的框时,它都会跟踪它位于 (0,0)。我尝试使用 localToGlobal 但无济于事。

这是我试过的
我尝试将一个事件侦听器附加到每个框,以便我可以获取本地坐标并获取它们的全局等效项,但失败了。

import com.mahmoud.util.drawLabel;
import flash.geom.Point;
import flash.events.MouseEvent;

var d: drawLabel;
var global: Point;



for (var i: uint = 0; i < 5; i++) {
    d = new drawLabel();
    d.init(i, "", "", 100, 75, i * 101, 0);
    d.addEventListener(MouseEvent.CLICK, check);
    addChild(d);

 }


 function check(e: MouseEvent): void {
    global = new Point(drawLabel(e.currentTarget).x,drawLabel(e.currentTarget).y)
    trace(drawLabel(e.currentTarget).localToGlobal(global));

  }

更新:这是 drawLabel 中的内容

package com.mahmoud.util {

import flash.display.Sprite;
import flash.display.Shape;
import flash.display.GradientType;
import flash.geom.Matrix;
import flash.text.TextField;
import flash.events.MouseEvent;
import flash.text.TextFormat;
import com.mahmoud.util.xScroll;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.core.DisplayObjectLoader;
import com.greensock.loading.core.LoaderItem;
import com.greensock.loading.ImageLoader;
import com.greensock.TweenLite;
import flash.display.Bitmap;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.ProgressEvent;


public class drawLabel extends Sprite {

    private var bc: Sprite;
    private var label_txt: TextField;
    private var theSentence: String;
    private var loader: ImageLoader;
    private var f: TextFormat = new TextFormat("Arial", 15, null, null, null, null, null, null);

    public var idArray: Array = new Array("default value");

    public function drawLabel() {}

    public function init(theSentence, whatFmt, iconOf, labelWidth: Number = 100, labelHeight: Number = 50, label_x: Number = 0, label_y: Number = 0, colorOf: uint = 0xEFEFEF, alphaOf: Number = 1, inputType: String = "dynamic", inputSelectable: Boolean = false, idArray: Array = null, factorX: Number = 100, factorY: Number = 75, iconOfScale: String = "", backUpImage: String = "", imageRatio: uint = 2) {
        bc = new Sprite;
        bc.tabEnabled = true
        label_txt = new TextField;
        bc.graphics.beginFill(colorOf, alphaOf);
        bc.graphics.drawRect(label_x, label_y, labelWidth, labelHeight);
        bc.graphics.endFill();
        bc.mouseChildren = true;

        bc.buttonMode = true;
        //center text within the box
        label_txt.width = factorX * (labelWidth / 100);
        label_txt.height = factorY * (labelHeight / 100);
        label_txt.x = (labelWidth / 2 - ((factorX * (labelWidth / 100)) / 2)) + label_x
        label_txt.y = (labelHeight / 2 - ((factorY * (labelHeight / 100)) / 2)) + label_y
        label_txt.multiline = true;
        label_txt.wordWrap = true;
        label_txt.border = true;
        label_txt.type = inputType
        label_txt.selectable = inputSelectable;
        label_txt.text = theSentence;
        label_txt.embedFonts = false;
        label_txt.tabIndex = 0

        if (whatFmt == "") {
            whatFmt = f
        }
        label_txt.setTextFormat(whatFmt);
        bc.addChild(label_txt);


        addChild(bc);

        //load the image and attach it to bc. create an ImageLoader using greensock
        //the image is optional, so check the text to see if it is not null
        if (iconOf !== "") {
            if (iconOfScale == "") {
                iconOfScale = "proportionalInside"
            }
            loader = new ImageLoader(iconOf, {
                name: "icon_",
                container: this,
                x: label_x + labelWidth - (33),
                y: label_y + labelHeight / 2,
                width: labelWidth / imageRatio,
                height: labelHeight / imageRatio,
                scaleMode: iconOfScale,
                centerRegistration: true,
                alternateURL: backUpImage,
                onComplete: onImageLoad,
                onError: fileNotFound
            });

            //begin loading
            loader.load();

            //when the image loads, fade it in from alpha:0 using TweenLite
            function onImageLoad(event: LoaderEvent): void {
                TweenLite.from(event.target.content, 1, {
                    alpha: 0
                });
            }
            function fileNotFound(event: LoaderEvent): void {
                trace("image missing")
            }
        }
    }
}
}

感谢任何帮助, 谢谢

您正在尝试将位于 0,0 的 drawLabel class 本地化。 class 内的框位于 100/200/300/etc... 您应该尝试在框上使用 localToGlobal,而不是 Class。或者您可以简单地移动 class 并继续跟踪 class 而不是简化事情。

绘图标签:

bc.graphics.drawRect(0, 0, labelWidth, labelHeight);
this.x = label_x;
this.y = label_y;

主要:

trace(e.currentTarget.x + ', ' + e.currentTarget.y);

如果出于任何原因您需要将 drawLabel 设置为 0,0 并将框移到其中,只需执行此操作即可。

绘图标签:

public var bc: Sprite;

主要:

trace(e.currentTarget.bc.x + ', ' + e.currentTarget.bc.y);

The problem I am having is that every time I click on each individual box, it traces that it is at (0,0). I tried using localToGlobal but to no avail.

另一种选择(避免 localToGlobal)是只使用 target.xtarget.y
这将允许您定位每个单独的框。

示例代码:

for (var i: uint = 0; i < 5; i++) 
{
    d = new drawLabel();
    d.init(i, "", "", 100, 75, i * 100, 0);
    d.name = "label_" + i; //# give each one a name if you want to...
    d.addEventListener(MouseEvent.CLICK, check);
    addChild(d);
 }

然后像这样检查...(注意:使用 currentTarget 作为名称,然后使用 target 作为 x/y 作为位置)。

function check(e: MouseEvent): void 
{
    trace( " " + "\n" + "# Got checking click" + "\n" + "-----------------------");
    trace( "Name : " + e.currentTarget.name);
    trace( "Pos  : " + "{ x: " + e.target.x + ", y: " + e.target.y + " }" );
}