有滑动益智游戏 - 动作脚本3的一些问题

时间:2013-02-05 19:44:41

标签: actionscript-3 flash

我对actionscript3很新,我目前正在开发滑动益智游戏。游戏就像,你在一个矩阵中生成8个盒子,每个盒子都有一个数字,你可以滑动盒子按照一定的顺序排列它们。好吧,我已经完成了大部分所需的功能,但有一件事 - 如果按下“下一步”按钮,则不能再移动这些框。

我会在这里粘贴我的代码,包括主类和盒子类。

Puzzlegame.as:

package 
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Graphics;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldType;
import flash.utils.Timer;
import flash.events.TimerEvent;

public class PuzzleGame extends Sprite
{
    public var whiteBox:Box;
    public var boxes:Array=new Array();
    public static const GAP:uint = 5;

    public var timerText:TextField= new TextField();
    public var timer:Timer;

    public var stepCounter:TextField=new TextField();
    public var steps:Number = 0;

    public var endGame:TextField=new TextField();
    public var endGameFormat:TextFormat=new TextFormat();

    public function PuzzleGame()
    {
        var background:Background;
        background= new Background();
        addChild(background);

        var nextbutton:nextButton;
        nextbutton=new nextButton();
        nextbutton.x = 100;
        nextbutton.y = 350;
        addChild(nextbutton);

        buildMatrix();

        stepCounter.x = 220;
        stepCounter.y = 370;
        stepCounter.text = "Steps: " + steps;

        addChild(stepCounter);

        timerText.border = true;
        timerText.height = 20;
        timerText.x = 10;
        timerText.y = 10;
        timerText.width = 150;
        timerText.text = "start";
        addChild(timerText);

        nextbutton.addEventListener(MouseEvent.CLICK, nextMatrix);

        stage.addEventListener(Event.ENTER_FRAME, checkAllBoxes);

    }


    public function nextMatrix(event:MouseEvent)
    {
        timer.stop();
        boxes[0].removeEventListener(MouseEvent.CLICK, box0Clicked);

        boxes[1].removeEventListener(MouseEvent.CLICK, box1Clicked);

        boxes[2].removeEventListener(MouseEvent.CLICK, box2Clicked);

        boxes[3].removeEventListener(MouseEvent.CLICK, box3Clicked);

        boxes[4].removeEventListener(MouseEvent.CLICK, box4Clicked);

        boxes[5].removeEventListener(MouseEvent.CLICK, box5Clicked);

        boxes[6].removeEventListener(MouseEvent.CLICK, box6Clicked);

        boxes[7].removeEventListener(MouseEvent.CLICK, box7Clicked);

        buildMatrix();
        steps = 0;
        stepCounter.text = "Steps: " + steps;

    }

    public function buildMatrix()
    {
        whiteBox = new Box(0);
        addChild(whiteBox);
        whiteBox.x = 10 + Box.BOX_SIZE * 2 + GAP * 3;
        whiteBox.y = 60 + Box.BOX_SIZE * 2 + GAP * 3;

        var i:uint;
        var rnd:Array = randomUnique.between(1,8);
        var box:Box;
        for (i=1; i<=8; i++)
        {

            if (i>=1&&i<=3)
            {
                box = new Box(rnd[i]);
                box.x = 10 + (i - 1) * Box.BOX_SIZE + GAP * i;
                box.y = 60 + GAP;

                addChild(box);
                boxes.push(box);
            }

            if (i>=4&&i<=6)
            {
                box = new Box(rnd[i]);
                box.x = 10 + (i - 4) * Box.BOX_SIZE + GAP * (i - 3);
                box.y = 60 + Box.BOX_SIZE + GAP * 2;

                addChild(box);
                boxes.push(box);
            }
            if (i>=7&&i<=8)
            {
                box = new Box(rnd[i]);
                box.x = 10 + (i - 7) * Box.BOX_SIZE + GAP * (i - 6);
                box.y = 60 + Box.BOX_SIZE * 2 + GAP * 3;

                addChild(box);
                boxes.push(box);
            }
        }
        boxes[0].addEventListener(MouseEvent.CLICK, box0Clicked);

        boxes[1].addEventListener(MouseEvent.CLICK, box1Clicked);

        boxes[2].addEventListener(MouseEvent.CLICK, box2Clicked);

        boxes[3].addEventListener(MouseEvent.CLICK, box3Clicked);

        boxes[4].addEventListener(MouseEvent.CLICK, box4Clicked);

        boxes[5].addEventListener(MouseEvent.CLICK, box5Clicked);

        boxes[6].addEventListener(MouseEvent.CLICK, box6Clicked);

        boxes[7].addEventListener(MouseEvent.CLICK, box7Clicked);

        timer = new Timer(1000);
        timer.addEventListener(TimerEvent.TIMER,onTimerTic);
        timer.start();


    }

