我正在尝试在数组中添加MovieClip的实例。 House Class内部是一个名为HouseObjects的属性。在该数组中,我创建了一个Comp和一个Light类。 MovieClips通过链接动态放置在舞台上。 MovieClips也充当“切换按钮”。如果按钮状态为ON,则值为1.如果按钮状态为OFF,则值为0.
如果值为1,我试图在onList数组中添加MovieClip实例。在该数组内部将是按钮状态为ON的所有实例。
我创建了一个名为objSelect的属性。
var objSelect:Object;
该变量保持选中currentTarget。我试图将它传递给function trackItems
,根据按钮状态在onList数组中推送/弹出它。
我收到此行的错误: onList.pop(objSelect); 参数数量不正确。预计不超过0。
public class House extends MovieClip
{
var HouseObjects:Array = new Array();
var onList:Array = []; // instances added to this array that have a bstatus ON
var power:int; // holds value of individual House Objects
var bstate:int; // 0 or 1 (ON or OFF)
var bstatus:int;
var userInput:int; // stores user data (of selected data);
//holds value of e.currentTarget.power
var currentPower:int; // stores current power
var objSelect:Object;
public function House()
{
// Instances are MovieClip "toggle buttons"
HouseObjects[0] = new Comp(); // creates instance of Comp
HouseObjects[1] = new Light(); // creates instance of Light
}
function toggleClick(e:MouseEvent) {
// go to appropriate frame
if (e.currentTarget.currentFrame == 2)
{
e.currentTarget.gotoAndStop(3);
e.currentTarget.bstate = 1;
}
if (e.currentTarget.currentFrame == 4)
{
e.currentTarget.gotoAndStop(1);
e.currentTarget.bstate = 0;
}
bstatus = e.currentTarget.bstate;
objName = e.currentTarget.name;
trackItems(objSelect, bstatus);
} // end of function toggle click
function trackItems(objSelect:Object, bstatus:int):void
{
if (bstatus == 0) {
// remove objSelect from Array onList
} else if (bstatus == 1) {
onList.push(objSelect);
//add to Array onList
}
}
// function called when user clicks on update button
function updateStage():void
{
for (var i:int = 0; i<=onList.length;i++) {
addChild(onList[i]);
}
}
}
答案 0 :(得分:1)
http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/Array.html#pop%28%29
Pop从数组中删除最后一个元素,如果要删除特定元素,请使用带有deleteCount == 0的Array.splice。
答案 1 :(得分:1)
创建一个函数,找到需要删除的项,并在objSelect中传入。找到项目后,再使用splice()。
function trackItems(objSelect:Object, bstatus:int):void
{
if (bstatus == 0) {
//remove instance from onList array
// call function removeArrayItem
removeArrayItem(objSelect);
} else if (bstatus == 1) {
//remove instance from onList array
onList.push(objSelect);
}
}
function removeArrayItem(objSelect:Object):void
{
var arrayLength:int = onList.length;
// Loop through array to find item that needs to be removed
for (var i:int=0; i<arrayLength; i++)
{
if (onList[i] == objSelect)
{
onList.splice(i, 1);
}
}
}
答案 2 :(得分:1)
您不需要通过数组循环以找到要删除的元素,只需使用indexOf方法:
var index:int = onList.indexOf(objSelect); onList.splice( index , 1 );
我建议只将对象的名称添加到onList数组中,这样可以使比较更直接,更不容易出错
//if the button status is On onList.push(objSelect.name); //if the button status is Off var index:int = onList.indexOf(objSelect.name); onList.splice( index , 1 );
然后您可以像这样更新舞台:
function updateStage():void
{
for (var i:int = 0; i<=HouseObjects.length;i++)
{
//if the onList Array contains the current name
if( onList.indexOf(HouseObjects[i].name) != -1 )
addChild(HouseObjects[i]);
}
}
答案 3 :(得分:0)
pop删除数组的最后一项。如果要删除给定的项目,则需要将数组的尾部向上移动到要删除的元素的位置
答案 4 :(得分:0)
我的建议是使用更高级的集合,例如
http://livedocs.adobe.com/flex/3/langref/mx/collections/ArrayCollection.html#methodSummary
这里是修改后的代码:
public class House extends MovieClip
{
var HouseObjects:Array = new Array();
var onList:ArrayCollection = new ArrayCollection(); // instances added to this array that have a bstatus ON
var power:int; // holds value of individual House Objects
var bstate:int; // 0 or 1 (ON or OFF)
var bstatus:int;
var userInput:int; // stores user data (of selected data);
//holds value of e.currentTarget.power
var currentPower:int; // stores current power
public function House()
{
// Instances are MovieClip "toggle buttons"
HouseObjects[0] = new Comp(); // creates instance of Comp
HouseObjects[1] = new Light(); // creates instance of Light
}
function toggleClick(e:MouseEvent) {
// go to appropriate frame
if (e.currentTarget.currentFrame == 2)
{
e.currentTarget.gotoAndStop(3);
e.currentTarget.bstate = 1;
}
if (e.currentTarget.currentFrame == 4)
{
e.currentTarget.gotoAndStop(1);
e.currentTarget.bstate = 0;
}
bstatus = e.currentTarget.bstate;
objName = e.currentTarget.name;
trackItems(objSelect, bstatus);
} // end of function toggle click
function trackItems(objName:Object, bstatus:int):void
{
if (bstatus == 0) {
onList.removeItemAt(onList.getItemIndex(objName));
// remove from onList
} else if (bstatus == 1) {
onList.addItem(objName);
//add to onList
}
}
// function called when user clicks on update button
function updateStage():void
{
for (var i:int = 0; i<=onList.length;i++) {
addChild(onListgetItemAt(i));
}
}