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

时间:2019-04-29 00:12:42

标签: actionscript-3 flash actionscript

我动态创建了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")
            }
        }
    }
}
}

感谢您的帮助, 谢谢

2 个答案:

答案 0 :(得分:2)

  

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

另一种选择(避免使用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 + " }" );
}

答案 1 :(得分:0)

您正在尝试将您的drawLabel类设为localToGlobal,该类位于0,0。类中的框位于100/200/300 / etc...。您应该尝试在框上使用localToGlobal,而不是Class。或者,您可以简单地移动课程并继续跟踪课程,以简化操作。

drawLabel:

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并将其移到其中,只需执行此操作即可。

drawLabel:

public var bc: Sprite;

主要:

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