匹配游戏延迟错误#2007

时间:2017-03-08 18:47:40

标签: actionscript-3

我正在使用我在Google上找到的教程 - 效果很好。但是,我有一些问题要让它按照我的意愿运作。我试着让第二张卡片在短时间内挂起,所以当有比赛时,小孩(球员)可以看到比赛。不幸的是,我的代码给了我TypeError:错误#2007:我的匹配名称中的参数child必须是非null运行时错误,当我在删除该组之前点击一张新卡时,匹配的一对被转回并且新点击的卡已移除。 (在其自己的)。我很感激任何帮助...

这是错误,然后是我的代码:

  TypeError: Error : Parameter child must be non-null.
        at flash.display::DisplayObjectContainer/removeChild()
        at MatchingGameObject9/remove_tiles()
        at flash.events::EventDispatcher/dispatchEventFunction()
        at flash.events::EventDispatcher/dispatchEvent()
        at flash.utils::Timer/tick()



      package {
        import flash.display.*;
        import flash.events.*;
        import flash.text.*;
        import flash.utils.getTimer;
        import flash.utils.Timer;


    public class MatchingGameObject9 extends MovieClip {
        // game constants
        private static const boardWidth:uint = 6;
        private static const boardHeight:uint = 5;
        private static const cardHorizontalSpacing:Number = 87;
        private static const cardVerticalSpacing:Number = 87;
        private static const boardOffsetX:Number = 20;
        private static const boardOffsetY:Number = 45;
        private static const pointsForMatch:int = 100;
        private static const pointsForMiss:int = -5;

        private var firstCard:Card;
        private var secondCard:Card;
        private var cardsLeft:uint;
        private var gameScore:int;
        private var gameStartTime:uint;
        private var gameTime:uint;
        private var pause_timer:Timer;
        private var gameScoreField:TextField;
        private var gameTimeField:TextField;

        public function MatchingGameObject9():void {
            // make a list of card numbers
            var cardlist:Array = new Array();
            for(var i:uint=0;i<boardWidth*boardHeight;i++) {
                cardlist.push(i);

            }

            // create all the cards, position them, and assign a randomcard face to each
            cardsLeft = 0;
            for(var x:uint=0;x<boardWidth;x++) { // horizontal
                for(var y:uint=0;y<boardHeight;y++) { // vertical
                    var c:Card = new Card(); // copy the movie clip
                    c.stop(); // stop on first frame
                    c.x = x*cardHorizontalSpacing+boardOffsetX; // set position
                    c.y = y*cardVerticalSpacing+boardOffsetY;
                    var r:uint = Math.floor(Math.random()*cardlist.length); // get a random face
                    c.cardface = cardlist[r]; // assign face to card
                    cardlist.splice(r,1); // remove face from list
                    c.addEventListener(MouseEvent.CLICK,clickCard); // have it listen for clicks
                    c.buttonMode = true;
                    addChild(c); // show the card
                    cardsLeft++;
                }
            }

            gameScoreField = new TextField();
            addChild(gameScoreField);
            gameScore = 0;
            showGameScore();

            gameTimeField = new TextField();
            gameTimeField.x = 450;
            addChild(gameTimeField);
            gameStartTime = getTimer();
            gameTime = 0;
            addEventListener(Event.ENTER_FRAME,showTime);
        }

        // player clicked on a card
        public function clickCard(event:MouseEvent) {
            var thisCard:Card = (event.target as Card); // what card?

            if (firstCard == null) { // first card in a pair
                firstCard = thisCard; // note it
                firstCard.gotoAndStop(thisCard.cardface+2); // turn it over

            } else if (firstCard == thisCard) { // clicked first card again
                firstCard.gotoAndStop(1); // turn back over
                firstCard = null;

            } else if (secondCard == null) { // second card in a pair
                secondCard = thisCard; // note it
                secondCard.gotoAndStop(thisCard.cardface+2); // turn it over

                // compare two cards
                if (Math.floor(firstCard.cardface/2) ==   Math.floor(secondCard.cardface/2)) {
                    **pause_timer = new Timer(2000,1);
                     pause_timer.addEventListener(TimerEvent.TIMER_COMPLETE,remove_tiles);
                    pause_timer.start();**
                }

            } else { // starting to pick another pair
                // reset previous pair
                firstCard.gotoAndStop(1);
                secondCard.gotoAndStop(1);
                secondCard = null;
                // select first card in next pair
                firstCard = thisCard;
                firstCard.gotoAndStop(thisCard.cardface+2);
            }
        }

        public function showGameScore() {
            gameScoreField.text = "ΣΚΟΡ: "+String(gameScore);
        }

        public function showTime(event:Event) {
            gameTime = getTimer()-gameStartTime;
            gameTimeField.text = "ΧΡΟΝΟΣ: "+clockTime(gameTime);
        }

        public function clockTime(ms:int) {
            var seconds:int = Math.floor(ms/1000);
            var minutes:int = Math.floor(seconds/60);
            seconds -= minutes*60;
            var timeString:String = minutes+":"+String(seconds+100).substr(1,2);
            return timeString;
        }
        **public function remove_tiles(event:TimerEvent) {
            removeChild(firstCard);
                    removeChild(secondCard);
                    // reset selection
                    firstCard = null;
                    secondCard = null;
                    // add points
                    gameScore += pointsForMatch;
                    showGameScore();
                    // check for game over
                    cardsLeft -= 2; // 2 less cards
                    if (cardsLeft == 0) {
                        MovieClip(root).gameScore = gameScore;
                        MovieClip(root).gameTime = clockTime(gameTime);
                        MovieClip(root).gotoAndStop("gameover");
                       pause_timer.removeEventListener(TimerEvent.TIMER_COMPLETE,remove_tiles);
                        } else {
                        gameScore += pointsForMiss;
                        showGameScore();
                }**
    }
}

1 个答案:

答案 0 :(得分:2)

我认为您需要以不同方式组织删除。类似的东西:

var removalQueue:Array = new Array;

addEventListener(Event.ENTER_FRAME, onRemove);

function onRemove(e:Event):void
{
    if (!removalQueue.length) return;

    var anEntry:Object = removalQueue[0];

    if (getTimer() < anEntry['time']) return;

    // Delete entry from queue.
    removalQueue.shift();

    removeChild(anEntry['first']);
    removeChild(anEntry['second']);

    gameScore += anEntry['score'];
    cardsLeft -= 2;

    // The rest of your code...
}

因此,如果您有两张匹配的卡片,则只需将适当的条目推送到队列:

var anEntry:Object =
{
    "time": getTimer() + 2000,
    "first": firstCard,
    "second": secondCard,
    "score": pointsForMatch
}

removalQueue.push(anEntry);