2013-07-18 95 views
0

我確定有一些愚蠢的錯誤隱藏在我身上,但我已經在這裏超過了2個小時,似乎無法找到問題所在。有人請幫我確定我做錯了什麼,錯誤在標題中,以下是我的相關代碼。Actionscript 3.0:TypeError:錯誤#1006:addToValue不是函數

進一步有用的細節:

確切錯誤是:

類型錯誤:錯誤#1006:addToValue不是一個函數。 在SimpleMenuMain/onTick() 在flash.utils ::定時器/ _timerDispatch() 在flash.utils ::定時器/蜱()

遊戲運行,但不計成績,我想比分被計數。我有三重檢查,我所有的實例名稱是正確的,我真的沒有看到我在那裏做了什麼錯。也許我可能會錯誤地引用它們,但代碼中使用的所有名稱都位於符號和文本內的對象中......等等。

非常感謝你的時間,這真的讓我很難過。

P.S .---我跟着一個教程,所以如果我做了一些不必要的事情,不要責怪我!儘管除了幫助我回答這個難題之外,還有其他評論是值得的=)。

文檔類

package 
{ 
    import flash.display.MovieClip; 

    public class SMGDocClass extends MovieClip 
    { 
     public var playScreen:SimpleMenuMain; 
     public var titleScreen:TitleScreen; 
     public var gameOver:GameOver; 

     public function SMGDocClass() 
     { 
      titleScreen = new TitleScreen(); 
      titleScreen.addEventListener(NavigationEvent.START,onRequestStart,false,0,true); 
      titleScreen.x = 0; 
      titleScreen.y = 0; 
      addChild(titleScreen); 

     } 

     public function onStickman1Death(stickman1Event:Stickman1Event):void 
     { 
      var finalScore:Number = playScreen.getFinalScore(); 
      var finalClockTime:Number = playScreen.getFinalClockTime(); 

      gameOver = new GameOver(); 
      gameOver.addEventListener(NavigationEvent.RESTART,onRequestRestart,false,0,true); 
      gameOver.addEventListener(NavigationEvent.MAINMENU,onRequestMainMenu,false,0,true); 
      gameOver.x = 5; 
      gameOver.y = 6; 
      gameOver.setFinalScore(finalScore); 
      gameOver.setFinalClockTime(finalClockTime); 
      addChild(gameOver); 

      playScreen = null; 

     } 

     public function onRequestStart(navigationEvent:NavigationEvent):void 
     { 
     playScreen = new SimpleMenuMain(); 
     playScreen.addEventListener(Stickman1Event.DEAD, onStickman1Death,false,0,true); 
     playScreen.x = 0; 
     playScreen.y = 0; 
     addChild(playScreen); 

     titleScreen = null; 
     } 

     public function restartGame():void 
     { 
      playScreen = new SimpleMenuMain; 
      playScreen.addEventListener(Stickman1Event.DEAD, onStickman1Death,false,0,true); 
      playScreen.x = 0; 
      playScreen.y = 0; 
      addChild(playScreen); 

      gameOver = null; 
     } 

     public function onRequestRestart(navigationEvent:NavigationEvent):void 
     { 
      restartGame(); 
     } 

     public function onRequestMainMenu(navigationEvent:NavigationEvent):void 
     { 
      titleScreen = new TitleScreen(); 
      titleScreen.addEventListener(NavigationEvent.START,onRequestStart,false,0,true); 
      titleScreen.x = 0; 
      titleScreen.y = 0; 
      addChild(titleScreen); 

      removeChild(gameOver); 
      gameOver = null; 
     } 

    } 
} 

播放屏幕CLASS

package { 

    import flash.display.MovieClip; 
    import flash.utils.Timer; 
    import flash.events.TimerEvent; 

    public class SimpleMenuMain extends MovieClip { 

     public var army1:Array; 
     public var enemy1:Enemy1; 
     public var gameTimer:Timer; 
     public var stickman1:Stickman1; 

     public function SimpleMenuMain() { 

      army1 = new Array(); 
      var newEnemy1 = new Enemy1(100, -15); 
      army1.push(newEnemy1); 
      addChild(newEnemy1); 

      stickman1 = new Stickman1(); 
      addChild(stickman1); 
      stickman1.x = mouseX; 
      stickman1.y = mouseY; 

      gameTimer = new Timer(25); 
      gameTimer.addEventListener(TimerEvent.TIMER, onTick, false, 0, true); 
      gameTimer.start(); 

     } 

     public function onTick(timerEvent:TimerEvent):void 
     { 
      gameClock.addToValue(25); 
      if (Math.random() < 0.1) 
      { 
       var randomX:Number = Math.random() * 400; 
       var newEnemy1:Enemy1 = new Enemy1(randomX, -15); 
       army1.push(newEnemy1); 
       addChild(newEnemy1); 
       gameScore.addToValue(10); 
      } 

      stickman1.x = mouseX; 
      stickman1.y = mouseY; 

      for each (var enemy1:Enemy1 in army1) 
      { 
       enemy1.moveDown(); 

       if (stickman1.hitTestObject(enemy1)) 
       { 
        gameTimer.stop(); 
        dispatchEvent(new Stickman1Event(Stickman1Event.DEAD)); 
       } 
      } 

     } 

     public function getFinalScore():Number 
     { 
      return gameScore.currentValue; 
     } 

     public function getFinalClockTime():Number 
     { 
      return gameClock.currentValue; 
     } 

    } 

} 

