调用内getChildByName / getChild在孩子吗?孩子、getChildByName、getChild

2023-09-08 13:57:45 作者:是我太过想你了i

我发现我不能针对主MC内的对象,如果我使用getChildAt / getChildByName。这将返回我

 错误#1119:的DisplayObject:可能未定义的属性someProperty通过与静态类型flash.display使用参考访问。
 

我是想使用类似

  this.getChildAt(0).getChildByName(对象名)......
 

解决方案

getChildByName 等得到孩子的方法返回类型的对象的DisplayObject 。前适当属性或方法不属于显示对象,则必须强制转换。的同时也要记住,这些让孩子方法属于的DisplayObjectContainer 类 - 所以你不能链就像你正在试图做的方式。

  VAR容器:级DisplayObjectContainer =的DisplayObjectContainer(getChildAt(3));
变种MC:影片剪辑=影片剪辑(container.getChildByName(intro_movie));
mc.gotoAndStop(4);

//要么

VAR容器:级DisplayObjectContainer = getChildAt(3)的DisplayObjectContainer;
变种MC:影片剪辑= container.getChildByName(intro_movie)作为影片剪辑;
mc.gotoAndStop(4);

//要么
影片剪辑(级DisplayObjectContainer(getChildAt(3))getChildByName(intro_movie)。)gotoAndStop(4)。
 
java构建树形接口两种实现方法 数据库 weixin 42518055的博客 CSDN博客

类名(OBJ)如果转换失败语法将抛出一个错误铸造;与铸造OBJ的类名返回没有任何错误空 - 这可能会导致混乱以后,因为它可能会导致错误1009(空引用)在意外的地方

I found out that I can't target a object inside the main MC if I use getChildAt / getChildByName. It will return me

Error #1119: Access of possibly undefined property someProperty through a reference with static type flash.display:DisplayObject.

I was trying to use something like

this.getChildAt(0).getChildByName("objectName")....

解决方案

getChildByName and other get child methods return an object of type DisplayObject. You must cast it appropriately before properties or methods not belonging to display objects. Also bear in mind that these get child methods belong to DisplayObjectContainer class - so you cannot chain like the way you're trying to do.

var container:DisplayObjectContainer = DisplayObjectContainer(getChildAt(3));
var mc:MovieClip = MovieClip(container.getChildByName("intro_movie"));
mc.gotoAndStop(4);

//or

var container:DisplayObjectContainer = getChildAt(3) as DisplayObjectContainer;
var mc:MovieClip = container.getChildByName("intro_movie") as MovieClip;
mc.gotoAndStop(4);

//or
MovieClip(DisplayObjectContainer(getChildAt(3)).getChildByName("intro_movie")).gotoAndStop(4);

Casting with ClassName(obj) syntax will throw an error if cast fails; casting with obj as ClassName returns null without any errors - this might lead to confusions later as it can lead to error 1009 (null reference) at unexpected locations.