0

我正在制作一个用正方形遮挡图像的工具。我想要发生的是有一个弹跳球,它击中方块并使它们消失。但是,removeChild 命令无法正常工作。我将其设置为使用空影片剪辑填充图像并为它们着色。但是,当我单击正方形时,我遇到了父母/孩子的问题。我一直遇到这个错误。“提供的 DisplayObject 必须是调用者的子对象。” 我想不出一种将 eventListeners 分配给每个单独方块的好方法。我确定这很明显。这是我的代码。先感谢您

编辑:如果我让它工作,它会删除正方形的所有实例,而不仅仅是我点击的那个。

这是我的代码

 var mc:MovieClip = bgIMG;
 var bd:BitmapData = new BitmapData(mc.width,mc.height);
 bd.draw(mc);

 var _img:Bitmap = new Bitmap(bd);
 var _imgNodes:Array = [];
 var _tiledImg:MovieClip = container_tiled_img;

 var pad:int = 0;
 var rows:int = 10;
 var cols:int = 10;
 var zero:Point = new Point();

 createImgNodeGrid(rows, cols, pad);
 pixelateNodes(_imgNodes);



 function removeMC(e:MouseEvent)
 {//removes the movie clip
 trace(e.currentTarget.x);
stage.removeChild(e.currentTarget.parent.parent);
 }


 function pixelateNodes(nodes:Array = null):void
 {
for each (var node:Bitmap in nodes)
{
    node.bitmapData.fillRect(node.bitmapData.rect, avgColor(node.bitmapData));

}
 }

 function avgColor(src:BitmapData):uint
 {
var A:Number = 0;
var R:Number = 0;
var G:Number = 0;
var B:Number = 0;

var idx:Number = 0;
var px:Number;

for (var x:int = 0; x < src.width; x++)
{
    for (var y:int = 0; y < src.height; y++)
    {
        px = src.getPixel32(x,y);

        A +=  px >> 24 & 0xFF;
        R +=  px >> 16 & 0xFF;
        G +=  px >> 8 & 0xFF;
        B +=  px & 0xFF;

        idx++;
    }
}

A /=  idx;
R /=  idx;
G /=  idx;
B /=  idx;

return A << 24 | R << 16 | G << 8 | B;
 }


 function createImgNodeGrid(rows:int = 1, cols:int = 1, pad:Number = 0):void
 {
var w:Number = _img.width / rows;
var h:Number = _img.height / cols;
var numNodes:int = rows * cols;

_imgNodes = [];

var nodeCount:int = 0;
for (var i:int = 0; i < rows; i++)
{
    for (var j:int = 0; j < cols; j++)
    {
        // get area of current image node
        var sourceRect:Rectangle = new Rectangle(i * w, j * h, w, h);

        // copy bitmap data of current image node
        var tempBd:BitmapData = new BitmapData(w,h,true);
        tempBd.copyPixels(_img.bitmapData, sourceRect, zero);

        // place image node bitmap data into sprite
        var imgNode:Bitmap = new Bitmap(tempBd);
        imgNode.x = i * (w + pad);
        imgNode.y = j * (h + pad);

        // store each image node
        //_imgNodes.push(imgNode);
        _imgNodes[nodeCount++] = imgNode;

        // add each image node to the stage
        _tiledImg.addChild(imgNode);
        _tiledImg.addEventListener(MouseEvent.CLICK,removeMC);
    }
}
 }
4

3 回答 3

0

如果我正确地遵循,您正在尝试删除 MouseClick 上的 imgNode。如果是这种情况,您应该将 removeMC 函数更改为:

function removeMC(e:MouseEvent)
{
    //removes the movie clip
    trace(e.target);
    _tiledImg.removeChild(event.currentTarget);
}

此外,您不应在 for 循环中添加侦听器,而应将其添加到 for 循环之外(因为您只是将侦听器添加到 _tiledImg 并且在 for 循环中不会改变)。

于 2012-10-31T17:05:05.087 回答
0

始终从正确的父母中删除孩子的一种简单方法是执行以下操作

function removeMC(e:MouseEvent)
{
    //removes the movie clip
    var target:Sprite = event.currentTarget as Sprite;
    target.parent.removeChild(target)
}
于 2012-10-31T17:58:44.580 回答
0

您遇到的问题是将事件添加到节点的父节点(_tiledImg)。

function createImgNodeGrid(rows:int = 1, cols:int = 1, pad:Number = 0):void
{
  var w:Number = _img.width / rows;
  var h:Number = _img.height / cols;
  var numNodes:int = rows * cols;

  _imgNodes = [];

  var nodeCount:int = 0;
  for (var i:int = 0; i < rows; i++)
  {
    for (var j:int = 0; j < cols; j++)
    {
        // get area of current image node
        var sourceRect:Rectangle = new Rectangle(i * w, j * h, w, h);

        // copy bitmap data of current image node
        var tempBd:BitmapData = new BitmapData(w,h,true);
        tempBd.copyPixels(_img.bitmapData, sourceRect, zero);

        // place image node bitmap data into sprite
        var imgNode:Bitmap = new Bitmap(tempBd);
        imgNode.x = i * (w + pad);
        imgNode.y = j * (h + pad);

        // store each image node
        //_imgNodes.push(imgNode);
        _imgNodes[nodeCount++] = imgNode;


        // you need a container since you can not attach event listeners to a BitMap
        var sprite:Sprite = new Sprite()
        sprite.mouseChildren = false;
        sprite.addEventListener(MouseEvent.CLICK,removeMC);
        sprite.addChild(imgNode);


        // add each image node to the stage
        _tiledImg.addChild(sprite);
       // _tiledImg.addEventListener(MouseEvent.CLICK,removeMC);
    }
  }
}

function removeMC(e:MouseEvent)
{
    var target:Sprite = event.currentTarget as Sprite;
    target.parent.removeChild(target)
}
于 2012-10-31T18:40:49.640 回答