开发者问题收集

加载swf游戏时出错,但刺激下载不正常

2015-02-20
118

好的,当我运行我的 loader.swf 时,它可以完美地加载我的 game.swf。但是当我启动下载时,在 70% 处,我的 game.swf 音乐开始播放,并显示以下错误:

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

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

正常加载时,游戏运行正常。当我将其上传到网上并检查它是否正常工作时,此问题仍然存在。它不工作。我的加载器对其他 swf 文件运行良好。如何解决这个问题?

第 1 层、第 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;

第 2 层、第 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.";
     }
   }
 }

如果我添加它们,其他帧将无法访问这些变量。

之前的加载器代码:

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);
}

之后的加载器代码:

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

}
2个回答

也许您应该尝试一下此事件: ADDED_TO_STAGE 。 当将对象添加到舞台时(名称和相关性非常奇怪),即执行 addChild 方法时,将触发 ADDED_TO_STAGE 事件。

尝试在外部 SWF 的主类中监听此事件,因此,在将此相应对象添加到舞台之前不会执行任何代码(现在您甚至可以在加载过程中听到音乐。

请遵循一些示例:

 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
               }
        }
  }

如果您使用的是 FLA 文件并使用时间轴添加代码,您可以尝试:

 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...
 }
gabriel
2015-02-20

加载外部 swf 时,需要注意 2 件事。

首先,加载的 swf 的第一帧在 Event.INIT 触发时开始播放,而不是在 Event.COMPLETE 触发时。当第一帧可以播放时,事件 init 会触发,而当加载整个 swf 时,事件 complete 会触发。

第二阶段或根仅在加载的 swf 被添加到添加到阶段的显示列表时才可用。当 Event.INIT 触发时,加载的 swf 没有阶段或根,因此 swf 的第一帧不应该有任何对阶段或根的引用。

如果您没有对 swf 使用文档类,则只需使用 stop() 使第一帧为空,然后在加载并添加到显示列表后,让 swf 转到第 2 帧,其中有您的所有代码。

BotMaster
2015-02-20