(AS3)DisplayObjectContainer.contains + DisplayObjectContainer.removeChild 引发的狗血BUG
2012-09-08 22:42
543 查看
先看一段代码:
以上代码能正常执行,再看一段代码:
以上代码不能正常执行,而且还会抛一个 ArgumentError 出来= =
会不会对 contains 这个方法有些误解了?
看官方API说明:
确定指定显示对象是 DisplayObjectContainer 实例的子项还是该实例本身。搜索包括整个显示列表(其中包括此 DisplayObjectContainer 实例)。孙项、曾孙项等,每项都返回 true。
孙项、曾孙项也返回true啊亲,fuck
也就是说判断一个对象能不能 removeChild 另一个对象并不能这样写,而是需要这样写:
contains 返回true 只能说明该对象存在于显示列表中,并不能说明这个对象就是它的孩子,也许它只是个孙子= =+
var sp1:Sprite = new Sprite; var sp2:Sprite = new Sprite; sp1.addChild(sp2); if (sp1.contains(sp2)) { sp1.removeChild(sp2); }
以上代码能正常执行,再看一段代码:
var sp1:Sprite = new Sprite; var sp2:Sprite = new Sprite; var sp3:Sprite = new Sprite; sp1.addChild(sp2); sp2.addChild(sp3); if (sp1.contains(sp3)) { sp1.removeChild(sp3); }
以上代码不能正常执行,而且还会抛一个 ArgumentError 出来= =
会不会对 contains 这个方法有些误解了?
看官方API说明:
确定指定显示对象是 DisplayObjectContainer 实例的子项还是该实例本身。搜索包括整个显示列表(其中包括此 DisplayObjectContainer 实例)。孙项、曾孙项等,每项都返回 true。
孙项、曾孙项也返回true啊亲,fuck
也就是说判断一个对象能不能 removeChild 另一个对象并不能这样写,而是需要这样写:
var sp1:Sprite = new Sprite; var sp2:Sprite = new Sprite; var sp3:Sprite = new Sprite; sp1.addChild(sp2); sp2.addChild(sp3); if (sp3.parent == sp1) { sp1.removeChild(sp3); }
contains 返回true 只能说明该对象存在于显示列表中,并不能说明这个对象就是它的孩子,也许它只是个孙子= =+
相关文章推荐
- 发现一个新的方法contains(child:DisplayObject)
- AS3容器:DisplayObjectContainer与Sprite、MovieClip
- Create another container to place the displayObject in:
- bug_ _fragment_“The specified child already has a parent. You must call removeView"的解决以及产生的原因
- as3 Loader.load()时出现错误“Error #2025: 提供的 DisplayObject 必须是调用者的子级”的解决方案
- DisplayObjectContainer 属性和方法
- AS3实现对对象(DisplayObject)的鼠标拖动(Drag)跟随
- Flex DisplayObject/DisplayObjectContainer
- BUG笔记:Win XP IE8下HTML Parsing Error: Unable to modify the parent container element before the child
- AS3中DisplayObject属性在Matrix中的计算方式
- BUG:The specified child already has a parent. You must call removeView() on the child's parent first
- BUG笔记:Win XP IE8下HTML Parsing Error: Unable to modify the parent container element before the child
- Fixjs——显示容器基类DisplayObjectContainer
- DisplayObjectContainer的getChildAt()方法
- 关于AS3中SharedObject对象的一些Bug现象……
- 【BUG】 ContainerBase.addChild---IDEA 和Tomcat部署非maven的web项目
- BUG笔记:Win XP IE8下HTML Parsing Error: Unable to modify the parent container element before the child
- 每天进步一点点——/srv/node不存在导致object-updater和container-updater崩溃(Swift Bug)
- 多继承引发的诡异bug (cocos2d-x CCObject)
- as3中DisplayObject对象的rotation属性