0

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

当我尝试从其他 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;
        }
    }

}

谁能帮忙?

4

1 回答 1

1

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

将您的 ZombieBots 实例(我假设它是根)转换为 MovieClip,只允许您使用它的类方法或它继承的方法。

尝试这个 :

var zombieBotsInstance:ZombieBots = root as ZombieBots;
zombieBotsInstance.updateItems(this);
于 2013-10-26T18:08:08.837 回答