我的代码运行正常。当用户按下1时,假设引入一个图像,当他/她按下2时将其换成另一个图像。但是,当我在先前按下相同的数字后按1或2时,我得到#2025错误。例如:按1然后再按1。
ArgumentError:错误#2025:提供的DisplayObject必须是子级 来电者。 在flash.display :: DisplayObjectContainer / removeChild() 在warren_fla :: MainTimeline / reportKeyDown2()
代码
import flash.events.KeyboardEvent;
var bdata = new image1(stage.stageWidth, stage.stageHeight);
var bdata2 = new image2(stage.stageWidth, stage.stageHeight);
var bmp = new Bitmap(bdata);
var bmp2 = new Bitmap(bdata2);
function reportKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == 49) {
//trace("1 is pressed");
bmp.x = 230;
bmp.y = 150;
addChild(bmp);
}
if (contains(bmp2)) {
removeChild(bmp2);
}
}
function reportKeyDown2(event:KeyboardEvent):void
{
if (event.keyCode == 50) {
//trace("2 is pressed");
bmp2.x = 230;
bmp2.y = 150;
addChild(bmp2);
removeChild(bmp);
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown2);
答案 0 :(得分:6)
您正在删除bmp
而不检查它是否已经是孩子。
function reportKeyDown2(event:KeyboardEvent):void
{
if (event.keyCode == 50) {
//trace("2 is pressed");
bmp2.x = 230;
bmp2.y = 150;
addChild(bmp2);
if(contains(bmp))
removeChild(bmp);
}
}
此外,您的代码可以重构为这个更简单的版本:
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
var bdata = new image1(stage.stageWidth, stage.stageHeight);
var bdata2 = new image2(stage.stageWidth, stage.stageHeight);
var bmp = new Bitmap(bdata);
var bmp2 = new Bitmap(bdata2);
function reportKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.NUMBER_1) {
swapBitmaps(bmp, bmp2);
} else if (event.keyCode == Keyboard.NUMBER_2) {
swapBitmaps(bmp2, bmp);
}
}
function swapBitmaps(first:Bitmap, second:Bitmap):void
{
first.x = 230;
first.y = 150;
addChild(first);
if(contains(second)) {
removeChild(second);
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, reportKeyDown);
答案 1 :(得分:2)
在reportKeyDown()中,尝试移动:
if (contains(bmp2)) {
removeChild(bmp2);
}
在检查密钥的if语句中:
function reportKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == 49) {
//trace("1 is pressed");
bmp.x = 230;
bmp.y = 150;
addChild(bmp);
if (contains(bmp2)) {
removeChild(bmp2);
}
}
}
在reportKeyDown2中,检查以确保bmp在删除之前是否为子项:
function reportKeyDown2(event:KeyboardEvent):void
{
if (event.keyCode == 50) {
//trace("2 is pressed");
bmp2.x = 230;
bmp2.y = 150;
addChild(bmp2);
if(contains(bmp))
{
removeChild(bmp);
}
}
}
答案 2 :(得分:0)
您可能会多次添加bmp。与删除它们相同,但如果您尝试删除不在显示列表中的子项,您将收到您看到的错误。在添加或删除的所有情况下尝试使用此代码:
if (!contains(bmp)) { addChild(bmp); } // or bmp2
if (contains(bmp)) { removeChild(bmp); } // or bmp2
----------- edit -------------
好的,显然你可以在不检查的情况下添加addChild(因为addChild会从显示树中删除对象),所以进行检查会更有效。事实上,它似乎快了约3倍。这个测试:
var clip = new MovieClip();
var i,j,tin,dur;
var tries = 100000;
for (j=0; j<5; j++) {
trace("-------------------");
tin = new Date().time;
for (i=0; i<tries; i++) { if (!contains(clip)) { addChild(clip); } }
dur = new Date().time - tin;
trace("Check Adding: "+dur);
removeChild(clip);
tin = new Date().time;
for (i=0; i<tries; i++) { addChild(clip); }
dur = new Date().time - tin;
trace("Just Adding: "+dur);
}
输出:
------------------- Check Adding: 9 Just Adding: 25 ------------------- Check Adding: 8 Just Adding: 25 ------------------- Check Adding: 9 Just Adding: 24 ------------------- Check Adding: 9 Just Adding: 24 ------------------- Check Adding: 9 Just Adding: 25