TypeError: Error #1010: A term is undefined and has no properties.
at SchoolBookV2_fla::MainTimeline/dragObject1()[SchoolBookV2_fla.MainTimeline::frame18:50]
我正在嘗試一些簡單的拖放操作。但它似乎給了我這個錯誤。 有人可以幫我解決嗎? 它看起來像是將變量v作爲null參考。 它假設按不同的順序排列?TypeError:錯誤#1010:術語未定義且沒有屬性。
代碼:
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();
}
}
很酷thx一堆。它確實工作得很好。但我有一個問題,如果我想刪除放在包裏的盒子,我該怎麼做? – tailedmouse 2013-03-15 01:08:34
如果它是'bag'的孩子,那麼你可以用'bag.removeChild(sprite)'''在'hitTestObject'檢查中刪除它。或者你可以通過'sprite.parent.removeChild(sprite);' – 2013-03-15 01:19:38
從它的父母中刪除它哦,我看到謝謝,我用sprite.parent ....它工作出色! – tailedmouse 2013-03-15 15:02:37