Search code examples
actionscript-3flashflashdevelop

FlashDevelop sound problems


My goal is to create an Asteroids clone, I have the game set, as found on the chrism web tutorial. I have the game almost completely set, but, when I add a sound, The program complains with the following errors.

\src\Main.as(21): col: 3 Error: Access of undefined property soundClip.

\src\Main.as(20): col: 17 Error: Type was not found or was not a compile-time constant: Sound.

The code is shown here :

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;

    /**
     * ...
     * @author Chris Moeller
     * http://www.chrismweb.com
     * Tutorial: Creating an Asteroids Game: Part 4
     */

    public class Main extends Sprite 
    {
        [Embed(source = "snd/9mmshot.mp3")]

        private const embeddedSound:Class;
        var soundClip:Sound = new embeddedSound(); // Bullet
        soundClip.play(); // Play the sound
        private var game:Game;
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point

            //create the game object passing in the swf width and height
            game = new Game(stage.stageWidth, stage.stageHeight);

            //add the game bitmap to the screen/ Main.as Sprite to make it visible
            addChild(game.bitmap);

            //Create the main game loop
            addEventListener(Event.ENTER_FRAME, Run);


            //add keylisteners
            stage.addEventListener(KeyboardEvent.KEY_DOWN, game.KeyDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, game.KeyUp);

            stage.addEventListener(MouseEvent.MOUSE_DOWN, game.MouseDown);
            stage.addEventListener(MouseEvent.MOUSE_UP, game.MouseUp);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, game.MoveMouse);
        }

        private function Run(e:Event):void
        {
           game.Update();
           game.Render();            
        }   
    }   
}

I have tried researching the problem and no luck here, I have even checked some adding sound tutorials using the program.

I have even reduced the sound quality as found online

but no luck here and I have no idea what's going on. Please Help!! Any Help would be extremely appreciated! I used online audio converter to convert the audio.


Solution

  • To avoid your first error (Error: Type was not found or was not a compile-time constant: Sound.), you have to import the Sound class to your class :

    // ...
    
    import flash.media.Sound;
    

    For the second error, you should know that you can not use your soundClip object outside a function, that part is just to declare (and initialize) your objects, so you can do, for example :

    private function shotFunction(): void 
    {
        soundClip.play();
    }
    

    Hope that can help.