Search code examples
actionscript-3flashactionscript

How to fix localToGlobal when not providing global coordinates?


I created dynamically 5 boxes that are stacked next to each other on the stage. The first one is located at (0,0), next one at (100,0), the third one on (200,0) and so on.

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.

Here is what I tried:
I tried attaching an event listener to each box so that I can take the local coordinates and get their global equivalents but that failed.

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

  }

UPDATE: this is what's in 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")
            }
        }
    }
}
}

Any help is appreciated, thank you


Solution

  • You are trying to localToGlobal your drawLabel class, which is at 0,0. The box inside the class, is at 100/200/300/etc... You should be trying to localToGlobal on the box, not the Class. Or you can simply move the class and continue to track the class instead to simplify things.

    drawLabel:

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

    Main:

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

    If for any reason you need drawLabel to be at 0,0 and move the box inside it instead, simply do this.

    drawLabel:

    public var bc: Sprite;
    

    Main:

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