lundi 10 avril 2017

TypeError 1009 and muting sound in AS3

I'm really new to this, so, I'm really sorry if there is some obvious solution that I've somehow missed, but oh well...
I am in the process of making a game, and I was trying to make a character selection menu. Each character would have a unique version of the same song (with the same bps) so that when you were "hovering over" one of the choices, the character's song would play and the other ones would be muted (Not paused or stopped, so as not to lose the synchronisation between all the versions of the song)...
Right now, I was trying to program this for only one of the songs, and then, repeat the method for the other ones once I figured it out.
So...
What am I doing wrong?

Here's the code:

package {

import flash.display.MovieClip;
import flash.events.Event; 
import flash.media.SoundTransform;
import flash.media.Sound;
import flash.media.SoundChannel; 
import flash.media.SoundMixer; 

public class MainThing extends MovieClip {
    private var soulcounting: Number = 1;
    private var Muting: Boolean = false;
    private var snd:Sound = new Sound();

    public function MainThing() {
        snd.load(new URLRequest("SOUL1.mp3"));
        stage.addEventListener(Event.ENTER_FRAME, FrameHandler);
        }

    private function FrameHandler(event: Event): void {
        if (Object(root).currentFrame == 2) {
            var channel:SoundChannel = snd.play(0,1);
            channel.soundTransform = new SoundTransform(1,0);
            /*the soulcounting number is determined by a keyboard function...  
            I didn't deem it necessary to be included in here,  
            as I don't think it is part of the problem.*/
            if (soulcounting == 1) {
                Object(root).SOULS.gotoAndStop(1);
                Muting = false;
                }

            if (soulcounting == 2) {
                Object(root).SOULS.gotoAndStop(2);
                Muting = true;
                }

            if (soulcounting == 3) {
                Object(root).SOULS.gotoAndStop(3);
                Muting = true;
                }

            if (soulcounting == 4) {
                Object(root).SOULS.gotoAndStop(4);
                Muting = true;
                }

            if (soulcounting == 5) {
                Object(root).SOULS.gotoAndStop(5);
                Muting = true;
                }

            if (soulcounting == 6) {
                Object(root).SOULS.gotoAndStop(6);
                Muting = true;
                }

            if(Muting){
                setVolume(channel);
                }

            if(!Muting){
                setVolume(channel, 1);
                }
            } 

    private function setVolume (Soundchannel:SoundChannel, volume:Number=0) {
        var trans:SoundTransform = Soundchannel.soundTransform;
        trans.volume = volume;
        Soundchannel.soundTransform = trans;
        }
    }
}

(If I try to preview the swf, everything works fine until I get to the second frame, in which the Output starts repeating TypeError: Error #1009: Cannot access a property or method of a null object reference. at MainThing/FrameHandler())
TypeError #1009 seems to be a very common mistake around here, but what I have read hasn't really helped me with this problem... Sorry if there was already a solution in here, and I just didn't search enough...
Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire