循环中添加了多个精灵,但只有最后一个精灵可点击

时间:2014-05-24 12:02:40

标签: actionscript-3 actionscript

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

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

感谢您的时间!

我的代码:

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

编辑:

 function gotoscene(e: MouseEvent):void{




        var index:Number;

        index = newArray.indexOf(e.target);

         trace(index);


        blackBox.graphics.beginFill(0x000000);
        blackBox.graphics.drawRect( -1, -1, stage.width, stage.height);
        blackBox.alpha = 0.7;
        addChild(blackBox);








        var originalBitmap : BitmapData = loadedArray[index].bitmapData;
                var duplicate:Bitmap = new Bitmap(originalBitmap);
                duplicate.width = stage.width;


                 _contentHolder1.addChild(duplicate);



  // Use counter here to only add _contentHolder1 once


  //Assuming that `samedata` is a class member (I can't see the rest of your code)

      addChild(_contentHolder1);







           }

EDIT2:

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

}

2 个答案:

答案 0 :(得分:0)

您应该将image.addEventListener(MouseEvent.CLICK, gotoscene);语句移动到添加子sprite的循环中。完成后,监听器将被添加到所有精灵中,而不仅仅是当前存储在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);
        }

对于所有神圣的东西,学会缩进代码,这样你就能够直观地找到循环的开始和结束,看看某个语句是否在循环内。

答案 1 :(得分:0)

我在AS3工作了几年,这是一个奇怪的常见问题。我曾经用一个将事件添加到每个剪辑的函数来解决它:

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

      addSceneListener(image);
   }
}

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