Flex collision testing with hitTestObject

徘徊边缘 提交于 2019-12-12 04:24:14

问题


I'm trying to test clipping on two canvases. Both canvases are 100px wide. They're 20px apart. I've placed a label inside one and made it 200px wide. Scroll bars will show up on the canvas. When I don't have the label inside and use hitTestObject it returns false. When I place the label inside it returns true. Is there any way to alter the canvas with the label inside so that it doesn't expand to the width of the label?

<?xml version="1.0" encoding="utf-8"?>

        private function init() : void {
            var hitBox:Sprite = new Sprite;
            hitBox.graphics.drawRect(box1.x, box1.y, 100, 100);

            box1.hitArea = hitBox;
            box1.mouseEnabled = false;

            trace('box hit area: ' + box1.getBounds(box1));
            trace('hitbox: ' + hitBox);
            trace('box hit test: ' + box1.hitTestObject(box2));
        }
    ]]>
</mx:Script>
<mx:Canvas id="box1" x="10" y="10" width="100" height="100" backgroundColor="#FFFFFF">
    <mx:Label text="This is a test" width="200" />
</mx:Canvas>
<mx:Canvas id="box2" x="120" y="10" width="100" height="100" backgroundColor="#FFFFFF" />


回答1:


Unfortunately, it doesn't look like you can accomplish what you want with hitTestObject. I played around with setting the clipContent and horizontalScrollPolicy properties on your canvases, to no use. What I think is happening is that hitTestObject considers your canvas to be as wide as the longest child component, regardless of any clip masks or scroll bars.

Are you forced to use hitTestObject? If not, I'd suggest writing your own collision detection function along the lines of:

public static function componentsCollide( obj1:DisplayObject, obj2:DisplayObject ):Boolean {
    var bounds1:Rectangle = new Rectangle( obj1.x, obj1.y, obj1.width, obj1.height );
    var bounds2:Rectangle = new Rectangle( obj2.x, obj2.y, obj2.width, obj2.height );

    return bounds1.intersects( bounds2 );
}


来源:https://stackoverflow.com/questions/1276108/flex-collision-testing-with-hittestobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!