2013-10-26 176 views
0

我在尝试访问另一个类中的方法时在标题中出现错误。我有主类ZombieBots,它链接到同名的影片剪辑。然后我有3个更多的影片剪辑,它们在运行时被添加到ZombieBots剪辑中,并且每个都有它们自己的类。错误1006:不是函数

当我尝试从其他3类之一的ZombieBots类中访问的方法,我得到错误1006

我试图在ZombieBots级接入功能,这是不能被访问:

package { 
    import flash.events.*; 
    import flash.display.MovieClip; 
    import flash.geom.Rectangle; 

    public class ZombieBots extends MovieClip{ 

     private var pLives:int; 
     private var pScore:int; 
     private var pSkill:int; 
     private var pItems:int; 
     private var characterMC:Character; 
     private var cGameObjs:Array; 

     public function ZombieBots() { 
      /*cGameObjs = new Array(); 
      addCharacter(); 
      addItems(); 
      addBots(); 

      pLives = 5 - pSkill; 
      pScore = 0; 

      pItems = pSkill + 5;*/ 
      resetGame(); 
     } 

     private function addCharacter():void{ 
      trace("Adding the character"); 

      if (!characterMC){ 
      var myBorder:Rectangle = new Rectangle(35,35,600,480); 
      var myXY:Array = [38, 400]; 
      var myChar:int = Math.ceil(Math.random()*3); 
      var myKeys:Array = [37,39,38,40]; 
      var myDistance:int = myChar * 3; 
      characterMC = new Character(myBorder, myXY, myKeys, myChar, myDistance); 
      addChild(characterMC); 
      } 
      else{ 

       characterMC.x = 38; 
       characterMC.y = 510; 


       characterMC.gotoAndStop(pSkill); 

      } 
     } 


    private function addItems():void{ 
     trace("yeah boi"); 
     var mySkill:int = Math.ceil(Math.random() *3); 
     var myMaxItems:int = mySkill + 5; 
     trace(mySkill); 
     trace(myMaxItems); 
     trace(this); 
     for (var i:int = 0; i < myMaxItems; i++){ 
      var thisItem:Item = new Item(this, characterMC, mySkill); 
      thisItem.name = "item" + i; 
      cGameObjs.push(thisItem); 
      addChild(thisItem); 
     } 

     pSkill = mySkill; 
     updateScores(); 
    } 

     private function addBots():void{ 
      trace("adding the bots bra"); 
      var myBorder:Rectangle = new Rectangle(100,100,400,350); 
      var mySkill:int = Math.ceil(Math.random()*3); 
      var myMaxBots:int = mySkill +10; 
      for (var i:int = 0; i < myMaxBots; i++){ 
       var thisBot:Bot = new Bot(myBorder, characterMC, mySkill); 
       thisBot.name = "bot" + i; 
       cGameObjs.push(thisBot); 
       addChild(thisBot); 

      } 
     } 

     private function updateScores():void{ 
      scoreDisplay.text = String(pScore); 
      itemsDisplay.text = String(pItems); 
      livesDisplay.text = String(pLives); 
      msgDisplay.text = "Orc Invasion"; 

     } 

     public function updateLives(myBot:MovieClip):void{ 
      trace("update lives"); 
      pLives--; 
      pScore -= myBot.getPts(); 
      var myIndex:int = cGameObjs.indexOf(myBot); 
      cGameObjs.splice(myIndex, 1); 
      if (pLives > 0){ 
       updateScores(); 

      } 

      else{ 
       gameOver(false); 
      } 
     } 

     public function updateItems(myItem:MovieClip):void{ 
     trace("update items"); 
     pItems--; 
     pScore += myItem.getPts(); 
     var myIndex:int = cGameObjs.indexOf(myItem); 
     cGameObjs.splice(myIndex, 1); 
     if (pItems > 0){ 
      updateScores(); 
     } 

     else{ 
      gameOver(true); 
     } 

    } 

     private function gameOver(bool:Boolean):void{ 
      trace("Game over dawg"); 
      updateScores(); 
      if(bool){ 
       msgDisplay.text = "Good job buddy"; 
      } 
      else{ 
       msgDisplay.text = "You suck dawg"; 
      } 
      removeLeftovers(); 
     } 

     private function resetGame():void{ 
      playAgainBtn.visible = false; 
      playAgainBtn.removeEventListener(MouseEvent.CLICK,playAgain); 
      cGameObjs = new Array(); 
      addCharacter(); 
      addItems(); 
      addBots(); 

      pLives = 5 - pSkill; 
      pScore = 0; 

      pItems = pSkill + 5; 
      updateScores(); 
     } 

     private function playAgain(evt:MouseEvent):void{ 
      resetGame(); 
     } 

     private function removeLeftovers():void{ 
      trace("Removing leftover items and bots"); 
      for each(var myObj in cGameObjs){ 
       myObj.hasHitMe(); 
       myObj = null; 
      } 
      playAgainBtn.visible = true; 
      playAgainBtn.addEventListener(MouseEvent.CLICK, playAgain); 
     } 





    } 

} 

