Adding Sound Upon Dropping to Target
Hi again,
I am trying to have a short sound play if the mode of transportation is dragged to the correct year. I have tried to add the code...but does not work. Could you please review it and let me know how the code should be?
Thanks!
PHP Code:
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.DisplayObject;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import DragDrop;
import TN;
import AP;
import CW;
public class DragGame extends MovieClip
{
private var tn:TN;
private var ap:AP;
private var cw:CW;
public function DragGame()
{
createPieces();
}
private function createPieces():void
{
tn = new TN();
addChild(tn);
tn.x = 169.9;
tn.y = 90.8;
tn.width = 370.9;
tn.height = 68;
tn.alpha = 0.3;
ap = new AP();
addChild(ap);
ap.x = 496.6;
ap.y = 169.9;
ap.width = 162.8;
ap.height = 110.2;
ap.alpha = 0.3;
cw = new CW();
addChild(cw);
cw.x = 44.8;
cw.y = 193.1;
cw.width = 197.0;
cw.height = 94;
cw.alpha = 0.3;
}
public function setUpTargets():void
{
var correctsoundReq:URLRequest = new URLRequest("correct1.wav");
//var wrongsoundReq:URLRequest = new URLRequest ("audio/wrong1.wav");
var correctsound:Sound = new Sound();
//var wrongsound:Sound = new Sound();
correctsound.load(correctsoundReq);
//wrongsound.load(wrongsoundReq);
tn._targetPiece = ttn_mc;
tn.addEventListener(MouseEvent.MOUSE_UP, checkTarget);
correctsound.addEventListener(Event.COMPLETE, checkTarget);
trace(ttn_mc);
ap._targetPiece = tap_mc;
ap.addEventListener(MouseEvent.MOUSE_UP, checkTarget);
trace(tap_mc);
cw._targetPiece = tcw_mc;
cw.addEventListener(MouseEvent.MOUSE_UP, checkTarget);
trace(tcw_mc);
apm._targetPiece = tap_mc;
apm.addEventListener(MouseEvent.MOUSE_UP, checkTarget);
trace(tap_mc);
cwm._targetPiece = tcw_mc;
cwm.addEventListener(MouseEvent.MOUSE_UP, checkTarget);
trace(tcw_mc);
tnm._targetPiece = ttn_mc;
tnm.addEventListener(MouseEvent.MOUSE_UP, checkTarget);
trace(ttn_mc);
}
/*private function onComplete(event:Event):void
{
correctsound.play();
}*/
private function checkTarget(event:MouseEvent):void
{
var ec:MovieClip = MovieClip(event.currentTarget);
var target:DisplayObject = DisplayObject(ec._targetPiece);
if(ec.hitTestObject(target))
{
trace("whooooot!");
correctsound.play();
ec.x = target.x;
ec.y = target.y;
ec.removeEventListener(MouseEvent.MOUSE_UP, checkTarget);
ec.disable();
}
else
{
//wrongsound.play();
ec.x = ec._origX;
ec.y = ec._origY;
}
}
}
}
moving declaration to higher level?
Hi again,
So I added the "import flash.events.Event;" to the package. And I tried to move the var declarations in the package area and the public function DragGame area and the private function createPieces...and I couldn't get them to work...where exactly should they go? With the code as it is below, I am getting an error on line 119, the line in the checkTarget function that says, "correctsound.play();"
PHP Code:
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.DisplayObject;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.events.Event;
import DragDrop;
import TN;
import AP;
import CW;
public class DragGame extends MovieClip
{
private var tn:TN;
private var ap:AP;
private var cw:CW;
public function DragGame()
{
createPieces();
}
private function createPieces():void
{
var correctsoundReq:URLRequest = new URLRequest("correct1.wav");
//var wrongsoundReq:URLRequest = new URLRequest ("audio/wrong1.wav");
var correctsound:Sound = new Sound();
//var wrongsound:Sound = new Sound();
correctsound.load(correctsoundReq);
//wrongsound.load(wrongsoundReq);
correctsound.addEventListener(Event.COMPLETE, checkTarget);
tn = new TN();
addChild(tn);
tn.x = 169.9;
tn.y = 90.8;
tn.width = 370.9;
tn.height = 68;
tn.alpha = 0.3;
ap = new AP();
addChild(ap);
ap.x = 496.6;
ap.y = 169.9;
ap.width = 162.8;
ap.height = 110.2;
ap.alpha = 0.3;
cw = new CW();
addChild(cw);
cw.x = 44.8;
cw.y = 193.1;
cw.width = 197.0;
cw.height = 94;
cw.alpha = 0.3;
}
public function setUpTargets():void
{
tn._targetPiece = ttn_mc;
tn.addEventListener(MouseEvent.MOUSE_UP, checkTarget);
trace(ttn_mc);
ap._targetPiece = tap_mc;
ap.addEventListener(MouseEvent.MOUSE_UP, checkTarget);
trace(tap_mc);
cw._targetPiece = tcw_mc;
cw.addEventListener(MouseEvent.MOUSE_UP, checkTarget);
trace(tcw_mc);
apm._targetPiece = tap_mc;
apm.addEventListener(MouseEvent.MOUSE_UP, checkTarget);
trace(tap_mc);
cwm._targetPiece = tcw_mc;
cwm.addEventListener(MouseEvent.MOUSE_UP, checkTarget);
trace(tcw_mc);
tnm._targetPiece = ttn_mc;
tnm.addEventListener(MouseEvent.MOUSE_UP, checkTarget);
trace(ttn_mc);
}
/*private function onComplete(event:Event):void
{
correctsound.play();
}*/
private function checkTarget(event:MouseEvent):void
{
var ec:MovieClip = MovieClip(event.currentTarget);
var target:DisplayObject = DisplayObject(ec._targetPiece);
if(ec.hitTestObject(target))
{
trace("whooooot!");
correctsound.play();
ec.x = target.x;
ec.y = target.y;
ec.removeEventListener(MouseEvent.MOUSE_UP, checkTarget);
ec.disable();
}
else
{
//wrongsound.play();
ec.x = ec._origX;
ec.y = ec._origY;
}
}
}
}
Added x and y and now getting new error
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at DragGame/checkTarget()
I added the x and y to the instances on the stage....what is wrong here?
PHP Code:
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.display.DisplayObject;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.events.Event;
import DragDrop;
import TN;
import AP;
import CW;
public class DragGame extends MovieClip
{
private var tn:TN;
private var ap:AP;
private var cw:CW;
private var correctsound:Sound;
public function DragGame()
{
createPieces();
}
private function createPieces():void
{
var correctsoundReq:URLRequest = new URLRequest("correct1.wav");
//var wrongsoundReq:URLRequest = new URLRequest ("audio/wrong1.wav");
var correctsound:Sound = new Sound();
//var wrongsound:Sound = new Sound();
correctsound.load(correctsoundReq);
//wrongsound.load(wrongsoundReq);
correctsound.addEventListener(Event.COMPLETE, checkTarget);
tn = new TN();
addChild(tn);
tn.x = 169.9;
tn.y = 90.8;
tn.width = 370.9;
tn.height = 68;
tn.alpha = 0.3;
ap = new AP();
addChild(ap);
ap.x = 496.6;
ap.y = 169.9;
ap.width = 162.8;
ap.height = 110.2;
ap.alpha = 0.3;
cw = new CW();
addChild(cw);
cw.x = 44.8;
cw.y = 193.1;
cw.width = 197.0;
cw.height = 94;
cw.alpha = 0.3;
}
public function setUpTargets():void
{
tn._targetPiece = ttn_mc;
tn.addEventListener(MouseEvent.MOUSE_UP, checkTarget);
trace(ttn_mc);
ap._targetPiece = tap_mc;
ap.addEventListener(MouseEvent.MOUSE_UP, checkTarget);
trace(tap_mc);
cw._targetPiece = tcw_mc;
cw.addEventListener(MouseEvent.MOUSE_UP, checkTarget);
trace(tcw_mc);
apm._targetPiece = tap_mc;
apm.addEventListener(MouseEvent.MOUSE_UP, checkTarget);
trace(tap_mc);
cwm._targetPiece = tcw_mc;
cwm.addEventListener(MouseEvent.MOUSE_UP, checkTarget);
trace(tcw_mc);
tnm._targetPiece = ttn_mc;
tnm.addEventListener(MouseEvent.MOUSE_UP, checkTarget);
trace(ttn_mc);
}
/*private function onComplete(event:Event):void
{
correctsound.play();
}*/
private function checkTarget(event:MouseEvent):void
{
var ec:MovieClip = MovieClip(event.currentTarget);
var target:DisplayObject = DisplayObject(ec._targetPiece);
if(ec.hitTestObject(target))
{
trace("whooooot!");
correctsound.play();
ec.x = target.x;
ec.y = target.y;
apm.x = 497.6;
apm.y = 409.9;
cwm.x = 50.3;
cwm.y = 426.1;
tnm.x = 181.9;
tnm.y = 295.8;
ec.removeEventListener(MouseEvent.MOUSE_UP, checkTarget);
ec.disable();
}
else
{
//wrongsound.play();
ec.x = ec._origX;
ec.y = ec._origY;
}
}
}
}