Counter類

package 
{ 
    import flash.display.MovieClip; 
    public class Counter extends MovieClip 
    { 
     public var currentValue:Number; 

     public function Counter() 
     { 
      reset(); 
     } 

     public function addToValue(amountToAdd:Number):void 
     { 
      currentValue = currentValue + amountToAdd; 
      updateDisplay(); 
     } 

     public function reset():void 
     { 
      currentValue = 0; 
      updateDisplay(); 
     } 

     public function updateDisplay():void 
     { 

     } 
    } 
} 

Stickman1Event CLASS

package 
{ 
    import flash.events.Event; 
    public class Stickman1Event extends Event 
    { 
     public static const DEAD:String = "dead"; 

     public function Stickman1Event(type:String, bubbles:Boolean = false, cancelable:Boolean = false) 
     { 
      super(type, bubbles, cancelable); 

     } 

     public override function clone():Event 
     { 
      return new Stickman1Event(type, bubbles, cancelable); 
     } 

     public override function toString():String 
     { 
      return formatToString("Stickman1Event", "type", "bubbles", "cancelable", "eventPhase"); 
     } 
    } 
} 

NavigationEvent CLASS

package 
{ 
    import flash.events.Event; 
    public class NavigationEvent extends Event 
    { 
     public static const RESTART:String = "restart"; 
     public static const START:String = "start"; 
     public static const MAINMENU:String = "mainmenu"; 

     public function NavigationEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false) 
     { 
      super(type, bubbles, cancelable); 

     } 

     public override function clone():Event 
     { 
      return new NavigationEvent(type, bubbles, cancelable); 
     } 

     public override function toString():String 
     { 
      return formatToString("NavigationEvent", "type", "bubbles", "cancelable", "eventPhase"); 
     } 
    } 
} 

得分類

package 
{ 
    import flash.text.TextField; 
    public class Score extends Counter 
    { 
     public function Score() 
     { 
      super(); 
     } 

     override public function updateDisplay():void 
     { 
      super.updateDisplay(); 
      scoreDisplay.text = currentValue.toString(); 
     } 

    } 
} 
+0

除非我完全失明,否則永遠不會定義或實例化'gameScore'和'gameClock',這兩個對象是您試圖調用'addToValue()'的。不知道爲什麼這不是拋出一個null-ref錯誤,但這絕對是問題。這些對象不存在,因此不能在其上使用該方法。 –

+0

這些都被定義爲我的主要時間線上的實例....這就是爲什麼它不會拋出一個完整的錯誤,我認爲....只要我給他們實例名稱,我認爲我應該能夠引用他們就這樣好嗎? – spaderdabomb

+1

它們是「Counter」的實例嗎?計數器有'addToValue()'方法,所以他們需要擴展那個類才能訪問該方法。 –

回答

1

由於喬希已經指出的那樣,沒有任何保證的gameScore和gameClock對象實際上是反的情況。

我們需要查看實例化它的代碼/位置。

一些可能的選擇;

  • gameScore和gameClock不是實際的計數器實例
  • 你實例化計數器作爲影片剪輯,所以你必須把它添加到你的舞臺,但你可能沒有一起正確地迷上圖書館資產和Counter類。

你可以改變你的Counter構造函數,並運行它;

public function Counter() 
{ 
    trace("i am a counter"); 
    reset(); 
} 

...你應該看到跟蹤輸出兩次(對於gameScore和gameClock),如果你沒有看到跟蹤輸出,計數器沒有被構造,這意味着gameScore和gameClock只是普通的MovieClip實例。

+0

啊,謝謝你的提示。看來第二個重點是問題。我以某種方式創建了兩個同樣的符號,並且在我的遊戲屏幕上出現了錯誤的庫符號,Counter類實際上並不是正在使用的符號!再次感謝 – spaderdabomb

相關問題