发生碰撞时我该如何播放声音?

时间:2015-01-26 16:37:09

标签: actionscript-3 flash

我是flash新手并使用as3。我正在制作一个简单的捕捉游戏,其中物品从顶部落下,你控制底部的篮子来抓住它们。我的剧本很好,并且在没有错误的情况下播放,我很高兴,但是当项目落入篮子时,我如何添加声音片段来播放这个剧本呢?在此先感谢!!!

import flash.events.MouseEvent;
import flash.events.Event;
import flash.text.TextField;

var catcher:Catcher;
var createEnemyID:uint;
var gameSpeed:uint;
var droppedText:TextField;
var caughtText:TextField;
var score:uint=0;

function initGame():void{
    catcher=new Catcher();
    catcher.x=500;
    catcher.y=1400;
    addChild(catcher);
    stage.addEventListener(MouseEvent.MOUSE_MOVE,moveCatcher);
    Mouse.hide();
    gameSpeed=500;
    createEnemyID=setInterval(createEnemy,gameSpeed);
    droppedText=new TextField();
    droppedText.x=50;
    droppedText.y=50;
    addChild(droppedText);
    caughtText=new TextField();
    caughtText.x=250;
    caughtText.y=50;
    addChild(caughtText);
    droppedText.text=caughtText.text='0';
}

function moveCatcher (e:MouseEvent):void{
    catcher.x=this.mouseX;
    e.updateAfterEvent();
}

function createEnemy():void{
    var enemy:Faller=new Faller();
    enemy.y=-1;
    enemy.x=Math.random()*stage.stageWidth;
    enemy.addEventListener (Event.ENTER_FRAME, dropEnemy);
    addChild(enemy);
}

function dropEnemy(e:Event):void{
    var mc:Faller=Faller(e.target);
    mc.y+=15;
    if(mc.hitTestObject(catcher)) {
        caught(mc);
    }

     else if (mc.y>stage.stageHeight){
         dropped(mc);
     }
}

function caught(mc:Faller):void{
    mc.removeEventListener (Event.ENTER_FRAME,dropEnemy);
    removeChild(mc);
    caughtText.text=String(Number(caughtText.text)+1);
}


function dropped(mc:Faller):void{
    mc.removeEventListener (Event.ENTER_FRAME,dropEnemy);
    removeChild(mc);
    droppedText.text=String(Number(droppedText.text)+1);

    if(droppedText.text=='5'){
        gameOver();


    }

}

function gameOver():void{
    score=Number(caughtText.text);
    stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveCatcher);
    removeChild(catcher);
    clearInterval(createEnemyID);
    removeChild(caughtText);
    removeChild(droppedText);
    while(numChildren>0){
        getChildAt(0).removeEventListener(Event.ENTER_FRAME,dropEnemy);
        removeChildAt(0);
    }
    Mouse.show();
    gotoAndStop('gameover');
}

initGame();

1 个答案:

答案 0 :(得分:1)

将声音导入闪光灯。 编辑属性并将声音类设置为MySoundClass或任何你喜欢的,但你必须在以后引用它。 在您的代码中,在碰撞方法中编写以下内容。

var sound:Sound = new MySoundClass(); 
sound.play();

请参阅此AS3 Sound tutorial

相关问题