actionscript-3flashaudiotypeerrorsoundchannel

AS3 - Yet another TypeError: Error #1009 Cannot acces


I am creating a "Space Invaders" game in AS3. Got everything working now, but keep ketting a #1009 error and i'm looking at it for quite a time now and i just don't see the problem. Some extra eyes could help out I think!

This is what the debugger says:

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()

Below I will paste the code that has to do with the error:

1: Creating the AS Linkage names:

//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: [Line 117] The AS line 117 is the highlighted one:

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

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

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

3: [Line 338] The function enemyHitTest(); where it starts the winGame(); function:

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: [Line 352] Where the winGame(); function run's after getting 660 points at the enemyHit.

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
}

So as you can see, it run's through these functions. I've already checked if something is wrong with my file in the Library, but the AS Linkage name is exactly the same as the var I defined above. Maybe a few extra eyes can see what's going wrong here, and explain me why..

Thanks in advance!


Solution

  • As per the livedocs :

    winFX.play() method may return null if you have no sound card or if you run out of available sound channels. The maximum number of sound channels available at once is 32.

    Check if either of the issues above is applicable to you....


    As Mark said, the class winGameSound is the culprit here. The call winFX.play() returns a null, not a sound channel. So you can not apply a sound transform to the null object.

    The only info one can currently get is that the class inherits Sound class & returns null with play() call.