    public function addWhiteBox()
    {
        whiteBox = new Box(0);
        addChild(whiteBox);
        whiteBox.x = 10 + Box.BOX_SIZE * 2 + GAP * 3;
        whiteBox.y = 60 + Box.BOX_SIZE * 2 + GAP * 3;
    }

    public function switchPlaces(box:Box,box2:Box)
    {
        var point:Point = new Point(box.x,box.y);
        box.x = box2.x;
        box.y = box2.y;
        box2.x = point.x;
        box2.y = point.y;
    }

    public function box0Clicked(event:MouseEvent)
    {
        if (((boxes[0].x==whiteBox.x && (Math.abs(boxes[0].y-whiteBox.y)==Box.BOX_SIZE+GAP)) || (boxes[0].y==whiteBox.y && (Math.abs(boxes[0].x-whiteBox.x)==Box.BOX_SIZE+GAP))))
        {
            switchPlaces(boxes[0],whiteBox);
            steps++;
            stepCounter.text = "Steps: " + steps;
        }
    }

    public function box1Clicked(event:MouseEvent)
    {
        if (((boxes[1].x==whiteBox.x && (Math.abs(boxes[1].y-whiteBox.y)==Box.BOX_SIZE+GAP)) || (boxes[1].y==whiteBox.y && (Math.abs(boxes[1].x-whiteBox.x)==Box.BOX_SIZE+GAP))))
        {
            switchPlaces(boxes[1],whiteBox);
            steps++;
            stepCounter.text = "Steps: " + steps;
        }
    }

    public function box2Clicked(event:MouseEvent)
    {
        if (((boxes[2].x==whiteBox.x && (Math.abs(boxes[2].y-whiteBox.y)==Box.BOX_SIZE+GAP)) || (boxes[2].y==whiteBox.y && (Math.abs(boxes[2].x-whiteBox.x)==Box.BOX_SIZE+GAP))))
        {
            switchPlaces(boxes[2],whiteBox);
            steps++;
            stepCounter.text = "Steps: " + steps;
        }
    }

    public function box3Clicked(event:MouseEvent)
    {
        if (((boxes[3].x==whiteBox.x && (Math.abs(boxes[3].y-whiteBox.y)==Box.BOX_SIZE+GAP)) || (boxes[3].y==whiteBox.y && (Math.abs(boxes[3].x-whiteBox.x)==Box.BOX_SIZE+GAP))))
        {
            switchPlaces(boxes[3],whiteBox);
            steps++;
            stepCounter.text = "Steps: " + steps;
        }
    }

    public function box4Clicked(event:MouseEvent)
    {
        if (((boxes[4].x==whiteBox.x && (Math.abs(boxes[4].y-whiteBox.y)==Box.BOX_SIZE+GAP)) || (boxes[4].y==whiteBox.y && (Math.abs(boxes[4].x-whiteBox.x)==Box.BOX_SIZE+GAP))))
        {
            switchPlaces(boxes[4],whiteBox);
            steps++;
            stepCounter.text = "Steps: " + steps;
        }
    }

