如果仅单击五个影片剪辑(不是更多),如何使影片剪辑可见

时间:2013-07-23 20:53:38

标签: actionscript-3

我在舞台上有25个电影剪辑,它们都可以点击并着色。我希望一个名为text_mc的影片剪辑变得可见,如果只有5个特定按钮被点击并着色 - 而不是更多。如果用户选择了超过这五个影片剪辑(甚至认为包含了5个影片剪辑),则名为text_mc的影片剪辑应保持不可见。我不能做最后一部分:如果点击了超过这5个特定的电影剪辑,那么text_mc应该保持不可见。你能帮我么?这是我的代码

  stop();

  import flash.display.MovieClip;



 var sximata:MovieClip = square1;
 import flash.display.MovieClip;
 import flash.events.MouseEvent;
 import flash.geom.ColorTransform;



 text_mc.visible=false;


  square1.addEventListener(MouseEvent.CLICK, onsquare1);
  function onsquare1(e:MouseEvent):void {
sximata = square1;
  }


  square2.addEventListener(MouseEvent.CLICK, onsquare2);
  function onsquare2(e:MouseEvent):void {
sximata = square2;
  }

  square3.addEventListener(MouseEvent.CLICK, onsquare3);
  function onsquare3(e:MouseEvent):void {
sximata = square3;
  }


  square4.addEventListener(MouseEvent.CLICK, onsquare4);
  function onsquare4(e:MouseEvent):void {
sximata = square4;
   }


  square5.addEventListener(MouseEvent.CLICK, onsquare5);
  function onsquare5(e:MouseEvent):void {
sximata = square5;
   }

   square6.addEventListener(MouseEvent.CLICK, onsquare6);
   function onsquare6(e:MouseEvent):void {
sximata = square6;
      }

    square7.addEventListener(MouseEvent.CLICK, onsquare7);
    function onsquare7(e:MouseEvent):void {
sximata = square7;
     }


    square8.addEventListener(MouseEvent.CLICK, onsquare8);
     function onsquare8(e:MouseEvent):void {
sximata = square8;
square8Clicked = true;
checkButtons();


   }

   square9.addEventListener(MouseEvent.CLICK, onsquare9);
   function onsquare9(e:MouseEvent):void {
sximata = square9;
square9Clicked = true;
checkButtons();
   }


    square10.addEventListener(MouseEvent.CLICK, onsquare10);
    function onsquare10(e:MouseEvent):void {
sximata = square10;
square10Clicked = true;
checkButtons();
     }


     square11.addEventListener(MouseEvent.CLICK, onsquare11);
     function onsquare11(e:MouseEvent):void {
sximata = square11;
    }


       square12.addEventListener(MouseEvent.CLICK, onsquare12);
       function onsquare12(e:MouseEvent):void {
sximata = square12;
     }

    square13.addEventListener(MouseEvent.CLICK, onsquare13);
      function onsquare13(e:MouseEvent):void {
sximata = square13;
square13Clicked = true;
checkButtons();
    }


   square14.addEventListener(MouseEvent.CLICK, onsquare14);
   function onsquare14(e:MouseEvent):void {
sximata = square14;
square14Clicked = true;
checkButtons();
    }


     square15.addEventListener(MouseEvent.CLICK, onsquare15);
     function onsquare15(e:MouseEvent):void {
sximata = square15;
     }

    square16.addEventListener(MouseEvent.CLICK, onsquare16);
     function onsquare16(e:MouseEvent):void {
sximata = square16;
     }

   square17.addEventListener(MouseEvent.CLICK, onsquare17);
   function onsquare17(e:MouseEvent):void {
sximata = square17;
     }


    square18.addEventListener(MouseEvent.CLICK, onsquare18);
    function onsquare18(e:MouseEvent):void {
sximata = square18;
       }

    square19.addEventListener(MouseEvent.CLICK, onsquare19);
    function onsquare19(e:MouseEvent):void {
sximata = square19;
     }


     square20.addEventListener(MouseEvent.CLICK, onsquare20);
     function onsquare20(e:MouseEvent):void {
sximata = square20;
      }


     square21.addEventListener(MouseEvent.CLICK, onsquare21);
     function onsquare21(e:MouseEvent):void {
sximata = square21;
      }


    square22.addEventListener(MouseEvent.CLICK, onsquare22);
    function onsquare22(e:MouseEvent):void {
sximata = square22;
      }

    square23.addEventListener(MouseEvent.CLICK, onsquare23);
    function onsquare23(e:MouseEvent):void {
sximata = square23;
      }


    square24.addEventListener(MouseEvent.CLICK, onsquare24);
     function onsquare24(e:MouseEvent):void {
sximata = square24;
       }


     square25.addEventListener(MouseEvent.CLICK, onsquare25);
      function onsquare25(e:MouseEvent):void {
sximata = square25;
       }




    var myColorTransform:ColorTransform=transform.colorTransform;
    red_btn.addEventListener(MouseEvent.CLICK, changeColour);
    function changeColour(event:MouseEvent):void {


    myColorTransform.color=0xBD8D46;

    sximata.transform.colorTransform=myColorTransform;


   }


     resetButton.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);

     function fl_MouseClickHandler(event:MouseEvent):void
     {
    gotoAndPlay(1);
       }




   var square8Clicked:Boolean = false;
   var square9Clicked:Boolean = false;
   var square10Clicked:Boolean = false;
   var square13Clicked:Boolean = false;
   var square14Clicked:Boolean = false;


     function checkButtons():void
{
if(square8Clicked && square9Clicked && square10Clicked && square13Clicked && square14Clicked)
    {
    text_mc.visible = true;

   }
}

