开发者

Accessing an instance in a dynamically added MC

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 s开发者_如何学编程econd 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.


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.


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"

Accessing an instance in a dynamically added MC

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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