AS3:无法清除行

时间:2014-07-07 15:23:14

标签: actionscript-3 line

所以我设置的是带有点的图案解锁屏幕,然后绘制图案,然后屏幕解锁。但问题是除非你将鼠标光标直接放在一个点上,否则不会终止模式。如果你通过点不会发生任何事情,但线条将留在屏幕上,你仍然会在锁定屏幕上。然后使用将必须单击以清除行并再次尝试。我一直在玩它,但似乎无法弄清楚为什么它会像这样工作。

这是代码

package
{
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import fl.transitions.Tween;
    import fl.transitions.easing.Strong;
    import flash.display.Shape;



    public class Main extends Sprite
    {    
        private var lineContainer:Shape = new Shape();
        private var dots:Array = []; // Stores the in stage movieclips
        private var pattern:Array = []; //The pattern entered by the user 
        private var pass:Array;

        public function Main():void
        {
            dots = [one,two,three,four,five,six,seven,eight,nine]; //add the clips in stage
            pass = [one,four,seven,eight,five,two]; //The correct pattern to proceed
            addChildAt(lineContainer, this.getChildIndex(one)); //the line container right behind the first dot.
            addListeners();
        }

        private function addListeners():void //adds the listeners to each dot
        {
            var dotsLength:int = dots.length;

            for (var i:int = 0; i < dotsLength; i++)
            {
                dots[i].addEventListener(MouseEvent.MOUSE_DOWN, initiatePattern);
                dots[i].addEventListener(MouseEvent.MOUSE_UP, stopPattern); 

            }
        }

        /* Adds a mouse over listener and uses it to add the number of the dot to the pattern */

        private function initiatePattern(e:MouseEvent):void
        {
            pattern = []; //reset array
            lineContainer.graphics.clear(); //clear lines

            for (var i:int = 0; i < dots.length; i++)
            {
                dots[i].addEventListener(MouseEvent.MOUSE_OVER, addPattern);
            }

                    addPattern(e); //trigger the mouse over for this element
        }

        private function addPattern(e:MouseEvent):void
        {
            if (pattern.indexOf(e.currentTarget) == -1) { 
                pattern.push(e.currentTarget); //adds the pattern on mouse over
                drawLines();

                var dot:MovieClip = MovieClip(e.currentTarget);
                if(dot.currentFrame < 2) dot.play(); //play only if on the first frame
            }
        }

        private function drawLines():void {
            lineContainer.graphics.clear(); //clear the current lines
            lineContainer.graphics.lineStyle(5, 0x00FF00); //thickness (8px) and color (green) of the lines

             if (pattern.length > 1) { //don't draw if there aren't at least two dots in the pattern
             lineContainer.graphics.moveTo(pattern[0].x + pattern[0].width * .0, pattern[0].y + pattern[0].height * .0); //move to first
         for (var i:int = 1; i < pattern.length; i++) {
        lineContainer.graphics.lineTo(pattern[i].x + pattern[i].width * .0, pattern[i].y + pattern[i].height * .0); //draw a line to the current dot
    }
          }

              lineContainer.graphics.endFill();

        }

        private function stopPattern(e:MouseEvent):void //stops storing the pattern on mouse up
        {
            for (var i:int = 0; i < dots.length; i++)
            {
                dots[i].removeEventListener(MouseEvent.MOUSE_OVER, addPattern);
                dots[i].gotoAndStop(1); //go back to the first frame
            }

            checkPattern();
        }

        private function checkPattern():void //compares the patterns
        {
            var pLength:int = pass.length;
            var correct:int = 0;

            for (var i:int = 0; i < pLength; i++) //compares each number entered in the user array to the pass array
            {
                if (pass[i] == pattern[i])
                {
                    correct++;
                }
            }

            if (correct == pLength) //if the arrays match
            {
              //Hides Sign In
              MovieClip(root).LockScreen.visible = false;
              MovieClip(root).RTID.visible = false;
              MovieClip(root).SignIn.visible = false;
              //Turns On Main Menu
              MovieClip(root).gamemenu_mc.visible = true;
              MovieClip(root).biggamesmenu_mc.visible = true;
              MovieClip(root).totaltextmenu_mc.visible = true;
              MovieClip(root).tmenu_mc.visible = true;
              MovieClip(root).smenu_mc.visible = true;
              MovieClip(root).optionsmenu_mc.visible = true;
            }

            pattern = []; //clears the user array
            lineContainer.graphics.clear(); //clear the lines

        }
    }
}

