闪存中的定时器相关错误,TypeError#1009

时间:2013-10-24 14:39:53

标签: actionscript-3 flash timer typeerror flash-cs6

我在我的flash应用程序中使用了一个计时器,我在下面有这个特殊的错误:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
        at FlashGame_fla::MainTimeline/toTimeCode()
        at FlashGame_fla::MainTimeline/timerTickHandler()
        at flash.utils::Timer/_timerDispatch()
        at flash.utils::Timer/tick()

以下是此特定Flash游戏应用程序的代码。这是一个玩家在特定时间内收集尽可能多的物品的游戏:

import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.geom.Rectangle;

menuButton.addEventListener(MouseEvent.CLICK, evilToMenu);
rulesButton.addEventListener(MouseEvent.CLICK, toggleRule);
gameRules.addEventListener(MouseEvent.CLICK, toggleRule);
gameRules.visible = false;
gameRules.buttonMode = true;

evilGameOverMC.visible = false;
evilWinLose.visible = false;
playAgainBtn.visible = false;
toMenuBtn.visible = false;

var pLives:int = 3;
var pEvilScore:int = 0;
var pItems:int = 10;
var daveMC:MovieClip;
var cGameObjs:Array = new Array();
var timer:Timer = new Timer(100, 300);
timer.start();
timer.addEventListener(TimerEvent.TIMER, timerTickHandler);
var timerCount:int = 15000;
//var cPlayerData:Object;
//var cSavedGameData:SharedObject;

addCharacter();
addBots();
addItems();

scoreDisplay.text = "" + pEvilScore;
livesDisplay.text = pLives + " lives";

function evilToMenu(Event:MouseEvent):void
{
    removeLeftovers();
    removeChild(daveMC);
    timer.stop();
    gotoAndStop("menu");
}

function timerTickHandler(Event:TimerEvent):void 
{
    timerCount -= 100;
    toTimeCode(timerCount);
    if (timerCount <= 0) 
    { 
        gameOver(false);
    }
}

function toTimeCode(milliseconds:int): void 
{
    //creating a date object using the elapsed milliseconds
    var time:Date = new Date(milliseconds);

    //define minutes/seconds/mseconds
    var minutes:String = String(time.minutes);
    var seconds:String = String(time.seconds);
    var miliseconds:String = String(Math.round(time.milliseconds)/100);

    //add zero if neccecary, for example: 2:3.5 becomes 02:03.5
    minutes = (minutes.length != 2) ? '0'+minutes : minutes;
    seconds = (seconds.length != 2) ? '0'+seconds : seconds;

    //display elapsed time on in a textfield on stage
    timerDisplay.text = minutes + ":" + seconds;

}

function addCharacter():void
{
    trace("Adding the character, Dave")
    //set the initial values 
    var myBorder:Rectangle = new Rectangle(355,145,395,285);
    var myXY:Array = [355,430];
    var myChar:int = Math.ceil(Math.random() * 3);
    var myKeys:Array = [37,39,38,40]; 
    var myDistance:int = 7; 
    // create and add a new player object to the stage 
    daveMC = new Character(myBorder,myXY,myKeys,myChar,myDistance); 
    addChild(daveMC);
}

function addBots():void 
{
    trace("Adding the bots..");
    // set the initial values (adapt to suit your game) 
    var myBorder:Rectangle = new Rectangle(355,145,395,285); 
    var myMaxBots:int = 5;// simulation
    // add bots one at a time via a loop 
    for (var i:int = 0; i < myMaxBots; i++) 
    { 
        // create a new bot object and name it 
        var thisBot:Bot = new Bot(myBorder, daveMC); 
        thisBot.name = "bot" + i; 
        cGameObjs.push(thisBot);
        // add it to the stage 
        addChild(thisBot); 
     } 
}

function addItems():void
{
    trace("Adding the items..");
    //set the initial values
    for (var i:int = 0; i < 10; i++) 
    { 
        // create a new bot object and name it 
        var thisItem:Item = new Item(daveMC); 
        thisItem.name = "item" + i; 
        cGameObjs.push(thisItem);
        // add it to the stage 
        addChild(thisItem);
    }
}

