In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: forked from: getObjectsUnderPoint

Mainの子供が_sp1
_sp1の子供が_sp2
_sp2にたくさん四角が配置される

_sp1のx,y座標が両方とも+20していて、それのせいであたり判定がおかしくなります
ちゃんと動作させるためには、x,y座標を移動させないか
関数を自作するしかないのでしょうか?
/**
 * Copyright meix ( http://wonderfl.net/user/meix )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/zKTc
 */

// forked from 9re's forked from: getObjectsUnderPoint
// forked from cpu_t's getObjectsUnderPoint
/*

Mainの子供が_sp1
_sp1の子供が_sp2
_sp2にたくさん四角が配置される

_sp1のx,y座標が両方とも+20していて、それのせいであたり判定がおかしくなります
ちゃんと動作させるためには、x,y座標を移動させないか
関数を自作するしかないのでしょうか?

*/
package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Point;
    import flash.text.TextField;
    
    public class Main extends Sprite 
    {
        
        public function Main():void 
        {
            _tf = new TextField();
            stage.addChild(_tf);
            _tf.background = true;
            _tf.autoSize = "left";
            
            _sp1 = new Sprite();
            _sp1.x = 20;
            _sp1.y = 20;
            _sp1.mouseChildren = false;
            
            _sp2 = new Sprite();
            _sp2.mouseChildren = false;
            for (var i:int = 0; i < 20; i++)
            {
                var rect:Sprite = new Sprite();
                rect.graphics.beginFill(Math.random() * 0xFFFFFF);
                rect.graphics.drawRect(0, 0, 40, 40);
                rect.graphics.endFill();
                rect.x = Math.random() * 200;
                rect.y = Math.random() * 200;
                rect.rotation = Math.random() * 360;
                _sp2.addChild(rect);
            }
            
            _sp1.addChild(_sp2);
            addChild(_sp1);
            
            addEventListener(Event.ENTER_FRAME, loop);
        }
        
        private var _tf:TextField;
        
        private var _sp1:Sprite;
        private var _sp2:Sprite;
        
        private function loop(e:Event):void 
        {
            var p:Point = new Point(mouseX, mouseY);
            // i guess _sp2.getObjectsUnderPoint(p) should work, but actually not,
            // while _sp1.getObjectsUnderPoint(p) is ok.
            // the coordinate for getObjectsUnderPoint should in the Stage coordinate space.
            var list:Array = getObjectsUnderPoint(p);
            
            _tf.text = p + "\n" + list;
        }
        
    }
    
}