Flash 精灵的透明部分响应鼠标事件
我正在用FlashCS5/as3制作一个拼图游戏,可以自定义拼图形状。 基本上,用户可以绘制拼图的基本突出部分。 然后我创建了一个黑白拼图块,上面有这样一个自定义函数Flash 精灵的透明部分响应鼠标事件,flash,actionscript-3,mouseevent,sprite,transparent,Flash,Actionscript 3,Mouseevent,Sprite,Transparent,我正在用FlashCS5/as3制作一个拼图游戏,可以自定义拼图形状。 基本上,用户可以绘制拼图的基本突出部分。 然后我创建了一个黑白拼图块,上面有这样一个自定义函数 var piece:PuzzlePiece= new PuzzlePiece(true,true,false,false); PuzzlePiece是一个扩展Sprite的类。这四个参数对应于拼图的四个面(上、下、左、右)。如果这个论点是正确的,那就意味着这个突出物应该从拼图的那一边伸出来。如果为假,则应在该侧有一个孔,以便适合
var piece:PuzzlePiece= new PuzzlePiece(true,true,false,false);
PuzzlePiece是一个扩展Sprite的类。这四个参数对应于拼图的四个面(上、下、左、右)。如果这个论点是正确的,那就意味着这个突出物应该从拼图的那一边伸出来。如果为假,则应在该侧有一个孔,以便适合突出部分。我首先连接突出的突出部分,然后翻转插入的突出部分,将它们连接到新的精灵(in_部分),并使用以下功能反转它们:
in_part=invertSprite(in_part,in_part.widht,in_part.height);
function invertSprite(source:Sprite, width: int, height: int) {
var child:Shape = new Shape();
child.graphics.beginFill(0xFFFFFF);
child.graphics.lineStyle();
child.graphics.drawRect(0, 0, width,height);
child.graphics.endFill();
var cont:Sprite = new Sprite();
cont.addChild(child);
cont.addChild(source);
source.blendMode = BlendMode.ERASE;
cont.blendMode = BlendMode.LAYER;
return cont;
}
我把这两部分结合起来,结果是黑白拼图
然后,我想将该片段作为遮罩应用于加载图像的一部分
puzzleImg = Bitmap(loader.content).bitmapData; // this is the .jpg image
var width = piece.width; // piece is black and white Sprite in the shape of the puzzle
var height = piece.height;
var puzzlePc:BitmapData = new BitmapData(width,height,true,0);
puzzlePc.draw(puzzleImg);
disp = new Bitmap(puzzlePc);
tmp.addChild(disp); //tmp is the main, holding Sprite
addChild(piece);
disp.mask = piece;
disp.cacheAsBitmap = true;
piece.cacheAsBitmap = true;
tmp.mouseChildren = false;
这一切都很好,除了拼图块上的洞(其他拼图块应该放在其中的透明部分)对鼠标点击做出响应,这显然不符合我的意图
我花了几天时间试图解决这个问题,但我真的不明白为什么会发生这种情况
有什么想法吗?在鼠标处理程序中,您可以检查鼠标单击点下像素的alpha值。如果该值小于full alpha,则取消任何功能
protected function onMouseDown( event:MouseEvent ):void
{
var pixelValue:uint = _bitmap.bitmapData.getPixel32( event.localX, event.localY );
var alphaValue:uint = pixelValue >> 24 & 0xFF;
trace( 'alphaValue: ' + alphaValue );
trace( 'pixelValue: ' + pixelValue );
if( alphaValue < 255 ) {
trace( 'clicked area is not opaque' );
} else {
trace( 'clicked area is opaque' );
}
}
mousedown上受保护的函数(事件:MouseEvent):无效
{
var pixelValue:uint=_bitmap.bitmapData.getPixel32(event.localX,event.localY);
var alphaValue:uint=pixelValue>>24&0xFF;
跟踪('alphaValue:'+alphaValue);
轨迹('pixelValue:'+pixelValue);
如果(字母值<255){
跟踪(“单击区域不不透明”);
}否则{
跟踪(“单击区域是不透明的”);
}
}
我相信您可以使用BitmapData.hitTest函数来实现这一点。使用此函数,您可以根据位图或位图测试点。组有一个属性mouseEnabledWhenTransparent,请将其设置为true 如果使用FXG,请将它们包装在一个组中
<s:Group mouseEnabledWhenTransparent="true">
<someRandomComponentWithTransparency/>
</s:Group>