奇怪的声音错误

时间:2012-04-18 23:06:33

标签: 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/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中是空的。但我不知道为什么。我已经检查了所有相应的数组,如果有什么东西在那里丢失,但似乎一切都好。这是一个非常讨厌的bug,现在持续数周。请 - 我需要你的帮助来摆脱这个错误,以完成我的游戏。非常感谢你。

这是发生错误的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行(见上面的错误):

dispatchEvent(new CustomEventSound(CustomEventSound.PLAY_SOUND, enemyProjSounds[tempEnemyProj._type], false, 0, 8, setSoundVolume, false, false, 0));

这是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-Events的监听器功能

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);
}
}

1 个答案:

答案 0 :(得分:2)

一次最多有32个有效声道。当您拨打tempSound.play(offset, loops)时,如果当前正在播放32个声音,它将返回null

因此,您应该跟踪播放的声音数量,并且当您有32个声音并且必须播放新声音时,stop()最早的SoundChannel,或者只是忽略新声音{{1} }}返回Sound.play()