A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Problems adding/removing external SWF

  1. #1
    Junior Member
    Join Date
    Apr 2008
    Posts
    11

    Problems adding/removing external SWF

    On my main timeline, I have a list of buttons that each load an external SWF when pressed. When a button is pressed, the SWF loads into a movieclip on the main timeline, and the list dissappears (so only one is loaded at a time). The main timeline moves to a frame with a back button. When the back button is pressed, I'm trying to remove the loaded SWF and make the menu visible again to load another SWF.

    This all works, but when I get back to the menu and try to load a new SWF, I get the error:

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

    CrystalBall being the first SWF I tried to load. The error fires like, four or five times too. I know it has something to do with the code trying to remove the loader before it loads it again, but I'm not sure how to fix it

    Code:
    package {
    	import flash.display.*;
    	import flash.events.*;
    	import flash.text.*;
    	import flash.net.*;
    	
    	public class Splash extends MovieClip {
    		var gameLoader:Loader = new Loader();
    		
    		function Splash() {
    			// ensures that games load from top left corner
    			gameHolder.x = 0; //gameHolder is already on timeline
    			gameHolder.y = 0;			
    		}
    		
    		function loaded() { //called after preloader			
    			// game buttons
    			game1.addEventListener(MouseEvent.CLICK, loadGame);
    			game2.addEventListener(MouseEvent.CLICK, loadGame);
    			game3.addEventListener(MouseEvent.CLICK, loadGame);
    
    			function loadGame(evt:MouseEvent) {
    				gameHolder.addChild(gameLoader);
    				var gamePath = evt.target.name+".swf";
    				var game:URLRequest = new URLRequest(gamePath);
    				gameLoader.load(game);
    				gameLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onGameLoad);
    				function onGameLoad() {
    					gameLoaded = true;
    					gameLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onGameLoad)
    				}
    				MovieClip(root).gotoAndStop("play now"); // reveals back button
    			}
    			
    			backBtn.addEventListener(MouseEvent.CLICK, backToMenu);
    			function backToMenu(evt:MouseEvent):void {
    				gameLoader.unload();
    				gameHolder.removeChild(gameLoader);
    				gameLoaded = false;
    				backBtn.visible = false;
                                    MovieClip(root).gotoAndStop("start"); // shows menu
    			}
    		}
    	}
    }
    I've searched the net and found similar problems, but no solutions that seemed to work for me.
    Last edited by metaphist; 05-20-2008 at 02:51 AM.

  2. #2
    Junior Member
    Join Date
    Apr 2008
    Posts
    11
    Well, aside from me not removing a few listeners, I've determined that it's a problem with my loaded movie and not the main timeline. I made a test movie that worked prefectly, but as soon as I tried loading in my CrystalBall game it throws the same error. I'll have to check that AS and see what I can find.

    edit: forgot I had this preloader on the swf I was embedding. Looks like there is a problem accessing loaderInfo after the game has been unloaded once already from the main timeline. I know the loader info is shared between the child and parent, so I'm guessing when the game unloads and gets reloaded, it can't find the loaderInfo?

    Code:
    //Create a listener to call the loading function as the movie loads
    this.loaderInfo.addEventListener (ProgressEvent.PROGRESS, PL_LOADING);
    
    /*This is the main function, basically it grabs the total and loaded bytes,
    calculates a percentage, and displays it by stretching the bar and adjusting
    the textfield display. If your bar/textbox instancenames are NOT lbar/lpc,
    you'll need to adjust this code to match your instance names*/
    function PL_LOADING(event:ProgressEvent):void {
    	var pcent:Number = event.bytesLoaded/event.bytesTotal*100;
    	//Stretch the bar
    	lbar.scaleX = pcent/100;
    	//Display the % loaded in textfield
    	lpc.text = int(pcent)+"%";
    	//If the movie is fully loaded, kick to the next frame on the main timeline
    	//You may wish to change the gotoAndStop(2) to a gotoAndPlay(2)
    	if(pcent == 100){
    		this.loaderInfo.removeEventListener (ProgressEvent.PROGRESS, PL_LOADING);
    		play();
    	} else {
    		stop();
    	}
    }
    Don't know a solution off the top, but I might just try loading all the games in separate loader objects at instantiation, and just add/remove the loader objects with the button. Seems it would add a lot of overhead, but I'll try it.
    Last edited by metaphist; 05-20-2008 at 09:48 AM.

  3. #3
    Junior Member
    Join Date
    Dec 2007
    Posts
    13

    Help Regarding This Issue

    I don't know if anyone is still responding to this post from last year but this is exactly the kind of issue I'm running into at this point with a site I'm building.

    I was wondering if there was a resolution with that existing script (preloader in the first frame of the SWF being loaded) or if the solution is using individual loaders in the container SWF.

    Thanks - Austin

  4. #4
    Junior Member
    Join Date
    Apr 2008
    Posts
    11
    Well as you imagine, it's been a while since that project, but if memory server me I did end up loading each SWF into it's own loader, using the preloader for as many SWFs I needed, and then loaded/unloaded the SWFs into the container as needed using button events.

    As a matter of fact, you can see the results yourself.

    www.mcdtoday.com/arcade

    Let me know if you need any specific help, and I should be able to dig up the ActionScript. (it's 3.0 mind you)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center