AS3 - 错误#1009:无法访问空对象引用的属性或方法

时间:2013-05-06 17:37:27

标签: actionscript-3 null

我遇到了这个问题:

  

错误#1009:无法访问null对象的属性或方法   参考

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at spyhookerproject_fla::MainTimeline/mapGen()
at spyhookerproject_fla::MainTimeline/startGame()
at spyhookerproject_fla::MainTimeline/gameLoop()

因此错误发生在mapGen()的某处,我知道的很多,我不知道如何解决它。

注意:MP01,MP02 ...没有设置AS链接,因为我无法通过AS Linkage设置操纵可见性。

我遇到了与removeMuzzle();

相同的问题

继承守则:

import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.text.engine.SpaceJustifier;
import flashx.textLayout.operations.MoveChildrenOperation;

/*----------------------------Main VARs-----------------------------*/
var STATE_START:String="STATE_START";
var STATE_START_PLAYER:String="STATE_START_PLAYER";
var STATE_PLAY:String="STATE_PLAY";
var STATE_END:String="STATE_END";
var gameState:String;

var player:MovieClip;
var playerSpeed:Number;
var speedLimit:Number;
var speedLimitInc:Number;
var map:Array;
var bullets:Array;
//holds civil vehicles
var civil:Array; 
//holds enemy vehicles
var enemy:Array; 
var explosions:Array;

var BBullet:MovieClip;
var FireBTN:MovieClip;
var muzzle:MovieClip;
var muzzleArray:Array;

//maps
//var mp1:MP00;
var mp3:MovieClip;
var mp1:Array;
/*
var MP_01:MovieClip;
var MP_02:MovieClip;
var MP_03:MovieClip; 
*/

//sets the bullet type and properties
var BType = "BBasic";
var BProp:Array;

//bullet preperties by type

var BBasic:Array = new Array(1, 1, 100, 50, 0, new BBasicA());

/**
ARRAY SETTING
0 = bullet position (middle , back, sides etc...)
    1-middle front
    2-left side front
    3-right side front
    4-left and right side middle
    5-back
    7-left and right side wheels

1 = bullet direction
    1-forward
    2-back
    3-sides

2 = fire speed (in millisecounds so 1000 = 1sec)

3 = speed of movement

4 = damage 10-100

5 = name of the firing animation in library

6 = name of launch animation in library

7 = name of impact animation in library 
**/

var level:Number;

//BCivil speed and randomness
var BCSpeed:Number = 3;
var BCRand:Number = 120;

/*------------------------------Setup-------------------------------*/


introScreen.visible = false;
loadingScreen.visible = false;
gameScreen.visible = false;
endScreen.visible = false;
//map visibility
MP00.visible = false;
MP01.visible = false;
MP02.visible = false;
//MpRSimple.visible = false;

/*---------------------------Intro screen--------------------------*/


/*-----------------------------mainScreen---------------------------*/

mainScreen.play_btn.addEventListener(MouseEvent.CLICK, clickAway);
function clickAway(event:MouseEvent):void
{
    gameStart();
    }

function gameStart():void
{
    //Move main screen from stage
    mainScreen.visible = false;

    //Begin loading the game
    gameState = STATE_START;
    trace (gameState);
    addEventListener(Event.ENTER_FRAME, gameLoop);
    }

/*----------------------------gameLoop-----------------------------*/

function gameLoop(e:Event):void
{
    switch(gameState)
    {
        case STATE_START:
            startGame();
            break;

        case STATE_START_PLAYER:
            startPlayer();
            break;

        case STATE_PLAY:
            playGame();
            break;

        case STATE_END:
            endGame();
            break;

        }
    }
/*-_________________________-Game STATES-_________________________-*/

/*---------------------------STATE_START---------------------------*/

function startGame():void
{

    level = 1; //setting level for enemies
    playerSpeed = 1;
    speedLimit = 10;
    speedLimitInc = 1;

    //Graphics
        //player icon
    player = new Player(); 
        //add bullet holder
    bullets = new Array();
        //basicCivil icon
    civil = new Array(); 
        //basicEnemy icon
    enemy = new Array(); 
        //holds explosions
    explosions = new Array(); 

    //mapGen();

    FireBTN = new fireBTN();
    FireBTN.y = stage.stageHeight - FireBTN.height;
    FireBTN.x = stage.stageWidth - FireBTN.width;

    //map
    mp3 = MP00;

    //Load map parts

    //End startGame
    gameState = STATE_START_PLAYER;
    trace(gameState);
    }

/*------------------------STATE_START_PLAYER-----------------------*/

function startPlayer():void
{
    //start the player

    //set possition of player
    player.y = stage.stageHeight - player.height;
    addChild(player);
    addEventListener(Event.ENTER_FRAME, movePlayer);

    //changing screens

    //start game
    gameState = STATE_PLAY;
    trace(gameState);

    }

//player controll
function movePlayer(e:Event):void 
{
    //mouse\touch recognition
    player.x = stage.mouseX;
    player.y = stage.mouseY;

    //player does not move out of the stage
    /*
    if (player.x < 0)
    {
        player.x = 0;
    }
    else if (player.x > (stage.stageWidth - player.width))
    {
        player.x = stage.stageWidth + player.width;
    }*/

    }

//setting bullet type
/*
if (BType == "BBasic")
{
        BProp = BBasic;
    /*case BMissile;
        BProp = BMissile;
        break;
    }*/
//creating bullets
var bulletTimer:Timer = new Timer (100);
bulletTimer.addEventListener(TimerEvent.TIMER, timerListener);
bulletTimer.start();
trace("nja");

function timerListener(e:TimerEvent):void
{
    //need and if statement to determine the bullet speed and travel depended on type of bullet
    var tempBullet:MovieClip = new BBasicA();
    var mF:MovieClip = new BBMF();
    //shoots bullets in the middle
    mF.x = player.x; 
    mF.y = player.y;
    tempBullet.x = player.x; 
    tempBullet.y = player.y - player.height / 2;
    //shooting speed
    tempBullet.speed = 50; 
    bullets.push(tempBullet);
    addChild(tempBullet);
    addChild(mF);
    //bullets movement forward
    for(var i=bullets.length-1; i>=0; i--)
    {
        tempBullet = bullets[i];
        tempBullet.y -= tempBullet.speed;
        }
    }
/*----------------------------STATE_PLAY---------------------------*/

function playGame():void
{
    //gameplay
    speedUp();
    mapMove();
    //fire();
    makeBCivil();
    makeBEnemy();
    moveBCivil();
    moveBEnemy();
    vhDrops();
    testCollision();
    testForEnd();
    removeExEplosions();
    removeMuzzle();
    }

function speedUp():void
{

    //making speed limit
    if (playerSpeed < speedLimit)
    {
        playerSpeed ++;
        }
    //increasing speed limit
    if (speedLimitInc == 60)
    {
        speedLimit ++;
        speedLimitInc = 1;
        }
    speedLimitInc ++;
    trace("player speed:", playerSpeed);
    trace("speed limit:", speedLimit);
    trace("speed limit inc:", speedLimitInc);
    }

function mapMove(){
    for(var i:int = mp1.length; i>=0; i--)
    {
        var tempMap:MovieClip;
        tempMap = new mp1[i];
        trace ("tempMap", tempMap);
        tempMap.visible = true;
        tempMap.y = tempMap.y + playerSpeed;
        var k:Number = 0;
        setChildIndex(mp3, k);
        }
    trace ("mapMove");
    if(mp3.y >= 0 )
    {
        trace ("mapMove, if");
        mapGen();
        }
    }


function mapGen()
{
    trace ("mapGen");
    var mapRand:Number = Math.floor(Math.random()*3);
    trace ("mapRand:", mapRand);
    switch (mapRand)
    {
        case 0:
            mp3 = MP00;
            break;

        case 1:
            mp3 = MP01;
            break;

        case 2:
            mp3 = MP02;
            break;
        }
    mp3.x = stage.stageWidth / 2;
    mp3.y = stage.stageHeight - 1000;
    addChild(mp3);
    mp1.push(mp3);
    }

function makeBCivil():void
{
    //random chance
    var chance:Number = Math.floor(Math.random()*BCRand);
    if (chance <= 1 + level) 
    {
        var tempBCivil:MovieClip;
        //generate enemies
        tempBCivil = new BCivil();
        tempBCivil.speed = BCSpeed + playerSpeed;
        tempBCivil.x = Math.round(Math.random()*800);

        addChild(tempBCivil);
        civil.push(tempBCivil);
        }
    }

function moveBCivil():void
{
    //move enemies
    var tempBCivil:MovieClip;
    for(var i:int = civil.length-1; i>=0; i--)
    {
        tempBCivil = civil[i];
        tempBCivil.y += tempBCivil.speed
        }

    //testion colision with the player and screen out
    if (tempBCivil.y > stage.stageHeight /* || tempBCivil.hitTestObject(player) */)
    {
        trace("ds hit");
        //makeExplosion (player.x, player.y);
        removeCivil(i);
        //gameState = STATE_END;
        }
    }

//Test bullet colision
function testCollision():void
{
    var tempBCivil:MovieClip;
    var tempBEnemy:MovieClip;
    var tempBullet:MovieClip;

    //civil/bullet colision
    civils:for(var i:int=civil.length-1; i>=0; i--)
    {
        tempBCivil = civil[i];
        for (var j:int=bullets.length-1; j>=0; j--)
        {
            tempBullet = bullets[j];
            if (tempBCivil.hitTestObject(tempBullet))
            {
                trace("laser hit the civil");
                makeExplosion (tempBCivil.x, tempBCivil.y);
                removeCivil(i);
                removeBullet(j);
                break civils;
                }
            }
        }

    //enemy/bullet colision
    enemy:for(var k:int=enemy.length-1; k>=0; k--)
    {
        tempBEnemy = enemy[k];
        for (var l:int=bullets.length-1; l>=0; l--)
        {
            tempBullet = bullets[l];
            if (tempBEnemy.hitTestObject(tempBullet))
            {
                trace("bullet hit the Enemy");
                makeExplosion (tempBEnemy.x, tempBEnemy.y);
                removeEnemy(k);
                removeBullet(l);
                break enemy;
                }
            }
        }
    }

function makeExplosion(ex:Number, ey:Number):void
{
    var tempExplosion:MovieClip;
    tempExplosion = new boom();
    tempExplosion.x = ex;
    tempExplosion.y = ey;
    addChild(tempExplosion)
    explosions.push(tempExplosion);
    }

function makeBEnemy():void
{
    //random chance
    var chance:Number = Math.floor(Math.random()*BCRand);
    if (chance <= 1 + level) 
    {
        var tempBEnemy:MovieClip;
        //generate enemies
        tempBEnemy = new BEnemy();
        tempBEnemy.speed = BCSpeed + playerSpeed;
        tempBEnemy.x = Math.round(Math.random()*800);

        addChild(tempBEnemy);
        enemy.push(tempBEnemy);
        }

    }

function moveBEnemy():void
{
    //move enemies
    var tempBEnemy:MovieClip;
    for(var i:int = enemy.length-1; i>=0; i--)
    {
        tempBEnemy = enemy[i];
        tempBEnemy.y += tempBEnemy.speed
        }

    //testion colision with the player and screen out
    if (tempBEnemy.y > stage.stageHeight /* || tempBCivil.hitTestObject(player) */)
    {
        trace("enemy");
        //makeExplosion (player.x, player.y);
        removeEnemy(i);
        //gameState = STATE_END;
        }
    }

function vhDrops():void
{}

function testForEnd():void
{
    //check damage


    //end game
    //gameState = STATE_END;
    trace(gameState);
    }

/*--------------------REMOVING BS FROM STAGE-----------------------*/
//removing civils
function removeCivil(idx:int):void 
{
    if(idx >= 0)
    {
        removeChild(civil[idx]);
        civil.splice(idx, 1);
        }
    }

//removing enemies
function removeEnemy(idx:int):void 
{
    if(idx >= 0)
    {
        removeChild(enemy[idx]);
        enemy.splice(idx, 1);
        }
    }

//removing civils
function removeBullet(idx:int):void 
{
    if(idx >= 0)
    {
        removeChild(bullets[idx]);
        bullets.splice(idx, 1);
        }
    }

//removing expired explosions
function removeExEplosions():void 
{
    var tempExplosion:MovieClip;
    for(var i=explosions.length-1; i>=0; i--)
    {
        tempExplosion = explosions[i];
        if (tempExplosion.currentFrame >= tempExplosion.totalFrames)
        {
            removeExplosion(i);
            }
        }
    }

//removing civils
function removeExplosion(idx:int):void 
{
    if(idx >= 0)
    {
        removeChild(explosions[idx]);
        explosions.splice(idx, 1);
        }
    }

function removeMuzzle():void
{
    var mF:MovieClip;
    for(var i=muzzleArray.length-1; i>=0; i--)
    {
        mF = muzzleArray[i];
        if (mF.currentFrame >= mF.totalFrames)
        {
            removeChild(muzzleArray[i]);
            }
        }
    }

/*--------------------------STATE_END------------------------------*/

function endGame():void
{

    }


/*gameScreen*/

/*endScreen*/

注意:代码不在接近完成的地方,并且doesent包含所有内容,有些东西是占位符。

提前致谢!

编辑: 将其添加到mapMove();

if(mp1 == null)
    {
        trace ("mp1 null, MAPGEN");
        mapGen();
        }

编辑:突出显示错误

1 个答案:

答案 0 :(得分:1)

只关注您的MapMoveMapGen功能:

似乎MP00 / MP01 / MP02是某种显示对象,您将它们存储在名为mp1的数组中。

在MapMove功能中,你有这一行:

tmpMap = new mp1[i];

这会引发错误,因为数组mp1不存储类名,而是实例化显示对象。

如果删除new关键字,那么它只会将数组中的对象分配给tmpMap var。

这可能不是您唯一的问题,但这肯定是您需要解决的问题。


其他问题:

你的mp1数组永远不会被实例化:你需要在mp1 = new Array();工作之前mp1.push

你的for循环为i创建了一个超出范围的值:

将此更改为for(var i:int = mp1.length; i>=0; i--)

for(var i:int = mp1.length - 1; i>=0; i--)