2013-12-09 19 views
0

嗨我現在正在測試這個遊戲,它會自動重新啓動,沒有我甚至完成遊戲...任何幫助將是偉大的!我真的不知道閃光那麼好,所以我不能真的要自己調試一下。AS3遊戲代碼不斷重新啓動

package{ 
import flash.display.MovieClip; 
import flash.events.KeyboardEvent; 
import flash.ui.Keyboard; 
import flash.events.Event; //used for ENTER_FRAME event 

public class Main extends MovieClip{ 
    const speed:int = 5;//speed of the snake 
    var score:int; 
    var vx:int; 
    var vy:int; 
    var gFood:Food; 
    var head:SnakePart; 
    var SnakeDirection:String; 
    var snake:Array; 

    public function Main(){ 
     init(); 
    } 
    function init():void { 
     //Initialize everything! 
     vx = 1; vy = 0; 
     score = 0; 
     snake = new Array(); 
     SnakeDirection = ""; 
     //add food to the stage 
     addFood(); 
     //add snakes head to the stage 
     head = new SnakePart(); 
     head.x = stage.stageWidth/2; 
     head.y = stage.stageHeight/2; 
     snake.push(head); 
     addChild(head); 

     stage.addEventListener(KeyboardEvent.KEY_UP , keyUp); 
     stage.addEventListener(KeyboardEvent.KEY_DOWN , keyDown); 
     addEventListener(Event.ENTER_FRAME , onEnterFrame); 
     //ENTER_FRAME listener is attached to main class and not to the stage directly 
    } 
    //This function will add food to the stage 
    function addFood():void { 
     gFood = new Food(); 
     gFood.x = 50 + Math.random()*(stage.stageWidth-100); 
     gFood.y = 50 + Math.random()*(stage.stageHeight-100); 
     addChild(gFood); 
    } 
    //this function will reset the game 
    function reset():void { 
     removeChild(gFood); 
     addFood(); 
     head.x = stage.stageWidth/2; 
     head.y = stage.stageHeight/2; 
     vx = 1;vy = 0; 

     for(var i = snake.length-1;i>0;--i){ 
      removeChild(snake[i]); 
      snake.splice(i,1); 
     } 
    } 
    function keyDown(event:KeyboardEvent):void{ 
     if(event.keyCode == Keyboard.LEFT){ 
      SnakeDirection = "left"; 
     }else if (event.keyCode == Keyboard.RIGHT) { 
      SnakeDirection = "right"; 
     }else if (event.keyCode == Keyboard.UP) { 
      SnakeDirection = "up"; 
     }else if (event.keyCode == Keyboard.DOWN) { 
      SnakeDirection = "down"; 
     } 
    } 
    function keyUp(event:KeyboardEvent):void { 
     if(event.keyCode == Keyboard.LEFT) { 
      SnakeDirection = ""; 
     }else if(event.keyCode == Keyboard.RIGHT) { 
      SnakeDirection = ""; 
     }else if(event.keyCode == Keyboard.UP) { 
      SnakeDirection = ""; 
     }else if(event.keyCode == Keyboard.DOWN){ 
      SnakeDirection = ""; 
     } 
    } 
    function onEnterFrame(event:Event):void { 
     //setting direction of velocity 
     if(SnakeDirection == "left" && vx != 1) { 
      vx = -1; 
      vy = 0; 
     }else if(SnakeDirection == "right" && vx != -1) { 
      vx = 1; 
      vy = 0; 
     }else if(SnakeDirection == "up" && vy != 1) { 
      vx = 0; 
      vy = -1; 
     }else if(SnakeDirection == "down" && vy != -1) { 
      vx = 0; 
      vy = 1; 
     } 

     //collison with stage 
     if(head.x - head.width/2 <= 0){ 
      score = 0; 
      reset(); 
     } 
     if(head.x + head.width/2 >= stage.stageWidth){ 
      score = 0; 
      reset(); 
     } 
     if(head.y - head.height/2 <= 0){ 
      score = 0; 
      reset(); 
     } 
     if(head.y + head.height/2 >= stage.stageHeight){ 
      score = 0; 
      reset(); 
     } 

     //move body of the snake 
     for(var i = snake.length-1;i>0;--i){ 
      snake[i].x = snake[i-1].x; 
      snake[i].y = snake[i-1].y; 
     } 
     //changing the position of snake's head 
     head.x += vx*speed; 
     head.y += vy*speed; 
     //collision with tail 
     for(var w = snake.length-1;i>=1;--i){ 
      if(snake[0].x == snake[i].x && snake[0].y == snake[i].y){ 
       reset(); 
       break; 
      } 
     } 
     //collision with food 
     if(head.hitTestObject(gFood)){ 
      score += 1; 
      removeChild(gFood); 
      addFood(); 
      var bodyPart = new SnakePart(); 
      bodyPart.x = snake[snake.length - 1].x; 
      bodyPart.y = snake[snake.length - 1].y; 
      snake.push(bodyPart); 
      addChild(bodyPart); 
     } 
     //display scores 
     txtScore.text = String(score); 
    } 
} 

}

+0

當您逐步瀏覽代碼時,您從調試中發現了什麼? – admdrew

+0

我會開始玩遊戲,然後重新開始,甚至沒有我完成它!在調試時我無法找到任何問題 – user3084741

+0

您是否能夠實際完成代碼?有沒有相關的日誌?您在這裏沒有特定的與編程相關的問題,只是一些代碼和一個聲明,說明它是「自己」重新啓動的。 – admdrew

回答

0

這主要是隻是一個問題: 「我怎麼解決這個問題的代碼?」。您應該嘗試提供問題發生時間的分析。 (即無論何時右轉)

對於您的特定問題,請嘗試在您撥打reset的不同位置插入trace語句,以便您確定問題的根源。

例如

if(head.y + head.height/2 >= stage.stageHeight){ 
     score = 0; 
     trace(">= stage.stageHeight"); 
     reset(); 
    } 

如果你開始行動腳本遊戲編程,我會深深建議http://gamedev.michaeljameswilliams.com/as3-avoider-game-tutorial-base/。這是一個真正學習如何製作遊戲的好教程,因爲每一步都經過仔細解釋。最終你會得到一個不錯的小遊戲,但更重要的是要理解一切都是如何運作的。

0

我認爲你應該在代碼的末尾或最後一幀添加stop(),以便循環在代碼完成時停止。不知道該把它放在哪裏。