Error when loading swf game while stimulate download but not normally

余生长醉 提交于 2020-01-05 08:35:16

问题


Okay, so When I run my loader.swf, it's loads my game.swf perfectly fine. But when I stimulate download, at exactly 70%, My music of game.swf starts playing and it shows following errors:

TypeError: Error #1009: Cannot access a property or method of a null object reference. at GAME_fla::MainTimeline/frame11()

And

Error #2044: Unhandled IOErrorEvent:. text=Error #2036: Load Never Completed.

The game works perfectly fine when loaded normally. This issue persists when I upload it online and check if it's working. It doesn't. My loader works well for other swf files. How to solve this?

Code of layer 1, frame 11

  import flash.media.SoundChannel;
  var bgmusic2: loverswalk = new loverswalk;
  var bgmusic: spring = new spring;
  var forestmusic: NatureAmbiance = new NatureAmbiance;
  var cafemusic: cafeteria1 = new cafeteria1;


  var shopmusic: pamfluiteloop = new pamfluiteloop;

  var cash: CashRegisterChaChing = new CashRegisterChaChing;
  var beeps: beep = new beep;
  var musicboxsound: BlaclRockShooterMusicBox = new BlaclRockShooterMusicBox;
  var failsound: fail = new fail;
  var stgup: stageup = new stageup;
  var talkbg: talkbag = new talkbag;
  var ca: correct = new correct;
  var myChannel: SoundChannel = new SoundChannel();
  var bg: SoundChannel = new SoundChannel();
  var mb: SoundChannel = new SoundChannel();
  var myChanneltalk: SoundChannel = new SoundChannel();
  var lastpositionbg: Number = 0;
  var moodshop = 0;
  var dilbgrun = 0;
  var alertsound: alert = new alert;

Layer 2, frame 20

 var hp: int = 100;
 var day: int = 1;
 var week: int = 1;
 var money: Number = 0;
 var pendants: int = 0;
 var bracelets: int = 0;
 var comics: int = 0;
 var poetrys: int = 0;
 var tshirts: int = 0;
 var named: String = "Driver";
 var age: int = 17;
 var reply: int = 0;

 var dilrun: String = " ";
 var ncpvar: int = -1;
 var ncpvare: int = -1;
 var ncpvarm: int = -1;
 var flavie_stage: int = 0;
 var shannon_stage: int = 0;
 var vivette_stage: int = 0;
 var audrey_stage: int = 0;
 var gabriel_stage: int = 0;
 var toret: int = 0;

 function refresh1(event: Event): void {
   Object(this).bar.hp_text.text = String(hp);
   Object(this).bar.day_text.text = String(day);
   Object(this).bar.week_text.text = String(week);
   Object(this).bar.money_text.text = String(money);
 }



 function bedbtn(event: MouseEvent): void {
   alert_mc.gotoAndStop("Alert");
   alert_mc.alert_mc.alert_text.text = "You slept a night and gained a massive 100 HP. Enjoy!";

   hp = 100;
   day = day + 1;
   if (day == 8) {
     day = 1;
     week = week + 1;
   }

 }

 function woman(event: MouseEvent): void {


   if (event.target == audrey_btn) {
     if (hp >= 30) {
       hp = hp - 30;
       dilrun = "Audrey";
       dial_mc.gotoAndPlay("Dialogue");
     } else {
       alertsound.play();
       alert_mc.gotoAndStop("Alert");
       alert_mc.alert_mc.alert_text.text = "Go get some more HP silly!";
     }

   }
   if (event.target == shannon_btn) {


     if (hp >= 30) {
       hp = hp - 30;
       dilrun = "Shannon";
       dial_mc.gotoAndPlay("Dialogue");
     } else {
       alertsound.play();
       alert_mc.gotoAndStop("Alert");
       alert_mc.alert_mc.alert_text.text = "Go get some more HP silly!";
     }



   }
   if (event.target == vivette_btn) {
     if (hp >= 30) {
       hp = hp - 30;
       dilrun = "Vivette";
       dial_mc.gotoAndPlay("Dialogue");
     } else {
       alertsound.play();
       alert_mc.gotoAndStop("Alert");
       alert_mc.alert_mc.alert_text.text = "Go get some more HP silly!";
     }


   }
   if (event.target == gabriel_btn) {
     if (hp >= 30) {
       hp = hp - 30;
       dilrun = "Gabriel";
       dial_mc.gotoAndPlay("Dialogue");
     } else {
       alertsound.play();
       alert_mc.gotoAndStop("Alert");
       alert_mc.alert_mc.alert_text.text = "Go get some more HP silly!";
     }
   }
   if (event.target == flavie_btn) {
     if (hp >= 30) {
       hp = hp - 30;
       dilrun = "Flavie";
       dial_mc.gotoAndPlay("Dialogue");
     } else {
       alertsound.play();
       alert_mc.gotoAndStop("Alert");
       alert_mc.alert_mc.alert_text.text = "Go get some more HP silly!";
     }

   }
 }

 function NCP(event: MouseEvent): void {
   if (event.target == odile_btn) {

     if (hp >= 10) {
       hp = hp - 10;

       dilrun = "Odile";
       dial_mc.gotoAndPlay("Dialogue");
     } else {
       alertsound.play();
       alert_mc.gotoAndStop("Alert");
       alert_mc.alert_mc.alert_text.text = "You need more HP to talk to Odile.";
     }



   }
   if (event.target == elias_btn) {

     if (hp >= 10) {
       hp = hp - 10;

       dilrun = "Elias";
       dial_mc.gotoAndPlay("Dialogue");
     } else {
       alertsound.play();
       alert_mc.gotoAndStop("Alert");
       alert_mc.alert_mc.alert_text.text = "You need more HP to talk to Elias.";
     }



   }
   if (event.target == mikel_btn) {

     if (hp >= 10) {
       hp = hp - 10;

       dilrun = "Mikel";
       dial_mc.gotoAndPlay("Dialogue");
     } else {
       alertsound.play();
       alert_mc.gotoAndStop("Alert");
       alert_mc.alert_mc.alert_text.text = "You need more HP to talk to Mikel.";
     }



   }
 }

 function buy(event: MouseEvent): void {
   if (event.target == gifts_btn) {
     Object(this).menu_mc.gotoAndStop("Shop");
   }
   if (event.target == buy_food_btn) {
     Object(this).menu_mc.gotoAndStop("Cafe");
   }
 }


 function job(event: MouseEvent): void {
   alertsound.play();
   alert_mc.gotoAndStop("Alert");

   if (event.target == job1_btn) {
     if (hp >= 40) {
       hp = hp - 40;
       money = money + 30;
       alert_mc.alert_mc.alert_text.text = "You earnt 30 for 40 HP";
     } else if (hp < 40) {
       alert_mc.alert_mc.alert_text.text = "You don't have 40 HP to that. Better sleep or eat to gain more HP.";
     }
   }
   if (event.target == job2_btn) {
     if (hp >= 30) {
       hp = hp - 30;
       money = money + 20;
       alert_mc.alert_mc.alert_text.text = "You earnt 20 for 30 HP";
     } else if (hp < 30) {
       alert_mc.alert_mc.alert_text.text = "You don't have 30 HP to that. Better sleep or eat to gain more HP.";
     }
   }
   if (event.target == job3_btn) {
     if (hp >= 20) {
       hp = hp - 20;
       money = money + 10;
       alert_mc.alert_mc.alert_text.text = "You earnt 10 for 20 HP";
     } else if (hp < 20) {
       alert_mc.alert_mc.alert_text.text = "You don't have 20 HP to that. Better sleep or eat to gain more HP.";
     }
   }
 }