2 个答案:

答案 0 :(得分:3)

您需要做的就是在舞台上添加MOUSE_UP侦听器而不是每个点。

  1. addListeners 方法中移除这一行:

    dots[i].addEventListener(MouseEvent.MOUSE_UP, stopPattern); 
    
  2. initiatePattern 方法中,添加以下行:

    stage.addEventListener(MouseEvent.MOUSE_UP, stopPattern); 
    
  3. stopPattern 方法中,添加以下行:

    stage.removeEventListener(MouseEvent.MOUSE_UP, stopPattern);
    
  4. 现在无论你在释放鼠标时光标在哪里(只要应用程序有焦点),它就会停止模式。

    另外(根据一些评论),只要您还没有启用多点触控,鼠标事件就可以在移动设备上正常运行。


    这是修改后的方法现在应该是什么样的:

        private function addListeners():void //adds the listeners to each dot
        {
            var dotsLength:int = dots.length;
    
            for (var i:int = 0; i < dotsLength; i++)
            {
                dots[i].addEventListener(MouseEvent.MOUSE_DOWN, initiatePattern);
            }
        }
    
        /* Adds a mouse over listener and uses it to add the number of the dot to the pattern */
    
        private function initiatePattern(e:MouseEvent):void
        {
            pattern = []; //reset array
            lineContainer.graphics.clear(); //clear lines
    
            for (var i:int = 0; i < dots.length; i++)
            {
                dots[i].addEventListener(MouseEvent.MOUSE_OVER, addPattern);
            }
    
            stage.addEventListener(MouseEvent.MOUSE_UP,stopPattern);
            addPattern(e); //trigger the mouse over for this element
        }
    
        private function stopPattern(e:MouseEvent):void //stops storing the pattern on mouse up
        {
            for (var i:int = 0; i < dots.length; i++)
            {
                dots[i].removeEventListener(MouseEvent.MOUSE_OVER, addPattern);
                dots[i].gotoAndStop(1); //go back to the first frame
            }
    
            stage.removeEventListener(MouseEvent.MOUSE_UP,stopPattern);
            checkPattern();
        }
    

    只是为了好玩,这是检查正确模式的一种更有效的方法。

        private function checkPattern():void //compares the patterns
        {
            var correct:Boolean = true;
    
            for (var i:int = 0; i < pass.length; i++) //compares each number entered in the user array to the pass array
            {
                if (pattern.length != pass.length || pass[i] != pattern[i]) //this way if they select less than the correct amount of dots, it won't error, and you stop the loop as soon as there's something incorrect
                {
                    correct = false;
                    break;
                }
            }
    
            if (correct) //if the arrays match
            {.....
    

答案 1 :(得分:1)

您唯一检查MOUSE_UP事件的模式。您需要在MOUSE_OVER中查看它。在checkPattern()中添加对addPatern()的电话。

if (pattern.indexof(e.currentTarget)==-1) {
   patern.push(e.currentTarget);
   drawLines();

   var dot:MovieClip = MovieClip(e.currentTarget);
   if(dot.currentFrame < 2) dot.play();

   checkPattern(); // <---
}

当用户将鼠标移到点上时,程序会调用addPatern(),然后检查模式以查看它是否正确。 (又名。if (correct=pLength)

编辑:

您需要调用清除if语句中的行和模式,以便您实际检查模式是否正确。这就是线条不渲染的原因。

if (correct==pLength)
{
  ...

  // Set pattern to [], clear screen, etc.
}