在Actionscript上播放声音

时间:2017-03-15 13:18:09

标签: actionscript-3

我在Flash中启动了一个简单的拖放游戏,大部分都在工作。

我已经添加了我的动物,你可以将它们拖放到正确的位置。我还添加了声音,这样当动物掉落在正确的位置时,每次我添加一个新的动物时,这是有效的正确的位置,它播放声音和最后的动物声音。

e.g 即可。把猪放在猪圈里,它会播放猪的声音 把牛放在牛的空间里,它播放牛声和猪声 把鸭子放在鸭子的空间里,它会播放鸭子的声音和牛声以及猪的声音。

显然,当动物被放置在正确的位置时,我只想播放声音 - 放下(不是下一个动物掉落)

我不确定我做错了什么

/ *拖放 通过拖放使指定的符号实例可移动。 * /

 import flash.media.Sound;
  var offset:int = 10;

  var pigStartX:int = 196.80;
  var pigStartY:int = 292.10;

  var pigEndX:int = 578.40;
  var pigEndY:int = 208.50;

  Pig.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);

  function fl_ClickToDrag(event:MouseEvent):void
  {
  Pig.startDrag();
   }

 stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);

  function fl_ReleaseToDrop(event:MouseEvent):void
   {
  Pig.stopDrag();
// dragging and dropping the pieces while checking for correct   location 

    if(Pig.x < pigEndX - offset || Pig.x > pigEndX + offset || Pig.y <    pigEndY - offset ||Pig.y > pigEndY + offset){

    Pig.x = pigStartX;
    Pig.y = pigStartY;
}

  else{

     //set piece back to original position
     Pig.x = pigEndX;
     Pig.y = pigEndY;

     var oink:PigOink = new PigOink(); 
     var channel:SoundChannel = oink.play();
    //checkGame();

}
}

/ *拖放 通过拖放使指定的符号实例可移动。 * /

 var cowStartX:int = 324;
 var cowStartY:int = 317.95;

 var cowEndX:int = 411.50;
 var cowEndY:int = 140.95;

   Cow.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_2);

  function fl_ClickToDrag_2(event:MouseEvent):void
  {
    Cow.startDrag();
  }

  stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_2);

 function fl_ReleaseToDrop_2(event:MouseEvent):void
 {
    Cow.stopDrag();
// dragging and dropping the pieces while checking for correct  location 
 if(Cow.x < cowEndX - offset || Cow.x > cowEndX + offset || Cow.y <  cowEndY - offset ||Cow.y > cowEndY + offset){

      Cow.x = cowStartX;
      Cow.y = cowStartY;
 }

  else{

    //set piece back to original position
      Cow.x = cowEndX;
      Cow.y = cowEndY;

    var moo:CowMoo = new CowMoo(); 
    var channel:SoundChannel = moo.play();
    //checkGame();

}

}

/ *拖放 通过拖放使指定的符号实例可移动。 * /

   var duckStartX:int = 209.45;
   var duckStartY:int = 402.05;

   var duckEndX:int = 56.45;
   var duckEndY:int = 225.05;

  Duck.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_3);

  function fl_ClickToDrag_3(event:MouseEvent):void
  {
     Duck.startDrag();
   }

  stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_3);

  function fl_ReleaseToDrop_3(event:MouseEvent):void
  {
     Duck.stopDrag();
// dragging and dropping the pieces while checking for correct location 
     if(Duck.x < duckEndX - offset || Duck.x > duckEndX + offset ||   Duck.y < duckEndY - offset ||Duck.y > duckEndY + offset){

    //set piece back to original position
        Duck.x = duckStartX;
        Duck.y = duckStartY;
     }

     else{


        Duck.x = duckEndX;
        Duck.y = duckEndY;

        var quack:DuckQuack = new DuckQuack(); 
        var channel:SoundChannel = quack.play();
    //checkGame();
   }


   }

2 个答案:

答案 0 :(得分:2)

@Neal Davis对导致问题的原因是正确的,但是我建议在动物为目标位置后删除事件监听器,而不是在释放函数中监视动物本身的MOUSE_UP事件:

function fl_ReleaseToDrop_3(event:MouseEvent):void {
    stage.removeEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_3);
    Pig.stopDrag();
    //...
}

有条件的建议。

同样@Organis指出。拥有更通用的代码要简单得多。 考虑这样的事情:

文档类:

package
{
    import flash.display.Sprite;
    import flash.geom.Point;

    public class Main extends Sprite
    {
        private var animals:Vector.<Animal> = Vector.<Animal>([]);

        public function Main() {
            animals.push(new Animal("pig", pigMc, 578, 208, "oink.mp3"));
            animals.push(new Animal("cow", cowMc, 411, 140, "moo.mp3"));
            animals.push(new Animal("duck", duckMc, 56, 225, "quack.mp3"));
        }

    }

}

简单的动物类:

package
{
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.media.Sound;
    import flash.net.URLRequest;

    public class Animal
    {
        private var _name:String;
        private var _dropMargin:Number = 10;
        private var _startPos:Point = new Point();
        private var _endPos:Point = new Point();
        private var _sound:Sound;
        private var _view:Sprite;

        public function Animal(name:String, view:Sprite, endX:Number = 0, endY:Number = 0, sound:String = null) {
            _name = name;
            _view = view;
            _startPos.x = _view.x; _startPos.y = _view.y;
            _endPos.x = endX; _endPos.y = endY;
            _sound = new Sound(new URLRequest(sound));
            view.addEventListener(MouseEvent.MOUSE_DOWN, onMDown);
        }

        private function onMDown(e:MouseEvent):void{
            _view.stage.addEventListener(MouseEvent.MOUSE_UP, onMUp);
            _view.startDrag();
        }

        private function onMUp(e:MouseEvent):void {
            _view.stage.removeEventListener(MouseEvent.MOUSE_UP, onMUp);
            _view.stopDrag();
            //Distance to destination point
            var dd:Number = Point.distance(pos, _endPos);

            if (dd > dropMargin) pos = _startPos;
            else {
                pos = _endPos;
                _sound.play();
            }
        }

        public function get view():Sprite{return _view;}

        public function get dropMargin():Number{return _dropMargin;}
        public function set dropMargin(value:Number):void{_dropMargin = value;}

        public function get pos():Point{return new Point(_view.x, _view.y);}
        public function set pos(value:Point):void{
            _view.x = value.x;
            _view.y = value.y;
        }

    }

}

答案 1 :(得分:1)

查看MouseUp事件处理程序。

您正在向舞台添加事件侦听器。所以每当舞台听到鼠标事件时,他们都会被调用。

将这些听众添加到牛,猪等中,就像您为鼠标按下听众所做的那样。

那应该解决它。

Pig.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);

  function fl_ClickToDrag(event:MouseEvent):void
  {
  Pig.startDrag();
   }

 //stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
  // change to this
 Pig.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
相关问题