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

发布于 2024-12-11 13:35:05 字数 997 浏览 0 评论 0原文

使用下面的代码,我创建了一些 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){

  }

With the code below I created some imgMcA and some imgMcB then I loaded images into imgMcA ones. ImgMcBs have no image at that moment. So if one of imgMcA is clicked the image should be transferred to one of the empty imgMcBs (may be randomly) and if imgmcB is clicked later the image should move back to its imgMcA back. I could not find out how I can accomplish this.

Thanks in advance

 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){

  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飞烟轻若梦 2024-12-18 13:35:05

您可以链接 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 包含文本,请将布尔值设置为 true。

可能值得查看类,或者您可以使用对象作为 MovieClips、TextField 和布尔值的容器,尽管类会给您带来更大的灵活性...

使用类,您不必按顺序迭代找到什么做什么。您的 Click 监听器看起来像这样:

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

You could linked mcA & mcB by adding them to the same parent.

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); 

    }
}

The rest is not too complicated to achieve. You will need to use a Boolean and add a TextField. If the TextField contains text, set the Boolean to true.

It may be worth looking at Classes or you could use an Object as a container for the MovieClips, the TextField and the Boolean, although a Class will give you more flexibility...

With a Class, you wouldn't have to iterate in order to find what does what. Your Click listener would look something like this:

private function mouseClickHandler(event:MouseEvent)
{
   receiver.addChild( image );
   if( hasText)
    imageReturn(); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文