而这正是我试图在其他3类之一中访问该功能的类:

package { 
    import flash.display.MovieClip; 
    import flash.events.*; 
    import ZombieBots; 

    public class Item extends MovieClip{ 
     private var cNumItem:int; 
     private var cNumPts:int; 
     private var characterMC:MovieClip; 
     private var ZombieBot:ZombieBots; 

     public function Item(myZB:ZombieBots, myChar:MovieClip, mySkill:int=1) { 
      ZombieBot = myZB; 
      cNumItem = Math.ceil(Math.random() * (mySkill * 3 + 1)); 
      characterMC = myChar; 
      this.addEventListener(Event.ADDED_TO_STAGE,initItem); 
      addEventListener(Event.ENTER_FRAME, checkCollision); 
     } 


     private function initItem(evt:Event):void{ 

      this.gotoAndStop(cNumItem); 
      cNumPts = cNumItem * 25; 
      setPosition(); 
      this.removeEventListener(Event.ADDED_TO_STAGE,initItem); 
     } 

     private function setPosition():void{ 

      this.x = (Math.ceil(Math.random() * 10)*50); 
      this.y = (Math.ceil(Math.random()*10)*35); 

     } 

     private function checkCollision(evt:Event){ 
      if (characterMC.hitTestObject(this)){ 

       ZombieBot.updateItems(this); 
       hasHitMe(); 
      } 
     } 

     public function hasHitMe():void{ 
      trace("remove"); 
      removeEventListener(Event.ENTER_FRAME, checkCollision); 
      this.parent.removeChild(this); 
     } 

     public function getPts():int{ 
      return cNumPts; 
     } 
    } 

} 

谁能帮助?

+0

myItem.getPts();你在Item类 –

+0

@Bolzano中没有类似getPts()的方法,整个类都是200行。我正在提供一个问题的例子。 – rakeshisu

+0

是的,但你必须添加你的函数或方法或在主代码中使用的类的定义...否则我们怎么能找到你的错误。它显然是你打电话的东西,但它没有被定义为功能。 –

回答

1

updateItems不是MovieClip方法。这是您的ZombieBots课程的一种方法。

将您的ZombieBots实例(我假设是root)作为MovieClip投射出来,只会允许您使用它继承的类方法或方法。

尝试这种情况:

var zombieBotsInstance:ZombieBots = root as ZombieBots; 
zombieBotsInstance.updateItems(this); 
+0

Woops,这是我的错误。使用正确的代码编辑原始文章。 – rakeshisu

+0

我试过了,但现在我在zombieBotsInstance.updateItems(this)这一行上得到了null对象引用。 我有一种感觉,试图以这种方式访问​​根是问题,但我不知道如何解决它。 – rakeshisu

+0

发布您的更新代码。我在这里做一些假设。什么是根?它是一个ZombieBots实例 - 即文档类? – prototypical

相关问题