热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何MouseEvent。单击数组中的多个影片剪辑-HowtoMouseEvent.ClickonMultipleMovieClipsinArray

IhaveanarrayofMovieClipsthatareaddedtothestagebyatimerevent.ThisMovieClipiscall

I have an array of Movie Clips that are added to the stage by a timer event. This Movie Clip is called mSquare.Now I wanted to set up a EventListener for the Movie clip so whenever the user clicks on the Movie Clip then it is destroyed, But I am having trouble setting this up since there is an Array of them Added to the stage. I keep getting this Error:

我有一组电影剪辑,通过计时器事件添加到舞台上。这个影片剪辑被称为mSquare。现在我想为影片剪辑设置一个EventListener,所以每当用户点击影片剪辑然后它就被销毁了,但是我在设置它时遇到了麻烦,因为它们有一个数组被添加到舞台。我一直收到这个错误:

Cannot access a property or method of a null object reference.

无法访问null对象引用的属性或方法。

Here is what I got so far:

这是我到目前为止所得到的:

mSquare.addEventListener(MouseEvent.CLICK, mIsDown);

Now in the mIsDown Function I know I have to go through the array so i tried to set up something like this:

现在在mIsDown函数中,我知道我必须通过数组,所以我尝试设置这样的东西:

private function mIsDown(e:MouseEvent):void 
{       
    for (var i:int = 0; i 

Also here is how my Square is added to the stage:

这里也是我的Square加入舞台的方式:

private function addSquare(e:TimerEvent):void 
{
     mSquare = new mcSquare();
     stage.addChildAt(mSquare, 0);
     mSquare.x = (stage.stageWidth / 2);
     mSquare.y = (stage.stageHeight / 2) + 450;

     aSquareArray.push(mSquare);
     // trace(aSquareArray.length);
}

Any help would be appreciated on what i need to do in order for the user to be able to MOUSE.click or MouseDown for the array of movie clips thanks!

任何帮助将不胜感激我需要做什么,以便用户能够MOUSE.click或MouseDown的电影剪辑数组谢谢!

**************** Here is how I am doing it now***************

****************以下是我现在的表现***************

stage.addEventListener(MouseEvent.MOUSE_DOWN, movieClipHandler); 

private function movieClipHandler(e:MouseEvent):void //test
    {
        mouseIsDown = true; 
        mSquare.addEventListener(MouseEvent.MOUSE_DOWN, squareIsBeingClicked);
    }

private function squareIsBeingClicked(e:MouseEvent):void 
    {

         var square:DisplayObject = e.target as DisplayObject; // HERE is your clicked square
         var i:int = aSquareArray.indexOf(square); // and HERE is your index in the array

         if (i <0) 
         {
            // the MC is out of the array
               trace("Clicked");
               checkSquareIsClicked();


         } else 
         {
            // the MC is in the array

         }


   }


  private function checkSquareIsClicked():void 
    {


        for (var i:int = 0; i 

1 个解决方案

#1


1  

The simplest answer is to use the target property of the event passed into the listener. This way you don't need to traverse the array in order to find what MC was clicked, you get it as target and go. To get the position of the targetted MC in the array, call indexOf function.

最简单的答案是使用传递给侦听器的事件的target属性。这样你就不需要遍历数组就可以找到被点击的MC,你可以将它作为目标然后去。要获取目标MC在数组中的位置,请调用indexOf函数。

private function mIsDown(e:MouseEvent):void 
{
    var mc:DisplayObject = e.target as DisplayObject; // HERE is your clicked square
    var i:int=aSquareArray.indexOf(mc); // and HERE is your index in the array
    if (i<0) {
        // the MC is out of the array
    } else {
        // the MC is in the array
    }
}

推荐阅读
author-avatar
ABC13517626247
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有