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 so that when the animal is dropped in the right spot , that is working however each time I add a new animal to the right spot it plays that sound and the last animal sound as well.
e.g. place pig in pig space , it plays pig sound place cow in cow space , it plays cow sound and pig sound place duck in duck space , it plays duck sound and cow sound and pig sound.
Obviously I only want to play the sound when the animal is placed in the right place - on drop ( not on next animal drop also )
I'm not sure what I have done wrong
/* Drag and Drop Makes the specified symbol instance moveable with drag and drop. */
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();
}
}
/* Drag and Drop Makes the specified symbol instance moveable with drag and drop. */
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();
}
}
/* Drag and Drop Makes the specified symbol instance moveable with drag and drop. */
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();
}
}
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);