【问题标题】:AS3 - Yet another TypeError: Error #1009 Cannot accesAS3 - 又一个 TypeError:错误 #1009 无法访问
【发布时间】:2012-10-07 02:57:39
【问题描述】:

我正在 AS3 中创建一个“太空入侵者”游戏。现在一切正常,但不断出现 #1009 错误,我已经查看了很长时间,但我只是没有看到问题所在。我认为多加一双眼睛会有所帮助!

调试器是这样说的:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at testbestandkopie_fla::MainTimeline/winGame()[testbestandkopie_fla.MainTimeline::frame1:352]
at testbestandkopie_fla::MainTimeline/enemyHitTest()[testbestandkopie_fla.MainTimeline::frame1:338]
at testbestandkopie_fla::MainTimeline/onTick()[testbestandkopie_fla.MainTimeline::frame1:117]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()

下面我将粘贴与错误有关的代码:

1:创建 AS 链接名称:

//Sound FX-------------------------------------------------------------------------
var startFX:Sound = new startGameSound();
var laserFX:Sound = new laserSound();
var explosionFX:Sound = new explosionSound();
var musicFX:Sound = new backgroundMusic();
var loseFX:Sound = new endGameSound();
var winFX:Sound = new winGameSound();
var SfxTransform = new SoundTransform();
var myChannelMisc:SoundChannel = new SoundChannel();
var myChannelMusic:SoundChannel = new SoundChannel();
var myChannelWin:SoundChannel = new SoundChannel();
var myChannelLose:SoundChannel = new SoundChannel();
//---------------------------------------------------------------------------------

2: [第 117 行] AS 第 117 行是突出显示的:

//Handlers Functions---------------------------------------------------------------
function onTick(e:TimerEvent) { //A continuous run of some functions below.
moveCharacter();
moveEnemyField(); 

playerCollisionDetectWall();
enemyCollisionDetectWall();
enemyCollisionDetectWallBottom();

moveLasers();
enemyHitTest(); <---- line 117
}

3: [第 338 行] 函数enemyHitTest(); winGame() 开始的地方;功能:

function enemyHitTest() {
//For each of the three enemys
for (var i:int = 0; i < enemyArray.length; i++) {
    //the each of the six lasers
    for (var j:int = 0; j < 6; j++) {
        //don't consider lasers that aren't in play:
        if (laserArray[j].y > SpelerMC.y) continue;
        if (enemyArray[i].visible &&     enemyArray[i].hitTestObject(laserArray[j])) {
            score += 10;
            myChannelMisc = explosionFX.play();
            SfxTransform.volume = 0.3;
            myChannelMisc.soundTransform = SfxTransform;
            scoreTxt.text = score.toString();
            trace("Invader nummer " + i + " neergeschoten!");
            enemyArray[i].visible = false;
            //Now we remove the laser when hitting.
            laserArray[j].x = j * 70 + 100; 
            laserArray[j].y = 895;
        }
        else if(score == 660) { 
        //If you reach a score of 660 (66 enemy's x 10 = 660) you win the game.
            winGame(); <---- Line 338
        }
    }
}
}

4: [第 352 行] winGame();在敌人命中获得 660 分后运行函数。

function winGame() {
winScreen.visible = true;
gameTimer.stop();
//Stop the music.
myChannelMusic.stop();
//Start the "You Win" music.
myChannelWin = winFX.play();
SfxTransform.volume = 0.02;
myChannelWin.soundTransform = SfxTransform; <---- line 352
}

如您所见,它通过这些函数运行。我已经检查了我在库中的文件是否有问题,但是 AS Linkage 名称与我在上面定义的 var 完全相同。也许多出几眼就能看出这里出了什么问题,给我解释一下为什么..

提前致谢!

【问题讨论】:

  • 从您的代码提取和 cmets 中可以明显看出,当您尝试设置 myChannelWin.soundTransform 时,myChannelWin 为空。它上面的两行设置为myChannelWin = winFX.play(); 那个方法play()返回的不是null吗?
  • 你是什么意思? 'myChannelWin' 与我认为的 'myChannelWin.soundTransform' 不同?此外,我对丢失声音“myChannelLose”/“myChannelLose.soundTransform”有完全相同的行,并且与我在游戏中的所有其他声音一样,它没有错误。如果我排除了给出错误的那个,那么其他的仍然可以正常工作..

标签: actionscript-3 flash audio typeerror soundchannel


【解决方案1】:

根据livedocs

winFX.play() 方法可能会在您没有声卡或没有可用的声道时返回 null。一次可用的最大声道数为 32。

检查上述任一问题是否适用于您....


正如 Mark 所说,类 winGameSound 是这里的罪魁祸首。 winFX.play() 调用返回空值,而不是声道。所以你不能对空对象应用声音变换。

目前可以得到的唯一信息是该类继承了 Sound 类并通过 play() 调用返回 null。

【讨论】:

  • 感谢您的回答。我正在使用 15" MacBook Pro '11。我在这里定义的所有声音都可以正常工作。所以我认为这不是声卡。整个游戏正常工作,但我不断收到此错误。如您所见,我'正在使用 4 个不同的频道。所以我真的不知道这个 TypeError 是如何存在的..
  • 他不是这么说的。你拥有什么样的电脑并不重要。您总是需要跟踪 myChannelWin(或 SfxTransform),您会看到它为空。然后你可以包装一个条件“if (myChannelWin){}”。不去trace或者debug,什么问题都看不懂。
  • 感谢各位大佬的讲解,问题解决了!这成功了:我删除了“myChannelWin.soundTransform = SfxTransform;”行并制作了这两行:'myChannelWin = winFX.play(1,0,SfxTransform);' 'SfxTransform.volume = 0.05;'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-31
  • 1970-01-01
  • 2014-04-13
  • 1970-01-01
  • 2015-05-27
  • 2013-01-29
相关资源
最近更新 更多