forked from: クリックしにくい
指定したピクセル毎に分割して、getColorBoundsRectで透明部分を含まない領域を検出して、それを元にgraphicsにdrawRectしているだけ。多分もっと上手いやり方はある!w
/**
* Copyright motikawa_rgm ( http://wonderfl.net/user/motikawa_rgm )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/7vI5
*/
// forked from bkzen's クリックしにくい
/**
* クリックしにくいオブジェクトをクリックしやすくするには
* どうしたらよいでしょうか?
*/
package
{
import flash.display.Sprite;
public class Test extends Sprite
{
public function Test()
{
for (var i: int = 0; i < 10; i++)
addChildAt(new TestChild(i), Math.random() < 0.5 ? numChildren : 0);
}
}
}
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.filters.GlowFilter;
import flash.geom.Rectangle;
import flash.display.DisplayObject;
import flash.geom.*;
import flash.display.*;
class TestChild extends Sprite
{
private var sp: Sprite;
private static var FILTER: GlowFilter = new GlowFilter(0xFF0000, 1, 6, 6, 3);
function TestChild(index: int)
{
// ここから
mouseChildren = false;
x = ((index % 5) + 1) * 25, y = ((index / 5 | 0) + 1) * 25 + 100;
// ここまでを変更不可
addChild(sp = makeGraphic(index));
_makeHitArea(sp);
//var r: Rectangle = sp.getBounds(null);
//var hitAreaRect: Sprite = new Sprite();
//var g: Graphics = hitAreaRect.graphics;
//g.beginFill(0xFFFFFF, 0.001);
//g.drawRect(r.x, r.y, r.width, r.height);
//addChild(hitAreaRect);
//hitArea = hitAreaRect;
buttonMode = true;
addEventListener(MouseEvent.MOUSE_OVER, onOver);
addEventListener(MouseEvent.MOUSE_OUT, onOut);
}
//追加 @motikawa
/*
*/
private function _makeHitArea(target:DisplayObject,stepPixel:int = 15):void
{
var i:int,
len:int,
br :Rectangle = target.getBounds(target),
basebmd:BitmapData = new BitmapData(width, height, true, 0x0),
bmd:BitmapData,
sh:Shape = new Shape(),
g:Graphics = sh.graphics,
mt:Matrix,
r:Rectangle;
basebmd.draw(target, new Matrix(1,0,0,1,-br.x,-br.y));
g.beginFill(0xff0000, 0);
for (i = 0, len = basebmd.width; i < len ; i+= stepPixel)
{
mt = new Matrix(1, 0, 0, 1, -i);
bmd = new BitmapData(stepPixel, basebmd.height, true, 0x0);
bmd.draw(basebmd, mt);
r = bmd.getColorBoundsRect(0xff000000, 0x00000000, false);
if (r) g.drawRect(i + r.x, r.y, r.width, r.height);
}
addChild(sh);
sh.x = br.x;
sh.y = br.y;
}
// ここは変更不可
private function makeGraphic(index: int): Sprite
{
var s: Sprite = new Sprite();
var g: Graphics = s.graphics;
g.beginFill(0x004080);
var n: int = Math.random() * 5 + 10;
// 適当に斜めに描画
for (var i:int = 0; i < n; i++)
{
var w: int = 70 * ((index % 2) ? (n - i) : i) / n;
var sy: int = i * 5 - n * 4;
g.drawRect(w, sy, w, 1);
}
return s;
}
private function onOut(e: MouseEvent): void { sp.filters = null; }
private function onOver(e: MouseEvent): void { sp.filters = [FILTER]; }
}