Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

why is my swf loading the wrong swf file even though the url is correct?

Explorer ,
Dec 28, 2012 Dec 28, 2012

I am creating a game that consist of multiple external SWF files that I connected using the loader class. I have 3 levels. At first the connection of the swf is woking well. But after finishing level 2, when the congratulatory screen appears and then pressed the button so that I can proceed to the next level, it goes back to level 2 instead of proceeding to Level 3. >.< When I check the URL, it is correct.

TOPICS
ActionScript
2.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 30, 2012 Dec 30, 2012

assuming the top snippet is stage2.swf and it loads a congratuloary.swf that uses that 2nd snippet, your code shows stage2.swf loading congratulatory.swf and congratulotory.swf loading stage2.swf and that's a problem.  fix that.

Translate
Community Expert ,
Dec 28, 2012 Dec 28, 2012

copy and paste the relevant code.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 28, 2012 Dec 28, 2012

Here is the code.

On the Congratulatory Screen where there is a button connected to Stage3:

next_stage.addEventListener(MouseEvent.CLICK, loadStage_2);

var music_1:Sound = new Sound(new URLRequest("Congrats_Theme.mp3"));

music_1.play();

function loadStage_2(event:MouseEvent):void

           {

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

     var myloader:Loader=new Loader();

     myloader.addEventListener("UnloadMe", unloadFunction);

     myloader.load(myrequest);

     stage.addChild(myloader);

           removeChildAt(0);

           removeChild(next_stage);

           SoundMixer.stopAll();

     }

          

function unloadFunction(event:Event):void {

Loader(event.currentTarget).unload();

}

Level 2 code:

if (fl_SecondsElapsed == 0 && score >= 70)

                                                            {

                                                                      stopGame();

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

                                                                      var myloader:Loader = new Loader();

                                                                      myloader.addEventListener("UnloadMe", unloadFunction);

                                                                      myloader.load(myrequest);

                                                                      SoundMixer.stopAll();

                                                                      stage.addChild(myloader);

                                                                      removeChild(Score_board);

                                                                      removeChild(Timer_board);

                                                                      removeChild(ScoreDisplay);

                                                                      removeChild(TimerDisplay);

                                                            }

 

                                                            else if (fl_SecondsElapsed == 0 && score < 70)

                                                            {

                                                                      stopGame();

                                                                      var myrequest1:URLRequest = new URLRequest("Game_Over_2.swf");

                                                                      var myloader1:Loader = new Loader();

                                                                      myloader1.addEventListener("UnloadMe", unloadFunction_1);

                                                                      myloader1.load(myrequest1);

                                                                      SoundMixer.stopAll();

                                                                      stage.addChild(myloader1);

                                                                      removeChild(Score_board);

                                                                      removeChild(Timer_board);

                                                                      removeChild(ScoreDisplay);

                                                                      removeChild(TimerDisplay);

                                                            }

                                                            }

                                                            public function unloadFunction(event:Event) {

                                                               Loader(event.currentTarget).unloadAndStop();

                                                            }

 

                                                            public function unloadFunction_1(event:Event) {

                                                               Loader(event.currentTarget).unloadAndStop();

                                                            }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 28, 2012 Dec 28, 2012

One more thing, when I play the swf file of Congratulatory Screen, it works well. It proceeds to Level 3 when I pressed the button. But when I start on Level 1, the Congratulatory Screen doesn't proceed to Level 3 but instead goes back to Level 2.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 28, 2012 Dec 28, 2012

is that first code snippet from Congratulatory_Screen_2.swf?  is level 3, Stage3.swf?

and, the first errors i see are related to an undefined event "Unload Me":


myloader.addEventListener("UnloadMe", unloadFunction);

you should fix that.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 28, 2012 Dec 28, 2012

Yes. The frst is from Congratulatory_Screen_2 and Level 3 is stage3.swf.

Regarding the UnloadMe, it is working well for me. >.< Here is my full code for Level2:

package

