2013-02-05 30 views
0

我對actionscript3很新穎,我目前正在研究滑動益智遊戲。遊戲就像,你在一個矩陣中生成8個盒子,每個盒子都有一個數字,你可以滑動盒子按照一定順序排列它們。那麼我已經完成了大部分所需的功能,但有一件事 - 如果按下「下一步」按鈕,則不能再移動這些框。滑動益智遊戲 - 動作3有一些問題3

我會在這裏粘貼我的代碼,包括主要類和框類。

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

這不是100%清楚,但在nextMatrix()方法中,您將刪除事件偵聽器。但是你永遠不要從舞臺上移除盒子(或者任何它們被添加到的容器)。

然後在buildMatrix()中添加新框並向它們添加事件偵聽器。

我相信正在發生的事情是原來的盒子仍然存在,但是沒有事件監聽器附加在它們上面。您的nextMatrix()函數應該有一個循環,它使用removeChild()方法刪除原始框。

即使這不能解決您當前的問題,它也會從舞臺上移除這些對象並釋放它們消耗的內存。

我不明白的是,通常當你做addChild()它應該添加對象的頂部可能已經有。所以有可能是其他問題導致了這個問題。但正如我所提到的,無論在舊箱子上做removeChild(),每次開始新遊戲時,它們都會持續並消耗更多的內存。

[編輯]

應該發生的另一件事是,你需要清除你nextMatrix()方法boxes陣列。否則,buildMatrix()方法中的代碼會將事件偵聽器添加到原始框中,而不是新框中。

兩個快速的方法來重置數組:

boxes.length = 0; 

boxes = []; 
+0

好 'removeChild之()' 不解決我的問題,但它是很好反正來釋放內存,謝謝。 – phil

+0

是的,我認爲可能是這樣。編輯我的答案,只是看到了另一個問題(這似乎是有道理的)。 –

+0

它的工作原理!你是對的,盒子陣列應該被清除掉。非常感謝! – phil