TypeError: Error #1009: Cannot access a property or method of a null object reference
I am trying to do a drag and drop learning game, but the hittest is not working.
The graphics of the airplane (ap), the train (tn), and the covered wagon (cw) are supposed to be dragged to the timeline and then if the correct year is chosen, it should drop the graphic there. I keep getting the TypeError: Error #1009: Cannot access a property or method of a null object reference. I believe the checkTarget function is not working properly.
The timeline with the year is done with the line tool and the year is text. I then selected a portion of the timeline and converted it to a symbol with the instance name i.e. ttn_mc (target train movieclip). Is it possible the movieclip needs a larger "surface area" to click? or that the surface area needs to be the same or larger as the item that is be dropped?
The target movieclips themselves are not generated by an addChild, I just created them on the timeline with instance names. I have no linkage set up on each of the 3 targets and the instance names are not set up as variables in the .as files. I have 2 .as files: DragGame.as and DragDrop.as. The Document Class is DragGame.
The airplane, train, and covered wagon graphics are movieclips and added twice to the stage, once via manually dragged from library and once via the addChild method. None of them have instance names. The linkage is set up so that , for example, the train (TN) has a class of TN and the base class is DragDrop. Is it possible the target movieclips need to be added via addChild for them to be recognized in playback?
The target is being called by its instance name on the stage. But the program is not recognizing it. Could it be that I need i.e. "parent.ttn_mc" to go from one movie clip to another all on the same frame?
Also I do have an animation running for the first 600 frames. Then it stops to have this interaction with the user. So this code needs to work further down the timeline, not on frame 1...which is what I have seen other examples of drag/drop do.
The trace in the checkTarget function in DragGame.as does not show up in my output window.
Thanks in advance.
not null anymore, but still snapping back
Thank you! So I got the values to return with real values and not null, as you can see below. The problem is still that the modes of transportation keep snaping back and recognizing the hit target area. Below is the code of the DragDrop.as that is being imported into the DragGame.as file we were just working on.
[object TTN]
[object TAP]
[object TCW]
PHP Code:
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.filters.DropShadowFilter;
import flash.display.DisplayObject;
public class DragDrop extends MovieClip
{
public var _targetPiece:*;
public var _origX:Number;
public var _origY:Number;
public function DragDrop()
{
_origX = this.x;
_origY = this.y;
this.buttonMode = true;
this.addEventListener(MouseEvent.MOUSE_DOWN,dragMovie);
this.addEventListener(MouseEvent.MOUSE_UP, dropMovie);
}
private function dragMovie(event:MouseEvent):void
{
this.startDrag();
_origX = event.target.x;
_origY = event.target.y;
this.filters = [new DropShadowFilter()];
this.parent.addChild(this);
}
private function dropMovie(event:MouseEvent):void
{
this.stopDrag();
this.filters = [];
event.target.stopDrag();
var myTargetName:String = "t" + event.target.name + "_mc";
var myTarget:DisplayObject = getChildByName(myTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){
event.target.removeEventListener(MouseEvent.MOUSE_DOWN, dragMovie);
event.target.removeEventListener(MouseEvent.MOUSE_UP, dropMovie);
event.target.buttonMode = false;
event.target.x = myTarget.x;
event.target.y = myTarget.y;
} else {
event.target.x = _origX;
event.target.y = _origY;
}
}
public function disable():void
{
this.buttonMode = false;
this.removeEventListener(MouseEvent.MOUSE_DOWN,dragMovie);
this.removeEventListener(MouseEvent.MOUSE_UP, dropMovie);
}
}
}
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;
}
}
}
}