2 个答案:

答案 0 :(得分:0)

您可以为每个其他函数添加一个布尔变量,如果单击任何其他正方形,则转换为true。例如:

var isClicked:Boolean = false;
square1.addEventListener(MouseEvent.CLICK, onsquare1);
function onsquare1(e:MouseEvent):void {
    sximata = square1;
    isClicked = true;
}

然后在你的检查按钮功能中,检查“isClicked”是否仍为假:

function checkButtons():void
{
    if(!isClicked && square8Clicked && square9Clicked && square10Clicked && square13Clicked && square14Clicked)
    {
        text_mc.visible = true;

    }
}

答案 1 :(得分:0)

我的解决方案如下。它基于计算用户可以点击的每种类型的MovieClip所获得的点击次数。代码的相关部分将采用onClick()checkClickCounts()方法。

首先,您将看到在buildMCs()方法中我只创建了一堆MovieClip并将它们放在网格中的舞台上。我已经做到了这一点,你提到的“特定”MC是网格每一行的第一项。对于这些“特定”MC中的每一个,我添加了一个属性:isSpecial:Boolean并将其设置为true。稍后,当单击MC时,OnClick()方法将检查MC是否特殊,并将增加相关的点击计数属性。然后,调用checkClickCounts()。如果计算出5次良好点击和0次不良点击,那么我们就会让用户知道。这是您显示文本字段的位置。 (就我而言,我只是在屏幕上画一个大红色矩形。

我在此演示的另一个建议是避免重复鼠标点击代码。如果你查看构造函数,你会看到我使用了这一行:

this.addEventListener(MouseEvent.CLICK, onClick);

这告诉主舞台精灵要听所有鼠标点击,甚至是那些发生在它内部的MovieClip的点击。在onClick()方法中,我检查项目是否被点击 - 事件的目标 - 是一个MovieClip。如果是的话,那么我会进行额外的检查以确定点击的MC是否是我想要的。通过这样做,我能够编写一次处理鼠标点击的代码,现在如果我需要改变一些东西,我只需要做一次,而不是25次。节省时间,但也使代码不易出错,因为如果需要进行更改,我不太可能错过任何内容。

package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;

public class Clicky extends Sprite
{
    public function Clicky()
    {
        this.buildMCs();

        this.addEventListener(MouseEvent.CLICK, onClick);
    }

    private function buildMCs ():void
    {
        var rows:int = 5;
        var cols:int = 5;

        var boxSize:int = 10;

        for (var r:int = 0; r < rows; r++)
        {
            for (var c:int = 0; c < cols; c++)
            {
                var newMC:MovieClip = new MovieClip();
                newMC.graphics.lineStyle(0, 0x00ff00);

                // want to mark the "specific" movieclips
                if (c == 0)
                {
                    newMC.graphics.beginFill(0x0000ff);

                    // just something that makes this MC unique... ideally
                    // this would be not a MovieClip, but a class that defines
                    // actual properties worth checking for
                    newMC.isSpecial = true;
                }
                else
                {
                    newMC.graphics.beginFill(0x00ff00);
                }
                newMC.graphics.drawRect(0, 0, boxSize, boxSize);

                this.addChild(newMC);

                newMC.x = (c * boxSize);
                newMC.y = (r * boxSize);
            }
        }
    }

    private function onClick (e:MouseEvent):void
    {
        if (e.target is MovieClip)
        {
            var mc:MovieClip = e.target as MovieClip;
            mc.alpha = .25;

            // disable the clicking for the clicked item
            mc.mouseEnabled = false;

            if (mc.isSpecial)
            {
                _specialClicks++;
            }
            else
            {
                _badClicks++;
            }

            this.checkClickCounts();
        }
    }

    private var _specialClicks:int = 0;
    private var _badClicks:int = 0;
    private function checkClickCounts ():void
    {
        if (_specialClicks == 5 && _badClicks == 0)
        {
            // do your thing - show the text_mc, play a sound, award a prize, etc.
            this.graphics.beginFill(0xff0000);
            this.graphics.drawRect(0, 0, 1000, 1000);
        }
    }
}
}