as3将图像从一个mc移动到另一个mc

时间:2011-10-23 06:24:15

标签: actionscript-3

使用下面的代码我创建了一些imgMcA和一些imgMcB然后我将图像加载到imgMcA中。 ImgMcBs当时没有图像。因此,如果单击imgMcA之一,则应将图像传输到其中一个空imgMcB(可能是随机的),如果稍后单击imgmcB,则图像应移回其imgMcA。我无法找到如何实现这一目标。

提前致谢

 function imageList(mcname, img, index){

    var imgMcA:MovieClip=new MovieClip();
    imgMcA.graphics.beginFill(0x000000);
    imgMcA.graphics.drawRect(0,0,imgWidth,imgHeight);
    imgMcA.graphics.endFill();
    imgMcA.name=lemma;
    imgMcA.addEventListener(MouseEvent.CLICK, moveImage);


    var imgMcB:MovieClip=new MovieClip();
    imgMcB.graphics.beginFill(0x000000);
    imgMcB.graphics.drawRect(0,0,imgWidth,imgHeight);
    imgMcB.graphics.endFill();
    imgMcB.name=index;
    addChild(imgMcB);


    var imgLoader:Loader = new Loader();
    imgLoader.load(new URLRequest(img));
    imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, changeProperties);
    imgLoader.mouseEnabled=false;
    imgMcA.addChild(imgLoader);

  }

  function moveImage(evnt:MouseEvent){

  }

1 个答案:

答案 0 :(得分:0)

你可以联系mcA& mcB将它们添加到同一个父级。

function createBlock():void
{
   var imgLoader:Loader = new Loader();
   //add the loading code here...

   var mcA:Sprite = new Sprite();
   var mcB:Sprite = new Sprite();
   // add Click event listeners for both Mcs here...

   var parent:Sprite = new Sprite();

   //add Children
   parent.addChild(mcA);
   parent.addChild(mcB);
   addChild(parent);
}

function mouseClickHandler(event:MouseEvent)
{
    var dispatcher:Sprite = event.currentTarget as Sprite;

    //if you added a Loader to it
    if( dispatcher.numChildren > 0)
    {
        //retrieve the image
        var img:Loader = dispatcher.getChildAt(0);

        //identify the parent
        var parent:Sprite = dispatcher.parent;

        var index:int = parent.getChildIndex(dispatcher);

        //identify the receiving MC
        //of course this only works with two children!!!
        if(index > 0)
            var receiver:Sprite = parent.getChildAt(0) as Sprite;
        else
            receiver = parent.getChildAt(1) as Sprite;

        //add the image to the other MC 
        receiver.addChild(img); 

    }
}

其余的并不太复杂。您将需要使用布尔值并添加TextField。如果TextField包含文本,请将Boolean设置为true。

可能值得查看Classes,或者您可以使用Object作为MovieClip,TextField和Boolean的容器,尽管Class会为您提供更大的灵活性......

使用Class,您不必迭代以找到什么做什么。您的Click侦听器看起来像这样:

private function mouseClickHandler(event:MouseEvent)
{
   receiver.addChild( image );
   if( hasText)
    imageReturn(); 
}