0

この基本的な関数メソッドを配列に変換して簡単に呼び出すにはどうすればよいですか。ほとんどの場合、オブジェクトが false になってから何かを実行するかどうかを比較しているだけです...しかし、これは非常に簡単なことの多くのコードのように思えます。何か案は?

    var b:Boolean = true;
        var i:Boolean = true;
        var t:Boolean = true;
        var a:Boolean = true;
        var m:Boolean = true;
        var ii:Boolean = true;
        var n:Boolean = true;
        var e:Boolean = true;
        var s:Boolean = true;

        level5_mc.let_b_mc.addEventListener(MouseEvent.CLICK, hitB);
        level5_mc.let_i_mc.addEventListener(MouseEvent.CLICK, hitI);
        level5_mc.let_t_mc.addEventListener(MouseEvent.CLICK, hitT);
        level5_mc.let_a_mc.addEventListener(MouseEvent.CLICK, hitA);
        level5_mc.let_m_mc.addEventListener(MouseEvent.CLICK, hitM);
        level5_mc.let_ii_mc.addEventListener(MouseEvent.CLICK, hitII);
        level5_mc.let_n_mc.addEventListener(MouseEvent.CLICK, hitN);
        level5_mc.let_e_mc.addEventListener(MouseEvent.CLICK, hitE);
        level5_mc.let_s_mc.addEventListener(MouseEvent.CLICK, hitS);


        function hitB(event:MouseEvent){
            b=false;
            trace("good");
              level5_mc.removeChild(level5_mc.let_b_mc);  
        }

        function hitI(event:MouseEvent){
            if (b==false){
                i=false;
                level5_mc.removeChild(level5_mc.let_i_mc);  
            }
            else {
                //decrease timer
                i=true;
            }
        }

        function hitT(event:MouseEvent){
            if (b==false && i==false){
                t=false;
                level5_mc.removeChild(level5_mc.let_t_mc);  
            }
            else {
                //decrease timer
                i=true;
            }
        }

など...以下に編集

 var b=level5_mc.let_b_mc;
                    var i=level5_mc.let_i_mc;
                    var t=level5_mc.let_b_mc;

                var movieClips:Array = [b,i,t];
                var movieClipFlags:Object = {
                    b:[],
                    i:[b],
                    t:[b,i]
                };

                for each(var mc:MovieClip in movieClips) {
                   mc.addEventListener(MouseEvent.CLICK,movieClipHit);
                }

                function movieClipHit(e:MouseEvent) {
                   var mc:MovieClip = e.target as MovieClip;
                   if(readyToRemove(mc))
                      level5_mc.removeChild(mc);
                   else
                      trace("Can't remove yet.");
                }

                function readyToRemove(mc:MovieClip):Boolean {
                   for each(var mc:MovieClip in movieClipFlags[mc]) {
                       //if it has parent, it isn't removed yet.
                       if(mc.parent)
                         return false;
                   }
                   return true;
                }
4

1 に答える 1

0

次のようなものから、いくつかのアイデアが得られるはずです。必要に応じていくつかの調整を行う必要があるコードをテストしていません。

//put other movies clips in this array too.
var movieClips:Array = [level5_mc.let_b_mc,level5_mc.let_i_mc]; 


var movieClipFlags:Object = {
   level5_mc.let_b_mc:[],
   level5_mc.let_i_mc:[level5_mc.let_b_mc],
   level5_mc.let_t_mc:[level5_mc.let_b_mc,level5_mc.let_i_mc]
   //put other movie clips here too with the movie clips that need to be removed in the array
};

foreach(var mc:MovieClip in movieClips) {
   mc.addEventListener(MouseEvent.CLICK,movieClipHit);
}


private function movieClipHit(e:MouseEvent) {
   var mc:MovieClip = e.target as MovieClip;
   if(readyToRemove(mc))
      level5_mc.removeChild(mc);
   else
      trace("Can't remove yet.");
}

private function readyToRemove(mc:MovieClip):Boolean {

   foreach(var mc:MovieClip in movieClipFlags[mc]) {
       //if it has parent, it isn't removed yet.
       if(mc.parent)
         return false;
   }
   return true;
}

それが役に立てば幸い。

于 2012-10-27T19:06:25.190 に答える