如何从嵌套容器中的显示对象和阶段或spark应用程序之类的根容器中获取距离?
例如:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
applicationComplete="applicationCompleteHandler(event)"
>
<fx:Script>
<![CDATA[
protected function applicationCompleteHandler(event:FlexEvent):void {
var point:Point = new Point(nestedButton.x, nestedButton.y);
var localToGlobalPoint:Point = this.localToGlobal(point);
var globalToLocalPoint:Point = this.globalToLocal(point);
var localToContentPoint:Point = this.localToContent(point);
trace("Button x: " + nestedButton.x);
trace("Button layout bounds x: " + nestedButton.getLayoutBoundsX());
trace("Button to main container localToGlobal x: " + localToGlobalPoint.x);
trace("Button to main container globalToLocal x: " + globalToLocalPoint.x);
trace("Button to main container localToContent x: " + localToContentPoint.x);
}
]]>
</fx:Script>
<mx:Canvas id="mainCanvas" top="10" left="10">
<mx:Canvas top="10" left="10">
<s:Group x="10" y="10">
<s:Button id="sparkButton2" label="spark button" x=10 y="10"/>
</s:Group>
</mx:Canvas>
</mx:Canvas>
</s:WindowedApplication>
如果按钮嵌套在3个容器中,并且每个容器距离前一个边缘10
像素,并且该按钮距其边缘10
个像素,则x
值应为40
x
。这不是我得到的价值。每次调用中的0
值都会追溯到var mySingleton = (function(){
var instance;
function init(){
var privateRandomNumber = Math.random();
return {
getRandomNumber : function(){
return privateRandomNumber;
}
}
}
return {
getInstance: function(){
if(!instance) {
instance = init();
}
return instance;
}
}
})();
var singleA = mySingleton.getInstance();
var singleB = mySingleton.getInstance();
console.log(singleA.getRandomNumber() === singleB.getRandomNumber());
。
答案 0 :(得分:1)
似乎没有Flash API。所以看起来你拿两个显示对象并在全局范围内获得它们的位置然后减去差异。
这是一个功能,它似乎正在运作:
public function distanceBetweenDisplayObjects(source:Object, target:Object):Point {
var sourceRelativePoint:Point;
var sourceLocalToGlobalPoint:Point;
var containerLocalToGlobalPoint:Point;
var x:Number;
var y:Number;
var zeroPoint:Point = new Point(0, 0);
sourceLocalToGlobalPoint = source.localToGlobal(zeroPoint);
containerLocalToGlobalPoint = target.localToGlobal(zeroPoint);
var sourceDifference:Point = sourceLocalToGlobalPoint.subtract(containerLocalToGlobalPoint);
var containerDifference:Point = containerLocalToGlobalPoint.subtract(sourceLocalToGlobalPoint);
return sourceDifference;
}
用法:
var sourceDifference:Point = distanceBetweenDisplayObjects(nestedButton, mainContainer);