Actionscript 3 AS3删除输入框中的项目
我生成了随机气泡,我使用了我在网上找到的一个代码。现在我想要一个点击事件,它将隐藏一个随机气泡 这就是我使用的代码 我让泡泡跑得很好 我已经试过了,但到目前为止运气不好Actionscript 3 AS3删除输入框中的项目,actionscript-3,flash-cs5,Actionscript 3,Flash Cs5,我生成了随机气泡,我使用了我在网上找到的一个代码。现在我想要一个点击事件,它将隐藏一个随机气泡 这就是我使用的代码 我让泡泡跑得很好 我已经试过了,但到目前为止运气不好 addEventListener(MouseEvent.CLICK, eventListener); function eventListener(eventObject:MouseEvent) { bubbles[i].splice(i,1,bubbles[i]); } 我尝试使用数组,但它返回此输
addEventListener(MouseEvent.CLICK, eventListener);
function eventListener(eventObject:MouseEvent) {
bubbles[i].splice(i,1,bubbles[i]);
}
我尝试使用数组,但它返回此输出
TypeError:Error#2007:参数子项必须为非null。
在flash.display::DisplayObjectContainer/removeChild()中
at函数/()
TypeError:Error#2007:参数子项必须为非null。
在flash.display::DisplayObjectContainer/removeChild()中
在Function/()中,您可以尝试在不使用原始数组中的随机元素的情况下创建新数组。然后只需将旧数组重新分配给新数组,例如
// get the random index to remove element at
var randomIndex:int = 0 + bubbles.length * Math.random();
var index:int = 0;
// create new array containing all apart from the chosen one
var newBubbles:Array = [];
for each (var item:Object in bubbles) {
if (index != randomIndex) {
newBubbles.push(item);
}
index++;
}
// here you go new array without one random item
bubbles = newBubbles;
或者类似的东西。如果数组中有气泡,那么应该可以
var randomIndex:int = int(Math.random() * bubbles.length);
parent.removeChild(bubbles[randomIndex]);
bubbles.splice(randomIndex, 1);
请注意,您也必须从显示列表中删除气泡。这里只是Baris Usakli代码的一个小修订,如果您希望删除单击的气泡
var bubbles:Array = [];
function makeBubbles()
{
for(var i:int=0;i<100;i++)
{
var bubble:Bubble = new Bubble();
bubbles.push(bubble);
addChild(bubble);
bubble.addEventListener(MouseEvent.CLICK, eventListener);
}
}
function eventListener(eventObject:MouseEvent) {
var clickedBubbleIndex:int = bubbles.indexOf(eventObject.currentTarget);
parent.removeChild(eventObject.currentTarget);
bubbles.splice(clickedBubbleIndex:int, 1);
}
var气泡:数组=[];
函数makeBubbles()
{
对于(var i:int=0;i试试这个
bubbles.addEventListener(MouseEvent.CLICK, eventListener); // place this listener in moveBubbles function.
function eventListener(eventObject:MouseEvent):void {
eventObject.currentTarget.visible = false;
}
虽然这是可行的,但它需要额外的CPU时间和内存来生成新的数组(指针列表,所以我想这不是什么大问题)。此外,如果您依赖于通过引用修改传入的数组参数,那么替换数组也行不通(取而代之的是,您必须从函数返回新数组,并在调用函数的地方将其分配给原始数组……这会变得很麻烦)。也就是说,这个答案有效,有时是一种更简单的方法。谢谢!我先尝试了这个方法,它出现在输出框TypeError:Error#2007:参数child必须为非null。在flash.display::DisplayObjectContainer/removeChild()at Function/()我的代码假设了两件主要事情,即气泡包含显示对象(电影剪辑或精灵)上注册了单击处理程序。我在这一行中遇到一个错误,bubbles.splice(clickedBubbleIndex:int,1);表示错误:冒号前应为rightparen