{

                    import flash.automation.StageCaptureEvent;

        import flash.display.*;

        import flash.events.*;

                    import flash.text.*;

        import flash.utils.Timer;

                    import flash.display.Stage;

        import flash.utils.getDefinitionByName;

                    import flash.net.URLRequest;

                    import flash.media.Sound;

                    import flash.media.SoundMixer;

        public class  Stage2 extends MovieClip       {

                                        var catcher:Catcher;

                                        var nextObject:Timer;

                var objects:Array = new Array();

                                        var ObjectHalfWidth: Number;

                const speed: Number = 6.0;

                                        var score: int = 0;

                                        var fl_TimerInstance:Timer = new Timer(1000, 60);

                                        var fl_SecondsElapsed:Number = 60;

                                        var music_1:Sound = new Sound(new URLRequest("Cheerful_Theme.mp3"));

                                        var soundfx_1:Sound = new Sound(new URLRequest("Catch_Good.mp3"));

                                        var soundfx_2:Sound = new Sound(new URLRequest("Catch_Bad.mp3"));

                                        var soundfx_3:Sound = new Sound(new URLRequest("Ticking_Clock.mp3"));

                public function Stage2() {

                                                            catcher = new Catcher ();

                                                            catcher.y = 768;

                                                            catcher.x = 120;

                                                            addChild(catcher);

                                                            catcher.mouth_mc.gotoAndStop(1);

                                                            setNextObject();

                        addEventListener(Event.ENTER_FRAME, moveObjects);

                                                            fl_TimerInstance.addEventListener(TimerEvent.TIMER, fl_TimerHandler);

                                                            fl_TimerInstance.start();

                                                            music_1.play();

                }

                public function setNextObject() {

                        nextObject = new Timer(1000 + Math.random() * 1500, 1);

                        nextObject.addEventListener(TimerEvent.TIMER_COMPLETE, newObject);

                        nextObject.start(); 

                                                                          if (fl_SecondsElapsed == 0)

                                                                                          {

                                                                                          nextObject.stop();

                                                                                          }

                                        }

                public function newObject(e:TimerEvent) {

                        var solidObjects:Array = ["Chocolate","Apple", "Laptop", "USB"];

                        var liquidObjects:Array = ["Coffee", "CookingOil", "Milk", "Water"];

                        var badObjects:Array = ["Fire"];

                        var randomNum:Number = Math.random();

                        if ((randomNum >= .1) && (randomNum <= .4)) {

                                var r:int = Math.floor(Math.random() * solidObjects.length);

                                var classRef:Class = getDefinitionByName(solidObjects) as Class;                   

                                var newObject:MovieClip = new classRef();

                                newObject.typestr = "solid";

                                }

                        else if ((randomNum >= .5) && (randomNum <= 1)) {

                                        r = Math.floor(Math.random() * liquidObjects.length);

                                        classRef = getDefinitionByName(liquidObjects) as Class;

                                        newObject = new classRef ();

                                        newObject.typestr = "liquid";

                                }

                        else    {

                                        r = Math.floor(Math.random() * badObjects.length);

                                        classRef = getDefinitionByName(badObjects) as Class;

                                        newObject = new classRef ();

                                        newObject.typestr = "bad";

                                                                                }

                                newObject.y = -300;

                                                                                newObject.x = 1024;

                                                                                newObject.x = (Math.random() * 400) + (Math.random()*400);

                                                                                addChild(newObject);

                                                                                objects.push(newObject);

                                                                                setNextObject();

                                                                                }

                        public function moveObjects(e:Event) {

                                                                      for (var i: int = objects.length -1; i >= 0; i--) {

                                objects.y += speed;

                                                                                addChild(objects);

                                if (objects.y > 750) {

                                removeChild(objects);

                                                                                objects.splice(i, 1);

                                }

                                                                                if (objects.hitTestObject(catcher.mouth_mc)) {

                                                                                          if (objects.typestr == "liquid") {

                                                                                                    score += 5;

                                                                                                    soundfx_1.play();

                                                                                                    catcher.mouth_mc.gotoAndPlay(9);

                                                                                          }

                                                                                          else

                                                                                          {

                                                                                                    score -= 5;

                                                                                                    soundfx_2.play();

                                                                                                    catcher.mouth_mc.gotoAndPlay(2);

                                                                                          }

                                                                                          if (score < 0) score = 0;

                                                                                          ScoreDisplay.text = String(score);

                                                                                          removeChild(objects);

                                                                                          objects.splice(i, 1);

                                                                                          }

                                                                                if (fl_SecondsElapsed == 0)

                                                                                          {

                                                                                                     removeChild(objects);

                                                                                                     objects.splice(i, 1);

                                                                                          }

                                                                                catcher.x = mouseX;

                                                                                checkStageBorder();

                                                                      }

                                                            }

                                                            public function fl_TimerHandler(Event:TimerEvent):void

                                                            {

                                                            fl_SecondsElapsed--;

                                                            TimerDisplay.text = String(fl_SecondsElapsed);

                                                            if(fl_SecondsElapsed == 10)

                                                            {

                                                            soundfx_3.play();

                                                            }

                                                            if (fl_SecondsElapsed == 0 && score >= 70)

                                                            {

                                                                      stopGame();

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

                                                                      var myloader:Loader = new Loader();

                                                                      myloader.addEventListener("UnloadMe", unloadFunction);

                                                                      myloader.load(myrequest);

                                                                      SoundMixer.stopAll();

                                                                      stage.addChild(myloader);

                                                                      removeChild(Score_board);

                                                                      removeChild(Timer_board);

                                                                      removeChild(ScoreDisplay);

                                                                      removeChild(TimerDisplay);

                                                            }

                                                            else if (fl_SecondsElapsed == 0 && score < 70)

                                                            {

                                                                      stopGame();

                                                                      var myrequest1:URLRequest = new URLRequest("Game_Over_2.swf");

                                                                      var myloader1:Loader = new Loader();

                                                                      myloader1.addEventListener("UnloadMe", unloadFunction_1);

                                                                      myloader1.load(myrequest1);

                                                                      SoundMixer.stopAll();

                                                                      stage.addChild(myloader1);

                                                                      removeChild(Score_board);

                                                                      removeChild(Timer_board);

                                                                      removeChild(ScoreDisplay);

                                                                      removeChild(TimerDisplay);

                                                            }

                                                            }

                                                            public function unloadFunction(event:Event) {

                                                               Loader(event.currentTarget).unloadAndStop();

                                                            }

                                                            public function unloadFunction_1(event:Event) {

                                                               Loader(event.currentTarget).unloadAndStop();

                                                            }

                                                            public function checkStageBorder()

                                                            {

                                                                      ObjectHalfWidth = catcher.width / 2;

                                                                      if (catcher.x - ObjectHalfWidth  < 0)

                                                                      {

                                                                                catcher.x = 18 + ObjectHalfWidth;

                                                                      }

                                                                      else if (catcher.x + ObjectHalfWidth > stage.stageWidth)

                                                                      {

                                                                                catcher.x = stage.stageWidth - ObjectHalfWidth + 17;

                                                                      }

                                                            }

                                                            public function stopGame()

                                                                      {

                                                                                for (var i:int = 0; i < objects.length; i++)

                                                                      {

                                                                                this.removeChild(objects);

                                                                                objects.splice(i, 1);

                                                                                nextObject.stop();

                                                                      }

                                                                      this.removeChild(catcher);

                                                                      }

          }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 28, 2012 Dec 28, 2012

i think it is doing nothing except making you think it is doing something.

to confirm it's doing nothing, put a trace() in your unloadme listener function and see if it's ever traced.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 28, 2012 Dec 28, 2012

you're right it's not tracing anything. is my way of loading and unloading swf files correct? if not, how can i correct them?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 28, 2012 Dec 28, 2012

is my way of loading and unloading swf files correct? *

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 28, 2012 Dec 28, 2012

1.  remove all that unload code.

2.  it looks like you only need to load, at most, one swf at any given time.  if that's true, you should only use one loader.  that way you don't have to unload anything, just reuse the same loader repeatedly.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 28, 2012 Dec 28, 2012

i removed the unload code, and only use one loader but still it keeps going back to stage 2. maybe i did the one loader thing wrong. how do you manage to do that?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 29, 2012 Dec 29, 2012

copy and paste your updated code.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 29, 2012 Dec 29, 2012

//stage2

package

{

                    import flash.automation.StageCaptureEvent;

        import flash.display.*;

        import flash.events.*;

                    import flash.text.*;

        import flash.utils.Timer;

                    import flash.display.Stage;

        import flash.utils.getDefinitionByName;

                    import flash.net.URLRequest;

                    import flash.media.Sound;

                    import flash.media.SoundMixer;

      

        public class  Stage2 extends MovieClip       {

                                        var catcher:Catcher;

                                        var nextObject:Timer;

                var objects:Array = new Array();

                                        var ObjectHalfWidth: Number;

                const speed: Number = 8.0;

                                        var score: int = 0;

                                        var fl_TimerInstance:Timer = new Timer(1000, 60);

                                        var fl_SecondsElapsed:Number = 60;

                                        var music_1:Sound = new Sound(new URLRequest("Cheerful_Theme.mp3"));

                                        var soundfx_1:Sound = new Sound(new URLRequest("Catch_Good.mp3"));

                                        var soundfx_2:Sound = new Sound(new URLRequest("Catch_Bad.mp3"));

                                        var soundfx_3:Sound = new Sound(new URLRequest("Ticking_Clock.mp3"));

                                        var myloader:Loader = new Loader();

                                        var myloader1:Loader = new Loader();

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

 

                public function Stage2 () {

                                                            catcher = new Catcher ();

                                                            catcher.y = 768;

                                                            catcher.x = 120;

                                                            addChild(catcher);

                                                            catcher.mouth_mc.gotoAndStop(1);

                                                            setNextObject();

                        addEventListener(Event.ENTER_FRAME, moveObjects);

                                                            fl_TimerInstance.addEventListener(TimerEvent.TIMER, fl_TimerHandler);

                                                            fl_TimerInstance.start();

                                                            music_1.play();

                }

 

                public function setNextObject() {

                        nextObject = new Timer(1000 + Math.random() * 1500, 1);

                        nextObject.addEventListener(TimerEvent.TIMER_COMPLETE, newObject);

                        nextObject.start(); 

 

                                                                            if (fl_SecondsElapsed == 0)

                                                                                          {

                                                                                                    nextObject.stop();

                                                                                          }

                                        }

              

                public function newObject(e:TimerEvent) {

                        var solidObjects:Array = ["Chocolate","Apple", "Laptop", "USB"];

                        var liquidObjects:Array = ["Coffee", "CookingOil", "Milk", "Water"];

                        var badObjects:Array = ["Fire"];

                        var randomNum:Number = Math.random();

 

                        if ((randomNum >= .1) && (randomNum <= .4)) {

                                var r:int = Math.floor(Math.random() * solidObjects.length);

                                var classRef:Class = getDefinitionByName(solidObjects) as Class;                   

                                var newObject:MovieClip = new classRef();

                                newObject.typestr = "solid";

                                }

                              

                        else if ((randomNum >= .5) && (randomNum <= 1)) {

                                        r = Math.floor(Math.random() * liquidObjects.length);

                                        classRef = getDefinitionByName(liquidObjects) as Class;

                                        newObject = new classRef ();

                                        newObject.typestr = "liquid";

                                }

                              

                        else    {

                                        r = Math.floor(Math.random() * badObjects.length);

                                        classRef = getDefinitionByName(badObjects) as Class;

                                        newObject = new classRef ();

                                        newObject.typestr = "bad";

                                                                                }

 

                                newObject.y = -300;

                                                                                newObject.x = 1024;

                                                                                newObject.x = (Math.random() * 400) + (Math.random()*400);

                                                                                addChild(newObject);

                                                                                objects.push(newObject);

                                                                                setNextObject();

 

                                                                                }

 

                        public function moveObjects(e:Event) {

                                                                      for (var i: int = objects.length -1; i >= 0; i--) {

                                objects.y += speed;

                                                                                addChild(objects);

 

                                if (objects.y > 750) {

                                removeChild(objects);

                                                                                objects.splice(i, 1);

                                }

 

                                                                                if (objects.hitTestObject(catcher.mouth_mc)) {

                                                                                          if (objects.typestr == "liquid") {

                                                                                                    score += 5;

                                                                                                    soundfx_1.play();

                                                                                                    catcher.mouth_mc.gotoAndPlay(9);

                                                                                          }

                                                                                          else

                                                                                          {

                                                                                                    score -= 5;

                                                                                                    soundfx_2.play();

                                                                                                    catcher.mouth_mc.gotoAndPlay(2);

                                                                                          }

 

                                                                                          if (score < 0) score = 0;

 

                                                                                          ScoreDisplay.text = String(score);

                                                                                          removeChild(objects);

                                                                                          objects.splice(i, 1);

                                                                                          }

 

                                                                                if (fl_SecondsElapsed == 0)

                                                                                          {

                                                                                                     removeChild(objects);

                                                                                                     objects.splice(i, 1);

                                                                                          }

 

                                                                                catcher.x = mouseX;

                                                                                checkStageBorder();

                                                                      }

                                                            }

 

                                                            public function fl_TimerHandler(Event:TimerEvent):void

                                                            {

                                                            fl_SecondsElapsed--;

                                                            TimerDisplay.text = String(fl_SecondsElapsed);

 

                                                            if(fl_SecondsElapsed == 10)

                                                            {

                                                            soundfx_3.play();

                                                            }

 

                                                            if (fl_SecondsElapsed == 0 && score >= 80)

                                                            {

                                                                      stopGame();

                                                                      myloader.load(myrequest);

                                                                      SoundMixer.stopAll();

                                                                      stage.addChild(myloader);

                                                                      removeChild(Score_board);

                                                                      removeChild(Timer_board);

                                                                      removeChild(ScoreDisplay);

                                                                      removeChild(TimerDisplay);

                                                            }

 

                                                            else if (fl_SecondsElapsed == 0 && score < 60)

                                                            {

                                                                      stopGame();

                                                                      var myrequest1:URLRequest = new URLRequest("Game_Over_2.swf");

                                                                      myloader1.load(myrequest1);

                                                                      SoundMixer.stopAll();

                                                                      stage.addChild(myloader1);

                                                                      removeChild(Score_board);

                                                                      removeChild(Timer_board);

                                                                      removeChild(ScoreDisplay);

                                                                      removeChild(TimerDisplay);

                                                            }

                                                            }

 

                                                            public function checkStageBorder()

                                                            {

                                                                      ObjectHalfWidth = catcher.width / 2;

                                                                      if (catcher.x - ObjectHalfWidth  < 0)

                                                                      {

                                                                                catcher.x = 18 + ObjectHalfWidth;

                                                                      }

                                                                      else if (catcher.x + ObjectHalfWidth > stage.stageWidth)

                                                                      {

                                                                                catcher.x = stage.stageWidth - ObjectHalfWidth + 17;

                                                                      }

                                                            }

 

                                                            public function stopGame()

                                                                      {

                                                                                for (var i:int = 0; i < objects.length; i++)

                                                                      {

                                                                                this.removeChild(objects);

                                                                                objects.splice(i, 1);

                                                                                nextObject.stop();

                                                                      }

                                                                      this.removeChild(catcher);

                                                                      }

 

          }

}

//Congratulatory_Screen_1

next_stage.addEventListener(MouseEvent.CLICK, loadStage_2);

var music_1:Sound = new Sound(new URLRequest("Congrats_Theme.mp3"));

music_1.play();

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

var myloader:Loader=new Loader();

function loadStage_2(event:MouseEvent):void

           {

     myloader.load(myrequest);

     stage.addChild(myloader);

           removeChildAt(0);

           removeChild(next_stage);

           SoundMixer.stopAll();

           }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 30, 2012 Dec 30, 2012

i have deleted the unload code and used only one loader but still the stage2.swf loads the congratulatory_screen_1.swf even though the url i used is supposed to load the congratulatory_screen_2.swf. this happens when i load it from the first level but when i load it from stage2.swf it correctly loads the next swf. why is that?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 30, 2012 Dec 30, 2012

assuming the top snippet is stage2.swf and it loads a congratuloary.swf that uses that 2nd snippet, your code shows stage2.swf loading congratulatory.swf and congratulotory.swf loading stage2.swf and that's a problem.  fix that.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 30, 2012 Dec 30, 2012
LATEST

I've finallly figured out what the problem is. It was the button that I copied and paste from a different swf. Thank you for all the help.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines