有時我在遊戲中得到這個奇怪的錯誤:奇怪的聲音錯誤
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的事件監聽器事件
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);
}
}
um什麼是SoundManager.as:106 – 2012-04-18 23:17:32
soundChannels [soundName] .soundTransform = tempSoundTransform; – drpelz 2012-04-19 20:23:07