简体   繁体   English

在Flash Pro的ActionScript3.0中使用声音文件的代码示例

[英]Code examples for using sound files in ActionScript3.0 for Flash Pro

I'd appreciate seeing some examples of ActionScript 3.0 code that deploys any kind of sound files to be used in Flash Pro files, for final output as SWF. 我很高兴看到一些ActionScript 3.0代码示例,这些示例部署了将在Flash Pro文件中使用的任何声音文件,最终输出为SWF。

I am using the code (below) for the Base class of a Bubble Game (of sorts), and the problem is, that when popped, the sound plays a little too late, not spot on. 我正在使用(下面)泡泡游戏(某些类型)的Base类的代码,问题是,当弹出该声音时,声音播放的太迟了,一点也没有出现。

thanks. 谢谢。 This is my code - which is linked to the Bubbles in the library of my FLA: 这是我的代码-链接到我的FLA库中的Bubbles:

package 
{
    import flash.display.*;
    import flash.events.*;
    import flash.media.Sound;
    import flash.net.URLRequest;

    public class BubbleBaseNew4 extends MovieClip
    {
        var thisBubble:MovieClip;
        var navdock:MovieClip;
        var thisParent:*;
        private var snd:Sound = new Sound();

        public function BubbleBaseNew4()
        {
            this.addEventListener(Event.ADDED_TO_STAGE, initialize);//off http://kb2.adobe.com/cps/838/cpsid_83815.html to do ADDED_TO_STAGE instead of ADDED
            this.addEventListener(MouseEvent.CLICK, pop);
        }

        function initialize(event:Event):void
        {
            this.removeEventListener(Event.ADDED_TO_STAGE, initialize);//this causes sym_mc to be defined as an Ojbect

            thisParent = event.currentTarget.parent;
            navdock = thisParent.nav_mc;
            thisBubble = MovieClip(this.parent.getChildByName(this.name));

            this.addEventListener(Event.ENTER_FRAME, moveBubble);

            var req:URLRequest = new URLRequest("my_own_twp_noise_tremolo_phaser_.mp3");
            snd.load(req); 
        }

        function moveBubble(event:Event):void
        {
            if(navdock != null && this.hitTestObject(navdock)) 
            {
                trace("HIT!");
                this.removeEventListener(Event.ENTER_FRAME, moveBubble);
                this.removeEventListener(MouseEvent.CLICK, pop);
                this.parent.addChild(this);//this adds the bubble that's removed, i think.
                this.parent.removeChild(thisBubble);
            }
            else
            {
                var mc:MovieClip = event.target as MovieClip;
                if(currentFrame == 1)
                {
                    mc.x = Math.random() * stage.stageWidth;
                    mc.y = Math.random() * stage.stageHeight;

                    rotation += Math.random() * -90;
                }

            }
        }

        function pop(event:MouseEvent):void
        {
            /*var bzp:Sound = Sound(thisParent.my_own_twp_noise_tremolo_phaser_.wav);
            this.load(bzp);*/
            this.removeEventListener(Event.ADDED_TO_STAGE, initialize);
            this.removeEventListener(MouseEvent.CLICK, pop);
            this.removeEventListener(Event.ENTER_FRAME, moveBubble);

            var main:MovieClip = MovieClip(this.parent.parent);
            main.increaseScore();
            //mediary 'increaseScore function' variable
            var main4:MovieClip = MovieClip(this.parent.parent);
            main4.increaseScore4();
            this.parent.removeChild(thisBubble);
            snd.play();
        }
    }
}

Two things you could try. 您可以尝试两件事。 The first is to change the mouseEventListener for the pop function from MouseEvent.CLICK to MouseEvent.MOUSE_DOWN , that way you don't have to wait for a MOUSE_UP before the function is called. 第一种是将弹出函数的mouseEventListener从MouseEvent.CLICK更改为MouseEvent.MOUSE_DOWN ,这样您就不必在调用函数之前就等待MOUSE_UP了。

The second thing is to make sure the sound file is trimmed down so there is absolutely not one nanosecond of silence at the beginning. 第二件事是确保声音文件被修整,因此开始时绝对没有一纳秒的沉默。 Audacity is a good program to use for stuff like that. Audacity是用于此类事情的好程序。

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

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