简体   繁体   English

Flixel + FlashDevelop声音无法播放

[英]Flixel+ FlashDevelop sound not playing

I'm a beginner at this, I have my Space Invaders clone set up, I learned how to make flash games on various tutorials on the internet, but with no sound. 我是一个初学者,我已经设置了Space Invaders克隆游戏,并且在互联网上的各种教程中学习了如何制作Flash游戏,但是没有声音。 However, the game runs fine with no errors, but, the sound is not playing when I press the SPACE Key, this is the kind of sound I want. 但是,游戏运行正常,没有错误,但是,当我按SPACE键时,声音没有播放,这是我想要的声音。 Here is the following code as seen on PlayerShip.as: 这是在PlayerShip.as上看到的以下代码:

package
{
    import org.flixel.*;

    public class PlayerShip extends FlxSprite       
    {
        [Embed(source = "../img/ship.png")] private var ImgShip:Class;  
        [Embed(source = "../snd/shoot.mp3")] private var ShootEffect:Class;


        public function PlayerShip()
        {

            super(FlxG.width/2-6, FlxG.height-12, ImgShip);
        }


        override public function update():void
        {

            velocity.x = 0;             

            if(FlxG.keys.LEFT)
                velocity.x -= 150;      
            if(FlxG.keys.RIGHT) 
                velocity.x += 150;      


            super.update();


            if(x > FlxG.width-width-4)
                x = FlxG.width-width-4; 
            if(x < 4)
                x = 4;                  



            if (FlxG.keys.justPressed("SPACE"))

            {

                var bullet:FlxSprite = (FlxG.state as PlayState).playerBullets.recycle() as FlxSprite;
                bullet.reset(x + width/2 - bullet.width/2, y);
                bullet.velocity.y = -140;
                FlxG.play(ShootEffect);
            }
        }
    }
}

I have researched on the internet and google shows only how to add music, not the sound I'm talking about, Please Help!!! 我已经在互联网上进行了研究,谷歌只显示了如何添加音乐,而不是我正在谈论的声音,请帮助!!! Any Help would be greatly appreciated as always!! 一如既往,任何帮助将不胜感激!

Playing a music or an isolated SFX is almost the same semantically, but here is a way of easily playing a SFX. 播放音乐或孤立的SFX在语义上几乎相同,但这是一种轻松播放SFX的方法。 It uses an instance of the FlxSound class: 它使用FlxSound类的实例:

package
{
    import org.flixel.*;

    public class PlayerShip extends FlxSprite       
    { 
        [Embed(source = "../snd/shoot.mp3")] private var ShootEffect:Class;


        private var shootSound:FlxSound;


        public function PlayerShip()
        {

            super(FlxG.width/2-6, FlxG.height-12, ImgShip);

            // Instantiate and load the SFX
            shootSound = new FlxSound();
            shootSound.loadEmbedded(ShootEffect);
        }


        override public function update():void
        {

            if (FlxG.keys.justPressed("SPACE"))

            {
                // Play the SFX
                shootSound.play();
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM