开发者

Actionscript 3 Null Object Error Message

I am building a AS3 only project and got runtime error that said "Cannot access a property or method of a null object reference."

Here is my code:

main.as

       public class videoMain extends Sprite{

         private var videoPlayer:Player;

       public function videoMain (){
         loadPlayer()
         loadProgress();
       }

       private function loadProgress():void{

        //the code below gave me null object error.....     
        var byteLoaded:Number=videoPlayer.videoBytesLoaded; //the problem code
        var byteTotal:Number=videoPlayer.videoBytesTotal;  //the problem code

        var percent:Number=Math.floor(byteLoaded/byteTotal)*100;
        }

        private function loadPlayer():void{

        videoPlayer= new Player();
        videoPlayer.createPlayer();
    }

}

Player.as

public function createPlayer():void{

    _loader = new Loader();
    _loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
    _loader.load(new URLRequest(playerType));

    }

public function get videoBytesLoaded():Number{
    return _Player.getVideoBytesLoaded();  //youtube api method
    }

public function get videoBytesTotal():Number{
    return _Player.getVideoBytesTotal;    //youtube api method
    }

 private function onLoaderInit(event:Event):void {
    _Player=_loader.content;
        //only show开发者_JAVA百科 part of codes....
    }

I appreciate any help....Thanks!!!!!


_Player is only defined after the Event.INIT has fired so any call before the _Player value is defined will throw an error.

You should, at the minimum , have this:

public function videoMain (){
     loadPlayer()
   }

private function onLoaderInit(event:Event):void {
    _Player=_loader.content;
    //only show part of codes....
    loadProgress();

}

but progress events are not static so really you should have an enterFrame event listener in order to listen to the changing values...

private function onLoaderInit(event:Event):void {
    _Player=_loader.content;
    //only show part of codes....

    addEventListener(Event.ENTER_FRAME , enterFrameListener);
}

private function enterFrameListener(event:Event):void
{
    loadProgress();
    // and here you add some way to remove this event listener when 
    // the video is fully loaded
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