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

时间:2015-03-24 07:03:46

标签: actionscript-3 flash actionscript flash-cs6

我的as代码存在一些问题,如下所示,每当我的项目点击相机时,它都会弹出:

  

TypeError:错误#1009:无法访问null的属性或方法   对象参考。在Prototype1 / hitCamera1()

     

TypeError:错误#1009:无法访问null的属性或方法   对象参考。在Prototype1 / hitCamera2()

     

TypeError:错误#1009:无法访问null的属性或方法   对象参考。在Prototype1 / hitCamera3()

更新#1:我意识到错误在于“channel3.stop();”每个命中相机的代码。为什么会出现错误?

stop();
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;

import flash.media.Sound;
import flash.media.SoundChannel;

var leftArrow:Boolean = false;
var rightArrow:Boolean = false;
var upArrow:Boolean = false;
var downArrow:Boolean = false;
var Test_hitting:Boolean = false;

var backSound2:VolatileReaction =  new VolatileReaction();
var channel3:SoundChannel = backSound2.play();


stage.removeEventListener(KeyboardEvent.KEY_DOWN, keydown);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyup);
stage.removeEventListener(Event.ENTER_FRAME, gameloop);

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
stage.addEventListener(Event.ENTER_FRAME, movePlayer);
stage.addEventListener(Event.ENTER_FRAME, hitCamera1);
stage.addEventListener(Event.ENTER_FRAME, hitCamera2);
stage.addEventListener(Event.ENTER_FRAME, hitCamera3);

function keyPressedDown(event:KeyboardEvent)
{
if(event.keyCode == 37)
{
    leftArrow = true;
}else if (event.keyCode == 39)
{
    rightArrow = true;
}else if(event.keyCode == 38)
{
    upArrow = true;
   }else if (event.keyCode == 40)
{
    downArrow = true;
}
}

function keyPressedUp (event:KeyboardEvent)
{
if(event.keyCode == 37)
{
    leftArrow = false;
}else if (event.keyCode == 39)
{
    rightArrow = false;
}else if (event.keyCode == 38)
{
    upArrow = false;
}else if(event.keyCode == 40)
{
    downArrow =false;
}
}

 function hitCamera1(event:Event)
 {
 if(camera1.cameralight.hitTestPoint(Player.x - Player.width/2, Player.y - Player.width/2, true))
 {
     channel3.stop();
             gotoAndStop("gameover");

 }
 }

 function hitCamera2(event:Event)
  {
 if(camera2.cameralight.hitTestPoint(Player.x - Player.width/2, Player.y - Player.width/2, true))

 {
    channel3.stop();
     gotoAndStop("gameover");
 }
}

 function hitCamera3(event:Event)
{
if(camera3.cameralight.hitTestPoint(Player.x - Player.width/2, Player.y - Player.width/2, true))

 {
    channel3.stop();
     gotoAndStop("gameover");
 }
 }


 function movePlayer(event:Event)
{
var speed:Number = 3;

if(leftArrow)
{
    Player.x -=speed;
    if(Player.hitTestObject(wall1) || Player.hitTestObject(mainwallLeft)|| Player.hitTestObject(wall2) || Player.hitTestObject(wall3))
    {
        Player.x +=speed;

    }
    if(Player.hitTestObject(exit))
       {
           channel3.stop();
           gotoAndStop("secondStage");
       }


}
if(rightArrow)
{
    Player.x += speed;
    if(Player.hitTestObject(wall1) || Player.hitTestObject(mainwallRight)|| Player.hitTestObject(wall2) || Player.hitTestObject(wall3))
    {
        Player.x -=speed;
    }
    if(Player.hitTestObject(exit))
       {
          channel3.stop();
           gotoAndStop("secondStage");
       }


}
if(upArrow)
{
    Player.y -= speed;
    if(Player.hitTestObject(wall1) || Player.hitTestObject(mainwallUp)|| Player.hitTestObject(wall2) || Player.hitTestObject(wall3))
    {
        Player.y += speed;
    }
    if(Player.hitTestObject(exit))
       {
          channel3.stop();
           gotoAndStop("secondStage");
       }


}
if(downArrow)
{
    Player.y +=speed;
    if(Player.hitTestObject(wall1) || Player.hitTestObject(mainwallDown) || Player.hitTestObject(wall2) || Player.hitTestObject(wall3))
    {
        Player.y-=speed;
    }
    if(Player.hitTestObject(exit))
       {
         channel3.stop();
           gotoAndStop("secondStage");
       }

}


 }

1 个答案:

答案 0 :(得分:0)

因为您在EnterFrame上调用了HitCamera函数,所以它可能是" channel3"是空的。

尝试使用HitCamera1,2和3

// instead of 
channel3.stop();

// use
if( channel3 ) channel3.stop();