访问动态添加的MC中的实例

发布于 2024-10-31 12:51:32 字数 705 浏览 0 评论 0原文

我使用以下代码动态添加了一个影片剪辑:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素食主义者 2024-11-07 12:51:33

我认为该错误是由 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 into apie.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.

潜移默化 2024-11-07 12:51:33

您收到该错误的原因是 cherry 不是 cPie 对象的属性或方法,它是 cPie 的子显示对象的实例名称 显示对象容器。要访问 cherry 显示对象,您必须使用 cPie 对象继承的 DisplayObjectContainer 方法,即 getChildByName()方法。幸运的是,frankhermes 已经在另一个答案中对此进行了解释,因此我将在以下示例中解释另一种(并且可以说是更好的)访问子显示对象的方法:

CherryPie.as:

package display
{
    import flash.display.MovieClip;

    public class CherryPie extends MovieClip
    {
        private var _cherry:MovieClip;

        public function get cherry():MovieClip
        {
            return _cherry;

        }// end function

        public function CherryPie()
        {
            _cherry = cherryMC;

        }// end function

    }// end class

}// end package

Main.as:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite 
    {
        public function Main():void 
        {
            init();

        }// end function

        private function init():void 
        {
            var cherryPie:CherryPie = new CherryPie();
            cherryPie.x = 100;
            cherryPie.y = 100;
            cherryPie.cherry.gotoAndStop(2);
            addChild(cherryPie);

        }// end function

    }// end class

}// end package

通过为您的cherryPie_mc 影片剪辑中,您可以创建一个名为 cherry 的属性,并在启动类时将 cherry 子显示对象分配给它。通过这种方式,您可以直接访问 cherry 子显示对象,如下所示:

cherryPie.cherry.gotoAndStop(2);

与以下方式相反:

cherryPie.getChildByName("cherryMC").gotoAndStop(2);

您可以将 CherryPie 类设置为 cherryPie_mc 影片剪辑的基础类如下:

注意:交易品种属性中的“名称”应为“cherryPie_mc”
Symbol Properties

也适合那些想知道为什么我没有将 cherry 设置为公共属性的

public var cherry:MovieClip;

人使用 getter 方法访问私有属性的

private var _cherry:MovieClip;

public function get cherry():MovieClip
{
    return _cherry;

}// end function

目的是使 Cherry 对象变为只读,这是通过省略 setter 方法来完成的。我将其设置为只读的原因是为了避免出现以下情况:

cherryPie.cherry = new FakeCherry();

The reason that you got that error is because cherry is not a property or method of the cPie object, its the instance name for a child display object of your cPie display object container. To access the cherry display object you have to use the cPie object's inherited DisplayObjectContainer methods, namely the getChildByName() 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:

package display
{
    import flash.display.MovieClip;

    public class CherryPie extends MovieClip
    {
        private var _cherry:MovieClip;

        public function get cherry():MovieClip
        {
            return _cherry;

        }// end function

        public function CherryPie()
        {
            _cherry = cherryMC;

        }// end function

    }// end class

}// end package

Main.as:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite 
    {
        public function Main():void 
        {
            init();

        }// end function

        private function init():void 
        {
            var cherryPie:CherryPie = new CherryPie();
            cherryPie.x = 100;
            cherryPie.y = 100;
            cherryPie.cherry.gotoAndStop(2);
            addChild(cherryPie);

        }// end function

    }// end class

}// end package

By creating a base class for your cherryPie_mc movie clip, you can create a property called cherry and assign the cherry child display object to it upon initiating the class. This way you can directly access the cherry child display object like:

cherryPie.cherry.gotoAndStop(2);

as opposed to:

cherryPie.getChildByName("cherryMC").gotoAndStop(2);

You can set the CherryPie class as the cherryPie_mc movie clip's base class as follows:

NOTE: The "Name" in the symbol properties should be "cherryPie_mc"
Symbol Properties

Also for those wondering why I didn't set cherry as a public property

public var cherry:MovieClip;

and instead used a getter method to access a private property

private var _cherry:MovieClip;

public function get cherry():MovieClip
{
    return _cherry;

}// end function

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:

cherryPie.cherry = new FakeCherry();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文