Actionscript 3 循环中添加了多个精灵,但只能单击最后一个精灵

Actionscript 3 循环中添加了多个精灵,但只能单击最后一个精灵,actionscript-3,actionscript,Actionscript 3,Actionscript,我在一个循环中将多个位图图像添加到精灵中(每个图像添加到一个精灵中),然后将所有精灵添加到1_contentHolder(精灵),然后将其添加到视口中 问题是,在循环中添加的多个精灵,所有显示都没有问题,但只有最后添加的精灵是可单击的。在可单击之前,未添加任何精灵。想知道问题是什么,它们没有重叠,当我将鼠标悬停在所有精灵的顶部时,它会变成鼠标点击器,但它就是不点击 谢谢你的时间 我的代码: function onImageLoaded(e:Event):void {

我在一个循环中将多个位图图像添加到精灵中(每个图像添加到一个精灵中),然后将所有精灵添加到1_contentHolder(精灵),然后将其添加到视口中

问题是,在循环中添加的多个精灵,所有显示都没有问题,但只有最后添加的精灵是可单击的。在可单击之前,未添加任何精灵。想知道问题是什么,它们没有重叠,当我将鼠标悬停在所有精灵的顶部时,它会变成鼠标点击器,但它就是不点击

谢谢你的时间

我的代码:

function onImageLoaded(e:Event):void {

                loadedArray.push(e.target.content as Bitmap);

                for(var i:int = 0; i < loadedArray.length; i++){
                var currentY1:int = 200;
                var image: Sprite= new Sprite;

                e.currentTarget.loader.content.height =200;
                e.currentTarget.loader.content.y += currentY1;


                 image.mouseChildren = true;    // ignore children mouseEvents
                 image.mouseEnabled = true;      // enable mouse on the object - normally set to true by default
                 image.useHandCursor = true;     // add hand cursor on mouse over
                 image.buttonMode = true; 

                  image.addChild(loadedArray[i]);

                    _contentHolder.addChild(image);
                }

            newArray.push(image);
            var viewport:Viewport = new Viewport();

            viewport.y = 0;

            viewport.addChild(_contentHolder);


            var scroller:TouchScroller = new TouchScroller();
            scroller.width = 300;
            scroller.height = 265;
            scroller.x = 10;
            scroller.y = 100;
            scroller.viewport = viewport;
            addChild(scroller);


             image.addEventListener(MouseEvent.CLICK, gotoscene);

                    }
loadImage();
编辑2:

     private var image:Array = new Array;


    //In the For loop 

    image[i] = new Sprite();
           image[i].addChild(loadedArray[i]);
                    image[i].addEventListener(MouseEvent.CLICK, gotoscene);


function gotoscene(e:MouseEvent):void{


 index = image.indexOf(e.target);
         trace(index);

}

您应该移动
image.addEventListener(MouseEvent.CLICK,gotoscene)语句添加到添加子精灵的循环中。一旦您这样做,监听器将被添加到所有精灵中,而不仅仅是当前存储在
image
变量中的最后一个精灵,并且是唯一响应您单击的精灵

        for(var i:int = 0; i < loadedArray.length; i++){
            var currentY1:int = 200;
            var image: Sprite= new Sprite;
            e.currentTarget.loader.content.height =200;
            e.currentTarget.loader.content.y += currentY1;


            image.mouseChildren = true;    // ignore children mouseEvents
            image.mouseEnabled = true;      // enable mouse on the object - normally set to true by default
            image.useHandCursor = true;     // add hand cursor on mouse over
            image.buttonMode = true; 
            image.addEventListener(MouseEvent.CLICK, gotoscene); // <-- THIS
            image.addChild(loadedArray[i]);

            _contentHolder.addChild(image);
        }
for(变量i:int=0;iimage.addEventListener(MouseEvent.CLICK,gotoscene);//我在AS3中工作了几年,这是一个奇怪的问题,也是一个常见的问题。我用一个函数将事件添加到每个剪辑中来解决它:

function someFunction():void {
   for (...) {
      var image:Sprite = new Sprite();

      addSceneListener(image);
   }
}

function addSceneListener(mc:Sprite):void {
   mc.addEventListener(MouseEvent.CLICK, gogoscene);
}

也许这会给你一些想法:我看了那个链接,但我仍然不太清楚为什么我的精灵不会点击tho。因为什么原因只有最后一个精灵点击,而没有其他精灵点击我不能做到,当我移动循环中的image.addEventListener行时,然后在编辑部分,从EventListen中转到了gotoscenener,我有一个错误#1010:一个术语未定义,前两个图像没有属性,最后一个图像仍然可以(可单击),但现在前两个图像不会单击并给出该错误,索引变成了-1。在这一行上,var originalBitmap:BitmapData=loadedArray[index].bitmapData;我尝试将索引从var originalBitmap:bitmapData=loadedArray[index].bitmapData;切换到[0],正如您所说,所有精灵都可以单击。但不确定为什么在我尝试查找索引时,前2变为-1,最后一个精灵跟踪索引正确。
function someFunction():void {
   for (...) {
      var image:Sprite = new Sprite();

      addSceneListener(image);
   }
}

function addSceneListener(mc:Sprite):void {
   mc.addEventListener(MouseEvent.CLICK, gogoscene);
}