I have a simple drag and drop game started in flash and mostly working.
I have added my animals and you can drag and drop them in the right place.I have also added sound
@Neal Davis is right about what is causing the problem, however I suggest to remove event listener once the animal is its destination position instead of listening MOUSE_UP
event on the animal itself in release function:
function fl_ReleaseToDrop_3(event:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_3);
Pig.stopDrag();
//...
}
Also as @Organis pointed out. It's much simpler to have more generic code. Consider having something like this:
Document class:
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"));
}
}
}
And simple Animal class:
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;
}
}
}
Look at your MouseUp event handlers.
You are adding the event listener to the stage. So they are all getting called whenever the stage hears a mouse up event.
Add those listeners to the cow, pig etc just as you did for the mouse down listeners.
That should solve it.
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);