Actionscript 3 我的ActionScript3游戏中出现奇怪的声音错误

Actionscript 3 我的ActionScript3游戏中出现奇怪的声音错误,actionscript-3,audio,Actionscript 3,Audio,有时我在玩游戏时会出现这种奇怪的错误: TypeError: Error #1009: Cannot access a property or method of a null object reference. at com.efg.framework_mod::SoundManager/playSound()[/Users/xxx/Documents/Developer/AS3_Flex/game_development/projects/xxx/SpaceGame/libs/src/com

有时我在玩游戏时会出现这种奇怪的错误:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.efg.framework_mod::SoundManager/playSound()[/Users/xxx/Documents/Developer/AS3_Flex/game_development/projects/xxx/SpaceGame/libs/src/com/efg/framework_mod/SoundManager.as:106]
at com.xxx.games.spacegame::Main/soundEventListener()[/Users/xxx/Documents/Developer/AS3_Flex/game_development/projects/xxx/SpaceGame/src/com/xxx/games/spacegame/Main.as:1407]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at com.xxx.games.spacegame::SpaceGame/createEnemyProj()[/Users/xxx/Documents/Developer/AS3_Flex/game_development/projects/xxx/SpaceGame/src/com/xxx/games/spacegame/SpaceGame.as:3708]
我仍然不知道是什么原因导致了这个错误。我唯一发现的是,有时候SoundManager中的对象为空。但我不知道为什么。我已经检查了所有适当的数组,如果有什么东西丢失了,但那里似乎一切正常

这是SoundManager中发生错误的部分:

public function playSound(soundName:String, isSoundTrack:Boolean = false, loops:int = 1, offset:Number = 0, _volume:Number = 1, fadeIn:Boolean = false,
                                _duration:Number = 1):void {



    tempSoundTransform.volume = _volume;


    tempSound = sounds[soundName];//sometimes null (still don't know why)

    if (!fadeIn) {
        if (isSoundTrack) {
            if (soundTrackChannel != null) {
                soundTrackChannel.stop();
            }
            soundTrackChannel = tempSound.play(offset, loops);                              
            soundTrackChannel.soundTransform = tempSoundTransform;  
        } else {


            soundChannels[soundName] = tempSound.play(offset, loops);//sometimes null but still don't know why

            soundChannels[soundName].soundTransform = tempSoundTransform;//line 106 (see errors above)


        }
    } else {

        fadeInSoundTransform = new SoundTransform(0, 0);

        if (isSoundTrack) {
            if (soundTrackChannel != null) {
                soundTrackChannel.stop();
            }
            soundTrackChannel = tempSound.play(offset, loops, fadeInSoundTransform);
        } else {
            soundChannels[soundName] = tempSound.play(offset, loops, fadeInSoundTransform);

        }

        TweenLite.to(fadeInSoundTransform, _duration, {volume:_volume, onUpdate:updateFadeIn, onUpdateParams:[soundName, isSoundTrack]});
    }
}
这是第3708行(参见上面的错误):

这是CustomEventSound类:

public function CustomEventSound(type:String, name:String, isSoundTrack:Boolean = false, loops:int = 0,
            offset:Number = 0, _volume:Number = 1, fadeIn:Boolean = false, fadeOut:Boolean = false,
        _duration:Number = 2, startVol:Number = 1, bubbles:Boolean = false, cancelable:Boolean = false)
{
    super(type, bubbles, cancelable);
    this.name = name;
    this.loops = loops;
    this.offset = offset;
    this._volume = _volume;
    this.isSoundTrack = isSoundTrack;
    this.fadeIn = fadeIn;
    this.fadeOut = fadeOut;
    this._duration = _duration;
    this.startVol = startVol;

}

public override function clone():Event {
    return new CustomEventSound(type, name, isSoundTrack, loops, offset, _volume, fadeIn, fadeOut, _duration, startVol, bubbles, cancelable)
}


public override function toString():String {
    return formatToString(type, "type", "bubbles", "cancelable", "eventPhase", name, isSoundTrack, loops, offset, _volume, fadeIn,
                fadeOut, _duration, startVol);
}
CustomEventSounds事件的侦听器函数

override public function soundEventListener(e:CustomEventSound):void {

if (e.type == CustomEventSound.PLAY_SOUND) {

soundManager.playSound(e.name, e.isSoundTrack, e.loops, e.offset, e._volume, e.fadeIn, e._duration);
} else {
    soundManager.stopSound(e.name, e.isSoundTrack, e.fadeOut, e._duration, e.startVol);
}
}

一次最多有32个活动声道。当您调用
tempSound.play(offset,loops)
时,当当前已播放32种声音时,它将返回
null


所以你应该记录播放声音的数量,当你有32个声音并且必须播放一个新的声音时,你应该记录最旧的声音频道,或者忽略新的声音。play()返回SoundManager中的声音。as:106SoundChannel[soundName].soundTransform=tempSoundTransform;哇!这是我到目前为止还不知道的事情。非常感谢!:)我会尽快尝试这个…找到一个可能的解决方案:[link]到目前为止仍在测试游戏,我再也看不到任何错误了。。。
override public function soundEventListener(e:CustomEventSound):void {

if (e.type == CustomEventSound.PLAY_SOUND) {

soundManager.playSound(e.name, e.isSoundTrack, e.loops, e.offset, e._volume, e.fadeIn, e._duration);
} else {
    soundManager.stopSound(e.name, e.isSoundTrack, e.fadeOut, e._duration, e.startVol);
}
}