0

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

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

益智游戏.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);
    }


}


}

盒子.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;
    }


}


}

问题是,一个新游戏应该通过单击“下一步”按钮来开始,但是当你这样做时,盒子就不能再移动了。

4

1 回答 1

1

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

然后在buildMatrix()您添加新框并向它们添加事件侦听器。

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

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

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

[编辑]

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

重置该数组的两种快速方法:

boxes.length = 0;

或者

boxes = [];
于 2013-02-05T20:03:28.120 回答