动作3 - 播放声音然后延迟

时间:2014-04-16 21:07:52

标签: android actionscript-3 flash audio

我是as3的全新人,大约3个月前作为大学项目的一部分开始,所以如果我提出一个明显的问题或者我提供了错误的信息,我会道歉。

我正在创造一个游戏,其中有一个怪物'从一个数组中碰撞出一个静态的影片剪辑,用户必须通过杀死怪物才能避免它们到达那里。

当怪物到达影片剪辑时,我希望它能够播放一个“傻瓜”。声音一次,然后再播放几秒钟,但代码就意味着当数组项和影片剪辑发生碰撞时声音反复播放,这听起来不对。

我尝试通过添加一个允许声音持续半秒然后停止声道的计时器来纠正这个问题,但这似乎没有用。

如何让声音播放一次,而不是在接下来的几秒钟再播放,然后在物体仍在碰撞时再次播放?

提前致谢。

继承我的代码:

var sc: SoundChannel;
var munch: Sound = new Sound(new URLRequest("audio/munch.mp3"));

var muteTest: Boolean = false;

var munchStop:Timer = new Timer (500, 1);
munchStop.addEventListener(TimerEvent.TIMER, afterMunchStop);
function afterMunchStop(event: TimerEvent): void {
sc.stop();
}

if (monster1Array[i].hitTestObject(centre_mc)) {
   if (muteTest == false) {
   sc = munch.play();
   munchStop.start();
}

1 个答案:

答案 0 :(得分:0)

var sc: SoundChannel;
var munch: Sound = new Sound(new URLRequest("audio/munch.mp3"));
var soundIsPlaying: Boolean = false;

//sound should loop until objects no longer collide
var munchStop:Timer = new Timer(500);
munchStop.addEventListener(TimerEvent.TIMER, afterMunchStop);

addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onEnterFrame(event:Event):void
{
    //play sound if monsters collide and sound is not already playing
    if (monster1Array[i].hitTestObject(centre_mc) && !soundIsPlaying)
    {
       sc = munch.play();
       soundIsPlaying = true;

       //start the timer if it's not running
       if(!munchStop.running)
           munchStop.start();
    } else {
       //stop the sound if it's playing
       if(sc)
           sc.stop();
       soundIsPlaying = false;

       //reset the timer when the objects are no longer colliding
       if(munchStop.running)
           munchStop.reset();
    }
}

function afterMunchStop(event: TimerEvent): void {
    soundIsPlaying = false;
}
相关问题