2014-01-08 101 views
0

我正在嘗試編輯此惡蛇版本的actionscript,以在我正在使用的網站中用作娛樂。我的問題是1010錯誤,這對我來說似乎沒有意義。我試過調試沒有成功。ActionScript 3.0錯誤1010

是出現打印的第一輸出如下:

Fonts should be embedded for any text that may be edited at runtime, other than text with the "Use Device Fonts" setting. Use the Text > Font Embedding command to embed fonts.

每當我的蛇吃的食物(這麼說),我得到的錯誤:

TypeError: Error #1010: A term is undefined and has no properties. at Main/onEnterFrame()

完整的代碼是這樣的:

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 = 20;//speed of the snake 
    var score:int; 
    var vx:int; 
    var vy:int; 
    var head:SnakePart; 
    var gFood; 
    var SnakeDirection:String; 
    var snake:Array; 

    public function Main(){ 
     init(); 
    } 
    function init():void { 
     //Initialize everything! 
     vx = 1; vy = 0; 
     score = 1; 
     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 , onKeyUp); 
     stage.addEventListener(KeyboardEvent.KEY_DOWN , onKeyDown); 
     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 { 
      if(score%2==0){ 
       gFood = new SnakePart(); 
      }else{ 
       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 onKeyDown(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 onKeyUp(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; 
      //head.rotate(-90); 
     }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 i = 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; 
      if(score%2==0){ 
       bodyPart = new Food(); 
      }else{ 
       bodyPart = new SnakePart(); 
      } 
      bodyPart.x = snake[snake.length - 10].x; 
      bodyPart.y = snake[snake.length - 10].y; 
      snake.push(bodyPart); 
      addChild(bodyPart); 
     } 
     //display scores 
     txtScore.text = String(score); 
    } 
} 

}

隨時提出任何問題!

+0

首先第一件事情......安裝播放器的閃存調試版本,所以你得到確切的行運行時錯誤。 (http://www.adobe.com/support/flashplayer/downloads.html) 然後發佈錯誤的輸出,以便更容易地看到問題出在哪裏。它的出現,在你的onEnterFrame方法有一個null參數,無論是:頭,階段,蛇或TXT分數 – mihai

+0

@mihai謝謝一堆,這真的幫助。但是我得到第二個問題。我正試圖旋轉蛇的頭部以適應它的走向。它告訴我旋轉不是函數 'head.rotate(-90);' –

+0

@mihai更新:我將它改爲旋轉。 ** TypeError:錯誤#1006:值不是函數。** –

回答

0

根據我們的談話,這似乎是解決問題的

  1. 的字體,發現是動態的文本框,並在屬性面板中選擇嵌入,然後檢查你需要嵌入的字符,否則它們可能不在運行時出現

  2. 運行時1010錯誤,看來在您的onEnterFrame方法中有一個null參數,可以是:head,stage,snake或txtScore。

建議安裝Flash Player的調試準確地告訴你哪一行的錯誤是: http://www.adobe.com/support/flashplayer/downloads.html

+0

你看起來很有能力。你介意幫助我度過這場遊戲大屠殺嗎?我是新來的AS,這裏的事情變得非常糟糕。我真的很感激。 –

+0

謝謝你的讚美塞巴斯蒂安,但不幸的是我不會因時間限制協助。您在本網站上發佈的任何問題,都有很多人願意提供幫助。 – mihai