    public function box5Clicked(event:MouseEvent)
    {
        if (((boxes[5].x==whiteBox.x && (Math.abs(boxes[5].y-whiteBox.y)==Box.BOX_SIZE+GAP)) || (boxes[5].y==whiteBox.y && (Math.abs(boxes[5].x-whiteBox.x)==Box.BOX_SIZE+GAP))))
        {
            switchPlaces(boxes[5],whiteBox);
            steps++;
            stepCounter.text = "Steps: " + steps;
        }
    }

    public function box6Clicked(event:MouseEvent)
    {
        if (((boxes[6].x==whiteBox.x && (Math.abs(boxes[6].y-whiteBox.y)==Box.BOX_SIZE+GAP)) || (boxes[6].y==whiteBox.y && (Math.abs(boxes[6].x-whiteBox.x)==Box.BOX_SIZE+GAP))))
        {
            switchPlaces(boxes[6],whiteBox);
            steps++;
            stepCounter.text = "Steps: " + steps;
        }
    }

    public function box7Clicked(event:MouseEvent)
    {
        if (((boxes[7].x==whiteBox.x && (Math.abs(boxes[7].y-whiteBox.y)==Box.BOX_SIZE+GAP)) || (boxes[7].y==whiteBox.y && (Math.abs(boxes[7].x-whiteBox.x)==Box.BOX_SIZE+GAP))))
        {
            switchPlaces(boxes[7],whiteBox);
            steps++;
            stepCounter.text = "Steps: " + steps;
        }
    }

    public function checkAllBoxes(event:Event)
    {

        if ((boxes[0].isInOriginalPos())&&(boxes[1].isInOriginalPos())&&(boxes[2].isInOriginalPos())&&(boxes[3].isInOriginalPos())&&(boxes[4].isInOriginalPos())&&(boxes[5].isInOriginalPos())&&(boxes[6].isInOriginalPos())&&(boxes[7].isInOriginalPos()))
        {

            endGame.height = 100;
            endGame.width = 300;
            endGame.x = 100;
            endGame.y = 180;
            endGame.text = "You win!";

            endGameFormat.color = 0x000000;
            endGameFormat.font = "Arial";
            endGameFormat.size = 15;
            endGameFormat.bold = true;
            endGame.setTextFormat(endGameFormat);

            addChild(endGame);

            onGameEnd();
        }
    }


    public function onGameEnd()
    {
        timer.stop();
        timerText.text = "DONE >> " + numberToTime(timer.currentCount);
    }

    public function onTimerTic(event:TimerEvent):void
    {
        timerText.text = numberToTime(Number(Timer(event.target).currentCount));
    }

    public function numberToTime(n:uint):String
    {
        return String(n/100);
    }


}


}

box.as:

package 
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.geom.Matrix;
import flash.display.GradientType;
import flash.display.Graphics;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFieldType;
import flash.events.Event;
public class Box extends MovieClip
{
    private var originalXPosition:Number;
    private var originalYPosition:Number;
    private var _color:uint;
    private var _borderAlpha:Number;
    private var _borderWidth:Number;
    private var numText:TextField = new TextField  ;
    private var numFormat:TextFormat = new TextFormat  ;
    public static const GAP:uint = 5;
    public static const BOX_SIZE:uint = 80;

    public static const BOX_CLICKED:String = "clicked";

