我的代码很好用。当用户按1时,应该输入一个图像,当他/她按2时,将其替换为另一个图像。但是,当我在先前按相同的数字之后按1或2时,我会得到一个#2025错误。例:按1再按1。
ArgumentError:错误#2025年:提供的DisplayObject必须是调用方的子级。在flash.display::DisplayObjectContainer/removeChild() at warren_fla::MainTimeline/reportKeyDown2 2()
代码
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);发布于 2013-12-10 18:49:12
您正在删除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);发布于 2013-12-10 18:49:24
在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之前,请检查是否是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);
}
}
} https://stackoverflow.com/questions/20502354
复制相似问题