If I add them, other frames are not being able to access those variables.

Loader code before:

var myrequest: URLRequest = new URLRequest("GAME.swf");
var myloader: Loader = new Loader();
myloader.load(myrequest);

myloader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progresshandler);
function progresshandler(myevent: ProgressEvent): void {

  var myprogress: Number = myevent.target.bytesLoaded / myevent.target.bytesTotal;


  bar_mc.scaleX = myprogress;
  percentage_text.text = Math.round(myprogress * 100) + "%";
}

myloader.contentLoaderInfo.addEventListener(Event.COMPLETE, finished);
function finished(myevent: Event): void {

  addChild(myloader);
  removeChild(percentage_text);
  removeChild(bar_mc);
  removeChild(background_mc);
}

Loader Code After:

var myrequest: URLRequest = new URLRequest("GAME.swf");

import flash.net.URLRequest;
import flash.display.MovieClip;
import flash.events.Event;
var myloader: Loader = new Loader();


myloader.load(myrequest);

function progresshandler(myevent: ProgressEvent): void {

  var myprogress: Number = myevent.target.bytesLoaded / myevent.target.bytesTotal;

  bar_mc.scaleX = myprogress;
  percentage_text.text = Math.round(myprogress * 100) + "%";
}



myloader.contentLoaderInfo.addEventListener(Event.INIT, finished);

myloader.contentLoaderInfo.addEventListener(Event.COMPLETE, completes);


function finished(myevent: Event): void {

  addChild(myloader);
  removeChild(percentage_text);
  removeChild(bar_mc);
  removeChild(background_mc);
}

function completes(myevent: Event): void {


  var loaderInfo: LoaderInfo = myevent.target as LoaderInfo;
  var swf: Object = loaderInfo.content;
  swf.play();

}

回答1:


Maybe you should give a try to this event: ADDED_TO_STAGE. The ADDED_TO_STAGE event is triggered when an object is added to stage (very curious name and correlation), that is when you execute an addChild method.

Try to listen to this event in your Main class of your external SWF, so, no code will be executed before you add this respective object to stage (now you can hear the music even during the load process.

Follow some example:

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

        public class TheMainClassOfYourExternalSWF extends Sprite 
        {
               public function TheMainClassOfYourExternalSWF() 
               {
                    addEventListener(Event.ADDED_TO_STAGE, initHandler);
               }

               private function initHandler(event:Event):void 
               {
                    removeEventListener(Event.ADDED_TO_STAGE, initHandler);
                    trace('this is the stage:', stage);
                    trace('this is my parent:', this.parent);
                    // here you start to include your logic
               }
        }
  }

if you are using a FLA file and adding your code using timeline, you can try:

 addEventListener(Event.ADDED_TO_STAGE, initHandler);

 function initHandler(event:Event):void 
 {
         removeEventListener(Event.ADDED_TO_STAGE, initHandler);
         // here you start to include your logic, call your functions, initialise your instances...
 }



回答2:


When loading an external swf there are 2 things to be aware of.

First the first frame of the loaded swf start playing when the Event.INIT triggers not when the Event.COMPLETE triggers. The event init triggers when the first frame can be played while the complete triggers when the whole swf is loaded.

Second stage or root is only available in the loaded swf when it had be added to a display list that is added to the stage. A loaded swf has no stage or root when the Event.INIT triggers so the first frame of the swf should not have any reference to stage or root.

If you do not use a document class with your swf then make your first frame empty with just a stop() then once loaded and added to a display list make your swf go to frame 2 where you have all your code.



来源:https://stackoverflow.com/questions/28621751/error-when-loading-swf-game-while-stimulate-download-but-not-normally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!