    public function Box(boxNum:int)
    {
        var boxColor:uint;
        var numColor:uint;
        if ((boxNum == 0))
        {
            boxColor = 0xCCCCCC;
            numColor = 0xCCCCCC;
            originalXPosition = 10 + 2 * BOX_SIZE + 3 * GAP;
            originalYPosition = 60 + 2 * BOX_SIZE + 3 * GAP;
        }
        if ((boxNum == 1))
        {
            boxColor = 0xFF9900;
            numColor = 0xFFFFFF;
            originalXPosition = 10 + GAP;
            originalYPosition = 60 + GAP;
        }
        if ((boxNum == 2))
        {
            boxColor = 0xFFFFFF;
            numColor = 0xFF9900;
            originalXPosition = 10 + BOX_SIZE + 2 * GAP;
            originalYPosition = 60 + GAP;
        }
        if ((boxNum == 3))
        {
            boxColor = 0xFF9900;
            numColor = 0xFFFFFF;
            originalXPosition = 10 + 2 * BOX_SIZE + 3 * GAP;
            originalYPosition = 60 + GAP;
        }
        if ((boxNum == 4))
        {
            boxColor = 0xFFFFFF;
            numColor = 0xFF9900;
            originalXPosition = 10 + GAP;
            originalYPosition = 60 + BOX_SIZE + 2 * GAP;
        }
        if ((boxNum == 5))
        {
            boxColor = 0xFF9900;
            numColor = 0xFFFFFF;
            originalXPosition = 10 + BOX_SIZE + 2 * GAP;
            originalYPosition = 60 + BOX_SIZE + 2 * GAP;
        }
        if ((boxNum == 6))
        {
            boxColor = 0xFFFFFF;
            numColor = 0xFF9900;
            originalXPosition = 10 + 2 * BOX_SIZE + 3 * GAP;
            originalYPosition = 60 + BOX_SIZE + 2 * GAP;
        }
        if ((boxNum == 7))
        {
            boxColor = 0xFF9900;
            numColor = 0xFFFFFF;
            originalXPosition = 10 + GAP;
            originalYPosition = 60 + 2 * BOX_SIZE + 3 * GAP;
        }
        if ((boxNum == 8))
        {
            boxColor = 0xFFFFFF;
            numColor = 0xFF9900;
            originalXPosition = 10 + BOX_SIZE + 2 * GAP;
            originalYPosition = 60 + 2 * BOX_SIZE + 3 * GAP;
        }

        this.graphics.lineStyle(0,boxColor);
        this.graphics.beginFill(boxColor,1);
        this.graphics.drawRect(0,0,BOX_SIZE,BOX_SIZE);
        this.graphics.endFill();
        numText.x = this.x;
        numText.y = this.y;
        numText.height = 40;
        numText.width = 30;
        numText.text = boxNum.toString();

        numFormat.color = numColor;
        numFormat.font = "Arial";
        numFormat.size = 30;
        numFormat.bold = true;
        numText.setTextFormat(numFormat);
        addChild(numText);



    }

    public function isInOriginalPos():Boolean
    {
        if (this.x == originalXPosition && this.y == originalYPosition)
        {
            return true;
        }
        return false;
    }


}


}

问题是,应该通过点击“下一步”按钮启动新游戏,但是当你这样做时,这些框不能再移动了。

1 个答案:

答案 0 :(得分:1)

对我来说,这并非100%明确,但在您的nextMatrix()方法中,您正在删除事件侦听器。但是你永远不会从舞台上移除这些盒子(或者它们被添加到的任何容器)。

然后在buildMatrix()中添加新框并向其添加事件侦听器。

我认为发生的事情是原始框仍然存在,但没有事件监听器附加到它们。您的nextMatrix()函数应该有一个循环,它使用removeChild()方法删除原始框。

即使这不能解决您当前的问题,它也会从舞台上删除这些对象并释放他们正在消耗的内存。

我不明白的是,通常当你addChild()时,它应该将对象添加到已经存在的任何内容之上。所以其他东西可能导致问题。但正如我所提到的那样,在旧盒子上做removeChild()时,每次开始新游戏时它们都会持续存在并消耗更多内存。

<强> [编辑]

应该发生的另一件事是您需要清除boxes方法中的nextMatrix()数组。否则,buildMatrix()方法中的代码会将事件侦听器添加到原始框中,而不是新框。

重置该阵列的两种快速方法:

boxes.length = 0;

boxes = [];
相关问题