function updateLives(myBot:MovieClip):void 
{ 
     // update the player's LIVES and score 
     pLives--;
     pEvilScore -= 30;
     var myIndex:int = cGameObjs.indexOf(myBot); 
     cGameObjs.splice(myIndex,1);
     // check for a LOST GAME 
     if (pLives > 0) 
     { 
        updateScores(); 
     } 
     else 
     { 
        gameOver(false); 
     } 
}

function updateItems(myItem:MovieClip):void 
{ 
     // update the player's LIVES and score 
     pItems--;
     pEvilScore += 20;
     var myIndex:int = cGameObjs.indexOf(myItem); 
     cGameObjs.splice(myIndex,1);
     // check for a LOST GAME 
     if (pItems > 0) 
     { 
        updateScores(); 
     } 
     else 
     { 
        gameOver(true); 
     } 
}

function gameOver(myFlag:Boolean):void 
{ 
     updateScores(); 
     if (myFlag) 
     { 
         // player wins 
         msgDisplay.text = "YAY! PAPPLE FOR \nEVERYBODY!";
         removeLeftovers();
         evilWinLose.text = "Weee!! We've got papples for Gru! \nYour Score: " + pEvilScore;
     } 
     else 
     { 
         // player loses 
         msgDisplay.text = "OH NO! NOT \nENOUGH PAPPLES"; 
         removeLeftovers();
         evilWinLose.text = "Boo!! Not enough papples for Gru! \nYour Score: " + pEvilScore;
     } 
     timerDisplay.text = "";
     removeChild(daveMC); 
     evilGameOverMC.visible = true;
     evilWinLose.visible = true;
     toMenuBtn.visible = true;
     playAgainBtn.visible = true;
     toMenuBtn.addEventListener(MouseEvent.CLICK, Click_backtoMain);
     playAgainBtn.addEventListener(MouseEvent.CLICK, backToEvil);
} 

function updateScores():void 
{ 
    scoreDisplay.text = "" + pEvilScore;
    livesDisplay.text = pLives + " lives";
    msgDisplay.text = pItems + " papples more to \ncollect..";
}

function removeLeftovers():void 
{ 
     // check each BOT/ITEM in array 
     for each (var myObj in cGameObjs) 
     { 
         myObj.hasHitMe(); 
         myObj = null; 
     } 
}

function backToEvil(event:MouseEvent):void 
{
    pEvilScore = 0;
    pLives = 3;
    pItems = 3;
    gotoAndStop("menu");
    gotoAndStop("evil");
}

任何人都可以帮我解决这个问题吗?非常感谢! :)

2 个答案:

答案 0 :(得分:0)

用以下代码替换你的toTimeCode函数代码:

function toTimeCode(milliseconds:int): void 
{
    trace(1);

    //creating a date object using the elapsed milliseconds
    var time:Date = new Date(milliseconds);

    trace(2);

    //define minutes/seconds/mseconds
    var minutes:String = String(time.minutes);

    trace(3);

    var seconds:String = String(time.seconds);

    trace(4);

    var miliseconds:String = String(Math.round(time.milliseconds)/100);

    trace(5);

    //add zero if neccecary, for example: 2:3.5 becomes 02:03.5
    minutes = (minutes.length != 2) ? '0'+minutes : minutes;

    trace(6);

    seconds = (seconds.length != 2) ? '0'+seconds : seconds;

    trace(7);

    //display elapsed time on in a textfield on stage
    timerDisplay.text = minutes + ":" + seconds;

    trace(8);
}

答案 1 :(得分:0)

请将timerDisplay行改为此...问题将出在toTimeCode方法中。错误说你正在尝试从var中调用一个方法而不是(还)一个对象......

if(null != timerDisplay)
   timerDisplay.text = minutes + ":" + seconds;

你必须找到一个空的对象!加上这个:

function toTimeCode(milliseconds:int): void 
{
    //creating a date object using the elapsed milliseconds
    var time:Date = new Date(milliseconds);
    trace("Time: " + time);