문제

I have some problem with my preloader.

Preloader Code:

import flash.net.URLRequest;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;

var game:MovieClip
var added:Boolean;

var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("source.swf");
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
mLoader.load(mRequest);

function onCompleteHandler(e:Event):void {
    game = e.currentTarget.content
    game.alpha = 0;
}
function onProgressHandler(e:ProgressEvent):void {
    loader.loadBar.setProgress(e.bytesLoaded, e.bytesTotal);
}

addEventListener(Event.ENTER_FRAME, function(e:Event):void {
                    if(game != null){
                        if(!added) {
                            addChild(game);
                            added = true;
                        }
                        if(game.alpha < 1) game.alpha += 0.1;

When I load my game console returns TypeError: Error #1009: Cannot access a property or method of a null object reference.

I turn on permit debugging in game and again load. Now console returns TypeError: Error #1009: Cannot access a property or method of a null object reference. at main()[C: \Users\Lukasz\Desktop\Flash\rs\main.as:141]; So I checked 141 line and since 141 to 155 I have keyboard events.

    stage.addEventListener(KeyboardEvent.KEY_UP, function(e:KeyboardEvent):void {
                            if(e.keyCode == 32 && moveAvailable) {
                                startEvent();

                            }else if(e.keyCode == 32) {
                                moveAvailable = true;
                            }
                            moveSpeed = 70;
                          });
    stage.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void {
                            if(e.keyCode == 32) {
                                moveSpeed = 140
                                if(!startBtn.enb) moveAvailable = false;
                            }
                          });

When I get comment /**/ between this code game load correctly. By the way I try this.parent and parent. instead of stage. but nothing changed :(

Someone have idea on this problem ?

도움이 되었습니까?

해결책

You need check stage before use it

if (stage) {
   addStageEvent();

} else {
   this.addEventListener(Event.ADDED_TO_STAGE, addStageEvent);
}

function addStageEvent(e:Event = null):void {
     //put the 141-155 line code here
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top