0
votes

I have 25 movie clips on stage and they all can be clicked and colored. I want a movie clip named text_mc to became visible if only 5 specific buttons from those are clicked and colored - not more. If the user choose more than those five movie clips (even thought that 5 movie clips are included) then the movie clip named text_mc should stay invisible. I can' t do the last part: if more than those 5 specific movie clips are clicked then the text_mc should stay invisible. Can you please help me? This is my code

  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

2 Answers

0
votes

You could add a boolean variable to each of the other functions that turns to true if any of the other squares are clicked. For example:

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

And then in your check buttons function, check to see if "isClicked" is still false:

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

    }
}
0
votes

My solution is below. It's based on counting the number of clicks received by each type of MovieClip the user can click on.The relevant parts of the code would be in the onClick() and checkClickCounts() methods.

First, you'll see that in the buildMCs() method I simply create a bunch of MovieClips and place them on the stage in a grid. I've made it so that the "specific" MCs that you mention are the first items on each row of the grid. To each of these "specific" MCs, I've added a property: isSpecial:Boolean and set it to true. Later, when a MC is clicked, the OnClick() method will check to see if the MC was special or not, and will increment the relevant click count property. Then, checkClickCounts() is called. If 5 good clicks and 0 bad clicks are counted up, then we let the user know. This is where you'd display your textfield. (In my case, I just draw a big red rectangle on the screen.

Another suggestion I demo here is to avoid repeating your mouse click code. If you look in the constructor, you'll see that I used this line:

this.addEventListener(MouseEvent.CLICK, onClick);

This tells the main stage sprite to listen to all mouse clicks, even the ones that happen to MovieClips inside of it. In the onClick() method I check to see if the item clicked - the target of the event - was a MovieClip. If it was, then I do the additional checking to see if the MC clicked was one that I wanted. By doing this, I was able to write the code for handling the mouse clicks once, and now if I need to change something, I only have to do it one time, rather than 25 times. Saves me time, but also makes the code less error-prone because I'm less likely to miss something if there's a change that needs to be made.

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