访问动态添加的MC中的实例
我使用以下代码动态添加了一个影片剪辑:
var apie=new cPie()
apie.x=100
apie.y=100
stage.addChild(apie)
现在我的舞台上有一个馅饼。嗯。假设这就像通过拖放放置在舞台上的影片剪辑一样,我添加了它来更改饼中的实例。
var apie=new cPie()
apie.x=100
apie.y=100
apie.cherry.gotoAndStop(2)
stage.addChild(apie)
cherry 是 cPie 影片剪辑中的一个实例,cPie 影片剪辑是另一个由 3 帧组成的影片剪辑。我希望它进入第二帧。通常,这样做是可行的,但是当尝试通过 ActionScript 添加影片剪辑时,我会遇到以下运行时错误:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at pies_fla::MainTimeline/frame1()
与往常一样,返回的错误根本无法帮助我解决此问题。这就像“隐性强制”一样晦涩难懂,这对我不是英语专业的人来说毫无意义。我所知道的是我做错了什么,结果我得到了这个错误。解决这个问题的正确方法将非常感激。
PS 我打算循环使用它。请考虑到这一点。
I've added a movie clip dynamically using the following code:
var apie=new cPie()
apie.x=100
apie.y=100
stage.addChild(apie)
I now have a pie on my stage. Yum. Assuming this works like a movie clip placed on the stage by dragging and dropping, I added this in to change an instance in the pie.
var apie=new cPie()
apie.x=100
apie.y=100
apie.cherry.gotoAndStop(2)
stage.addChild(apie)
cherry is an instance in the cPie movie clip which is another movie clip consisting of 3 frames. I want it to go to the second frame. Usually, doing it this way would work, but when trying with a movie clip added through ActionScript I'm faced with the following runtime error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at pies_fla::MainTimeline/frame1()
As always, the error returned doesn't help me solve this problem at all. It's about as obscure as the "implicit coercion" thing, which makes no sense to me not being an English major. What I do know is that I'm doing something wrong and as a result I get this error. The correct way to go about this would be very appreciated.
P.S. I plan to use this in a loop. Please take that into consideration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为该错误是由
apie.cherry.gotoAndStop(2)
行引起的。您可能必须将其更改为apie.getChildByName("cherry").gotoAndStop(2)
但由于您的饼图是一个影片剪辑,因此子项通常不会立即实例化。解决这个问题的一种方法是在尝试访问 MovieClip 的子级之前等待帧重绘。
I suppose the error is caused by the line
apie.cherry.gotoAndStop(2)
. You might have to change that intoapie.getChildByName("cherry").gotoAndStop(2)
but since your pie is a movieclip, children are very often not instantiated right away.A way to get around that is by waiting for a frame redraw before you try to access children of MovieClips.
您收到该错误的原因是
cherry
不是cPie
对象的属性或方法,它是cPie 的子显示对象的实例名称
显示对象容器。要访问cherry
显示对象,您必须使用cPie
对象继承的DisplayObjectContainer
方法,即getChildByName()
方法。幸运的是,frankhermes 已经在另一个答案中对此进行了解释,因此我将在以下示例中解释另一种(并且可以说是更好的)访问子显示对象的方法:CherryPie.as:
Main.as:
通过为您的
cherryPie_mc
影片剪辑中,您可以创建一个名为cherry
的属性,并在启动类时将cherry
子显示对象分配给它。通过这种方式,您可以直接访问cherry
子显示对象,如下所示:与以下方式相反:
您可以将
CherryPie
类设置为cherryPie_mc
影片剪辑的基础类如下:注意:交易品种属性中的“名称”应为“cherryPie_mc”
也适合那些想知道为什么我没有将
cherry
设置为公共属性的人使用 getter 方法访问私有属性的
目的是使 Cherry 对象变为只读,这是通过省略 setter 方法来完成的。我将其设置为只读的原因是为了避免出现以下情况:
The reason that you got that error is because
cherry
is not a property or method of thecPie
object, its the instance name for a child display object of yourcPie
display object container. To access thecherry
display object you have to use thecPie
object's inheritedDisplayObjectContainer
methods, namely thegetChildByName()
method. Fortunately this is already explained in another answer by frankhermes, so I'll be explaining another(and arguably better) approach to accessing the child display object in the following example:CherryPie.as:
Main.as:
By creating a base class for your
cherryPie_mc
movie clip, you can create a property calledcherry
and assign thecherry
child display object to it upon initiating the class. This way you can directly access thecherry
child display object like:as opposed to:
You can set the
CherryPie
class as thecherryPie_mc
movie clip's base class as follows:NOTE: The "Name" in the symbol properties should be "cherryPie_mc"
Also for those wondering why I didn't set
cherry
as a public propertyand instead used a getter method to access a private property
was to make the cherry object read-only which is done by omitting the setter method. The reason I made it read-only was to avoid something like the following: