Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 在动作脚本上播放声音_Actionscript 3 - Fatal编程技术网

Actionscript 3 在动作脚本上播放声音

Actionscript 3 在动作脚本上播放声音,actionscript-3,Actionscript 3,我有一个简单的拖放游戏,从flash开始,主要是工作 我已经添加了我的动物,您可以将它们拖放到正确的位置。我还添加了声音,这样当动物被放到正确的位置时,这是有效的。但是每次我将新动物添加到正确的位置时,它都会播放该声音和最后一个动物声音 例如。将清管器放置在清管器空间中,可播放清管器声音 将奶牛放置在奶牛空间,播放奶牛声和猪声 把鸭子放在鸭子的空间里,它会播放鸭子的声音、牛的声音和猪的声音 很明显,我只想在动物被放置在正确的位置时播放声音-在跌落时播放(而不是在下一次动物跌落时播放) 我不确定我

我有一个简单的拖放游戏,从flash开始,主要是工作

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

例如。将清管器放置在清管器空间中,可播放清管器声音 将奶牛放置在奶牛空间,播放奶牛声和猪声 把鸭子放在鸭子的空间里,它会播放鸭子的声音、牛的声音和猪的声音

很明显,我只想在动物被放置在正确的位置时播放声音-在跌落时播放(而不是在下一次动物跌落时播放)

我不确定我做错了什么

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

导入flash.media.Sound;
var偏移量: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_向下,fl_单击拖动);
函数fl\u ClickToDrag(事件:MouseeEvent):无效
{
Pig.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE\u UP,fl\u releaseTrop);
函数fl_releaseTorop(事件:MouseEvent):无效
{
Pig.stopDrag();
//在检查位置是否正确的同时拖放工件
if(Pig.xpigEndX+offset | | | Pig.ypigEndY+offset){
Pig.x=pigStartX;
Pig.y=pigStartY;
}
否则{
//将工件放回原始位置
Pig.x=pigEndX;
Pig.y=pigEndY;
var-oink:PigOink=新的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_向下,fl_单击拖动2);
函数fl\u ClickToDrag\u 2(事件:MouseeEvent):无效
{
奶牛。startDrag();
}
stage.addEventListener(MouseEvent.MOUSE\u UP,fl\u releaseTrop\u 2);
函数fl_releaseTrop_2(事件:MouseEvent):无效
{
停止拖动();
//在检查位置是否正确的同时拖放工件
if(Cow.xcowEndX+offset | | | Cow.ycowEndY+offset){
Cow.x=cowStartX;
Cow.y=cowStartY;
}
否则{
//将工件放回原始位置
Cow.x=前弯;
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_向下,fl_单击拖动3);
函数fl\u单击ToDrag\u 3(事件:MouseeEvent):无效
{
Duck.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE\u UP,fl\u releaseTrop\u 3);
函数fl_releaseTrop_3(事件:MouseEvent):无效
{
Duck.stopDrag();
//在检查位置是否正确的同时拖放工件
if(Duck.xduckEndX+offset | | | Duck.yduckEndY+offset){
//将工件放回原始位置
Duck.x=duckStartX;
Duck.y=duckStartY;
}
否则{
Duck.x=duckEndX;
Duck.y=duckEndY;
var-quack:DuckQuack=new-DuckQuack();
var channel:SoundChannel=quack.play();
//checkGame();
}
}

查看您的MouseUp事件处理程序

您正在将事件侦听器添加到阶段。因此,每当舞台上听到鼠标移动事件时,他们都会被呼叫

将这些侦听器添加到cow、pig等中,就像您对鼠标按下侦听器所做的那样

这应该可以解决问题

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);

查看您的MouseUp事件处理程序

您正在将事件侦听器添加到阶段。因此,每当舞台上听到鼠标移动事件时,他们都会被呼叫

将这些侦听器添加到cow、pig等中,就像您对鼠标按下侦听器所做的那样

这应该可以解决问题

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);

@Neal Davis关于导致问题的原因是正确的,但是我建议在动物到达其目标位置后删除事件侦听器,而不是在释放功能中侦听动物本身上的
MOUSE_UP
事件:

function fl_ReleaseToDrop_3(event:MouseEvent):void {
    stage.removeEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_3);
    Pig.stopDrag();
    //...
}
传统的建议。 正如@Organia所指出的。拥有更多的通用代码要简单得多。 考虑一下这样的事情:

文档类:

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;
        }

    }

}

@Neal Davis关于导致问题的原因是正确的,但是我建议在动物到达其目标位置后删除事件侦听器,而不是在释放功能中侦听动物本身上的
MOUSE_UP
事件:

function fl_ReleaseToDrop_3(event:MouseEvent):void {
    stage.removeEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_3);
    Pig.stopDrag();
    //...
}
传统的建议。 正如@Organia所指出的。拥有更多的通用代码要简单得多。 考虑一下这样的事情:

文档类:

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;
        }

    }

}

首先,去掉额外的代码。取而代之的是3段类似的代码,您需要某种具有一些数据的DragableAnimal类:origin:Point、destination:Point、voice:class、image:Sprite等。然后创建该类的3个实例,并用适当的数据填充它们。到那时,问题可能会自行解决。谢谢,但我对这一点还不太熟悉,并试图以我首先理解的方式解决它。我知道你的方式会更有效率和整洁,但我想先解决这个问题-有什么想法吗?当然。您为同一鼠标启动事件订阅了3种方法,并同时触发了所有3种方法。我想,你只需点击舞台而不用拖拽,仍然可以听到所有的声音。为了避免这种情况,只有当你开始拖动动物时,你才需要订阅MOUSE_UP。嗨,Organi,谢谢你的回复,