I'm clearly missing something basic. In the example below, the
mouseDown handler is only reached when I click in the green Sprite,
not in the red shape, even though the listener is added to the root.
Is there some sort of mask in effect? Or do the Sprite and Shape
affect the size of the parent in different ways?

In the game that I'm working on, I actually have a UIComponent
subclass with children which are subclasses of Sprite (because they
themselves need a child text object). There, I was finding that the
hitTestPoint method wasn't succeeding on the Sprite, but if I made it
a Shape, it did succeed. That led me to create this test case.

Thanks in advance!

- Richard

package {
        import flash.display.Shape;
        import flash.display.Sprite;
        import flash.events.MouseEvent;
        import flash.geom.Point;

        public class hittest extends Sprite
        {
                private var _circleSprite:Sprite;
                private var _circleShape:Shape;

                public function hittest()
                {
                        super();
                        graphics.beginFill(0xFFFFFF);
                        graphics.drawRect(0,0,500,500);
                        graphics.endFill();
                        this.width = 500;
                        this.height = 500;
                        
                        _circleSprite = new Sprite();
                        _circleSprite.graphics.beginFill(0x00FF00);
                        _circleSprite.graphics.drawRect(0,0,100,100);
                        _circleSprite.graphics.endFill();
                        _circleSprite.x = 10;
                        _circleSprite.y = 10;
                        addChild(_circleSprite);

                        _circleShape = new Shape();
                        _circleShape.graphics.beginFill(0xFF0000);
                        _circleShape.graphics.drawRect(0,0,100,100);
                        _circleShape.graphics.endFill();
                        _circleShape.x = 200;
                        _circleShape.y = 200;
                        addChild(_circleShape);
                        
                        this.addEventListener(MouseEvent.MOUSE_DOWN, 
handleMouseDown);
                }
                
                private function handleMouseDown(event:MouseEvent):void{
                        trace("mousedown");
            var selectPoint:Point = localToGlobal(new
Point(event.localX, event.localY));
                        if (_circleSprite.hitTestPoint(selectPoint.x, 
selectPoint.y, false)) {
                                trace("hit sprite");
                        }
                        if (_circleShape.hitTestPoint(selectPoint.x, 
selectPoint.y, false)) {
                                trace("hit shape");
                        }
                }
        }
}

Reply via email to