Reputation: 365
TypeError: Error #1010: A term is undefined and has no properties.
at SchoolBookV2_fla::MainTimeline/dragObject1()[SchoolBookV2_fla.MainTimeline::frame18:50]
I am trying out some simple drag and drop. But it seems to be giving me this error. Can somebody please assist me with solving it? It looks like it's taking in the variable v as null ref. Is it suppose to be sequenced differently?
Code:
import flash.sampler.NewObjectSample;
import flash.display.Sprite;
var bag : Sprite = new Sprite ();
var book1: Sprite = new Sprite ();
var book2: Sprite = new Sprite ();
var book3: Sprite = new Sprite ();
var book4: Sprite = new Sprite ();
var totalArray: Array = new Array ();
var v:int = 0;
bag.graphics.beginFill(0xFF6666);
bag.graphics.drawRect( 30,30, 100, 150);
bag.graphics.endFill();
addChild(bag);
book1.graphics.beginFill(0xCC6666);
book1.graphics.drawRect(300,300, 100, 150);
book1.graphics.endFill();
addChild(book1);
book2.graphics.beginFill(0xCC6666);
book2.graphics.drawRect( 150,150, 100, 150);
book2.graphics.endFill();
addChild(book2);
book3.graphics.beginFill(0xCC6666);
book3.graphics.drawRect( 200,200, 100, 150);
book3.graphics.endFill();
addChild(book3);
book4.graphics.beginFill(0xCC6666);
book4.graphics.drawRect( 80,80, 100, 150);
book4.graphics.endFill();
addChild(book4);
totalArray[totalArray.length] = book1;
totalArray[totalArray.length] = book2;
totalArray[totalArray.length] = book3;
totalArray[totalArray.length] = book4;
for (v; v < totalArray.length; v++)
{
trace(totalArray.length);
totalArray[v].addEventListener(MouseEvent.MOUSE_UP, stopdragObject1);
totalArray[v].addEventListener(MouseEvent.MOUSE_DOWN, dragObject1);
function dragObject1(e:MouseEvent)
{
totalArray[v].startDrag();
}
function stopdragObject1(l:MouseEvent)
{
totalArray[v].startDrag(false);
trace("exists");
if (totalArray[v].hitTestObject(bag))
{
totalArray[v].removeEventListener(MouseEvent.MOUSE_UP, stopdragObject1);
totalArray[v].removeEventListener(MouseEvent.MOUSE_DOWN, dragObject1);
trace("itworks");
totalArray[v].x = xPos;
totalArray[v].y = yPos;
trace("it doesn'twork");
}
else
{
totalArray[v].x = xPos;
totalArray[v].y = yPos;
trace("it doesn'twork");
}
totalArray[v].stopDrag();
}
}
Upvotes: 1
Views: 8668
Reputation: 8149
There are a few things of issue here, though I cannot pinpoint what exactly is causing the error.
First and foremost, you should never instantiate functions within a loop like that. Please read my answer for AS3 loop doen't work to better understand why. Basically: it is a memory nightmare.
Second, and this may actually be your issue, v
does not refer to the object being selected when dragging. You see, after the loop runs (which will likely happen in a few milliseconds), v equals totalArray.length
. totalArray[ totalArray.length ]
cannot possible exist because an array index starts at 0, but length starts at 1. So that may be the issue.
Here's what you should do:
totalArray[v]
as you are now, do this var cur:Sprite = e.currentTarget as Sprite;
and then refer to cur
instead. That will retrieve the object that was actually selected.Beyond that, I really suggest you read up on how scope works. You seem to have a very basic grasp of what and how it works, but it is far from adequate.
Upvotes: 1
Reputation: 13532
Closures inside for loops are bad mojo. When the event handler is called it will use the value of v for the last iteration of the loop.
Make the dragObject1
and stioDragObject1
functions normal functions and access the element through event.target
for (v; v < totalArray.length; v++)
{
trace(totalArray.length);
totalArray[v].addEventListener(MouseEvent.MOUSE_UP, stopdragObject1);
totalArray[v].addEventListener(MouseEvent.MOUSE_DOWN, dragObject1);
}
private function dragObject1(e:MouseEvent)
{
e.target.startDrag();
}
private function stopdragObject1(l:MouseEvent)
{
var sprite:Sprite = l.target as Sprite;
if (sprite.hitTestObject(bag))
{
sprite.removeEventListener(MouseEvent.MOUSE_UP, stopdragObject1);
sprite.removeEventListener(MouseEvent.MOUSE_DOWN, dragObject1);
sprite.x = xPos;
sprite.y = yPos;
}
else
{
sprite.x = xPos;
sprite.y = yPos;
}
sprite.stopDrag();
}
Upvotes: 2