AS3无法理解矩形交点

cro*_*y88 1 air flash intersection actionscript-3

我正在尝试使用Rectangle.intersection为我提供2个重叠形状的交叉区域的矩形,但没有取得多大成功.

下面的代码只是两个相同大小的形状.最顶级的形状是可拖动的.当拖动停止时,我执行bottomRect.intersection(topRect)调用,但这总是返回rect的完整大小,而不是交集大小.

(可以将代码复制并粘贴到第一帧的新ActionScript文件中并运行.)

有谁知道我哪里出错了?

谢谢

import flash.geom.Rectangle;
import flash.display.Sprite;

var bottomSprite:Sprite = new Sprite();
addChild(bottomSprite);

var bottomRect:Shape = new Shape;
bottomRect.graphics.beginFill(0xFF0000);
bottomRect.graphics.drawRect(0, 0, 320,480);
bottomRect.graphics.endFill();
bottomSprite.addChild(bottomRect);

var topSprite:Sprite = new Sprite();
addChild(topSprite);

var topRect:Shape = new Shape;
topRect.graphics.beginFill(0x000033);
topRect.graphics.drawRect(0, 0, 320,480);
topRect.graphics.endFill();
topSprite.addChild(topRect);

var bottomBoundsRect:Rectangle = stage.getBounds(bottomSprite);
trace("START: bottomBoundsRect ", bottomBoundsRect);

var topBoundsRect:Rectangle = stage.getBounds(topSprite);
trace("START: topBoundsRect ", topBoundsRect);

topSprite.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
topSprite.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

function mouseDownHandler(evt:MouseEvent):void{

topSprite.startDrag();

}


function mouseUpHandler(evt:MouseEvent):void{

topSprite.stopDrag();

topBoundsRect = stage.getBounds(topSprite);

trace("INTERSECTION RECT", bottomBoundsRect.intersection(topBoundsRect));

}
Run Code Online (Sandbox Code Playgroud)

And*_*pov 6

问题是由于toIntersect您传递的错误属性:

topBoundsRect = stage.getBounds(topSprite);
trace("INTERSECTION RECT", bottomBoundsRect.intersection(topBoundsRect));
Run Code Online (Sandbox Code Playgroud)

你所做的就是让你获得舞台topSprite重复.如果你跟踪它,它会给你这样的东西:

(x=-62, y=-41, w=382, h=521)
Run Code Online (Sandbox Code Playgroud)

所以你有一个从0,0开始并且有更大宽度/高度的边界,因为你移动了topSprite - 这里我把它向右移动了62个像素(382 - 320 [宽度]),向下移动了41个像素(521个) - 480 [身高]).

这个矩形与底部矩形的实际交点正好是底部矩形的大小.

你应该做的是类似的事情:

// somehow get the rectangle of the bottom sprite
var br:Rectangle = new Rectangle(bottomSprite.x, bottomSprite.y, bottomSprite.width, bottomSprite.height);
// somehow get the rectangle of the top sprite
var tr:Rectangle = new Rectangle(topSprite.x, topSprite.y, topSprite.width, topSprite.height);
trace (br.intersection(tr)); // intersect them
Run Code Online (Sandbox Code Playgroud)

获得界限的方法很少,但这也很有效并且显示了这个想法.

希望有所帮助!:)