AS3如何从显示对象获取锚点(枢轴,注册点)位置

时间:2013-07-08 23:59:49

标签: flash actionscript-3

我将获得相对于DisplayObject的锚点

例如:  enter image description here

// HOW GET THIS VALUES IN CODE
    Object height = 90px 
    Object width = 40px 

    anchor point X = 20px 
    anchor point Y = 72px 
//  


    real anchor point X =  anchor point X/Object width = 0.5 
    real anchor point y =  anchor point Y/Object height = 0.8

提前谢谢

enter image description here

import flash.events.Event;
import fl.motion.*;

run();

var frameID:uint;
var plistText:String;

function run()
{
    plistText = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n<plist version=\"1.0\">\n<dict>\n";
    processFrame(null);
    this.addEventListener(Event.ENTER_FRAME,processFrame);

}

function processFrame(e:Event)
{
    if (this.currentFrameLabel != null) {
        if (this.currentFrame != 1)
        plistText += "</array>\n";
        frameID = 0;
        plistText += "<key>"+this.currentFrameLabel+"</key>\n<array>\n";
    }
    plistText+="<dict>\n";
    plistText +="<key>FrameID</key>\n<string>"+frameID+"</string>\n"
    frameID++;
    for (var i:uint = 0; i < this.numChildren; i++)
    {
        var currentObject = this.getChildAt(i);
        plistText += "<key>"+currentObject.name+"</key>\n"
        plistText += "<dict>\n"
        plistText += "<key>positionX</key>\n<string>"+currentObject.x+"</string>\n";
        plistText += "<key>positionY</key>\n<string>"+currentObject.y+"</string>\n";
        plistText += "<key>scaleX</key>\n<string>"+currentObject.scaleX+"</string>\n";
        plistText += "<key>scaleY</key>\n<string>"+currentObject.scaleY+"</string>\n";
        plistText += "<key>rotation</key>\n<string>"+currentObject.rotation+"</string>\n";
        plistText += "<key>alpha</key>\n<string>"+currentObject.alpha+"</string>\n";
        plistText += "<key>visible</key>\n<string>"+currentObject.visible+"</string>\n";
        plistText += "</dict>\n"
    }
    plistText+="</dict>\n";
    if (this.currentFrame == totalFrames)
    {
        plistText += "</array>\n</dict>\n</plist>";
        trace(plistText)
        removeEventListener(Event.ENTER_FRAME, processFrame);
        this.stop();
    }
}

2 个答案:

答案 0 :(得分:5)

您可以通过getBounds()getRect()调用派生对象的锚点。为此,您的DisplayObject应该具有访问阶段的权限,否则getBounds()getRect()将返回伪造值(已测试)。实际上,锚点对应于对象在其自己的坐标空间中的(0,0)点,因此,为您的测试对象运行test.getRect(test)将返回(-20,-18,40,90) - 请注意Y坐标向下,而不是向上。然后,你得到那个矩形的左上角,反转它,瞧,对象的锚点相对于它的尺寸。

function getAnchorPoint(o:DisplayObject):Point {
    var onStage:Boolean;
    var p:DisplayObject=o.parent;
    onStage=(o.stage!=null);
    if (!onStage) stage.addChild(o);
    var res:Point=new Point();
    var rect:Rectangle=o.getRect(o);
    res.x=-1*rect.x;
    res.y=-1*rect.y;
    if (!onStage) {
        stage.removeChild(o);
        if (p) p.addChild(o);
    }
    return res;
}

答案 1 :(得分:2)

您不能。锚点未导出,仅存在于 * .fla 文件中。

但是,您可以实现自己的锚系统(就像许多库一样),但是无法访问原始fla中的那些。