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

Posted

技术标签:

【中文标题】如果仅单击五个影片剪辑(不是更多),如何使影片剪辑可见【英文标题】:How to make a movie clip visible if only five movie clips (not more) are clicked 【发布时间】:2013-07-23 03:39:23 【问题描述】:

我在舞台上有 25 个电影剪辑,它们都可以点击和着色。如果只有 5 个特定按钮被单击和着色,我希望一个名为 text_mc 的电影剪辑变得可见 - 而不是更多。如果用户选择的影片剪辑超过了这五个影片剪辑(甚至认为包含 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;

   

【问题讨论】:

【参考方案1】:

您可以为每个其他函数添加一个布尔变量,如果单击其他任何方块,则该变量变为 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;

    

【讨论】:

非常感谢。我现在按照我的意愿做了。您的帮助非常有帮助。【参考方案2】:

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

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

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

this.addEventListener(MouseEvent.CLICK, onClick);

这告诉主舞台精灵听所有的鼠标点击,甚至是发生在其中的 MovieClips 的那些。在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);
        
    


【讨论】:

以上是关于如果仅单击五个影片剪辑(不是更多),如何使影片剪辑可见的主要内容,如果未能解决你的问题,请参考以下文章

用flash as3语言如何将影片剪辑存入一个数组以及如何访问?

AS3.0中影片剪辑元件嵌套了个动态文本框,怎样才能在文本框上也能显示手型?

如何加载带有条件动态文本的影片剪辑。如果别的

ActionScript 3 选择影片剪辑

如何使用 as3 同时访问所有影片剪辑(以及影片剪辑中的影片剪辑......)?

调整影片剪辑 AS3 的大小