Actionscript 3 空对象错误消息

发布于 2024-09-13 07:57:50 字数 1404 浏览 4 评论 0原文

我正在构建一个仅 AS3 的项目,并收到运行时错误,提示“无法访问空对象引用的属性或方法”。

这是我的代码:

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 part of codes....
    }

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

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 part of codes....
    }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

小猫一只 2024-09-20 07:57:50

_Player 仅在 Event.INIT 触发后定义,因此在定义 _Player 值之前的任何调用都会引发错误。

你至少应该有这个:

public function videoMain (){
     loadPlayer()
   }

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

}

但是进度事件不是静态的,所以你实际上应该有一个 EnterFrame 事件监听器,以便监听不断变化的值...

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
}

_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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文