TypeError: Error #1010: A term is undefined and has no properties.
at SchoolBookV2_fla::MainTimeline/dragObject1()[SchoolBookV2_fla.MainTimeline::frame18:50]
我正在尝试一些简单的拖放操作。但它似乎给了我这个错误。 有人可以协助我解决它吗? 看起来它将变量v作为null ref。 是否假设按顺序排序?
代码:
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();
}
}
答案 0 :(得分:2)
for循环中的闭包是坏事。当调用事件处理程序时,它将使用v的值作为循环的最后一次迭代。
使dragObject1
和stioDragObject1
函数成为普通函数,并通过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();
}
答案 1 :(得分:1)
这里有一些问题,但我无法确定导致错误的确切原因。
首先,你不应该像这样在循环中实例化函数。请阅读我对AS3 loop doen't work的回答,以便更好地理解原因。基本上:这是一场记忆噩梦。
其次,这可能实际上是您的问题,v
不会引用拖动时选择的对象。你看,在循环运行之后(可能会在几毫秒内发生),v等于totalArray.length
。 totalArray[ totalArray.length ]
不可能存在,因为数组索引从0开始,但长度从1开始。所以这可能是问题。
这是你应该做的:
totalArray[v]
,而是执行此var cur:Sprite = e.currentTarget as Sprite;
,然后转而使用cur
。这将检索实际选择的对象。除此之外,我真的建议您阅读范围的工作原理。你似乎对什么以及如何运作有了非常基本的把握,但这远远不够。