• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

AS3 Preloader Errors

Hi There

I have implemented Lee Brimlows AS3 preloader (http://www.gotoandlearn.com/play?id=85) successfully in its basic form but as soon as I try to expand on the functionality of the preloader I get output errors.

I have added a graphic/ MC to the stage call preLogo which contains a few graphical elements and a counter that I animate, the animation + preloading works fine but it seems to be the issue with the adding bits to the stage, where is goes wrong. I keep getting the following output error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
      at AntMain/initStage()
      at AntMain/initSite()
      at AntMain/pressImageLoadComplete()
      at flash.events::EventDispatcher/dispatchEventFunction()
      at flash.events::EventDispatcher/dispatchEvent()
      at br.com.stimuli.loading::BulkLoader/_onAllLoaded()
      at br.com.stimuli.loading::BulkLoader/_onItemComplete()
      at flash.events::EventDispatcher/dispatchEventFunction()
      at flash.events::EventDispatcher/dispatchEvent()
      at br.com.stimuli.loading.loadingtypes::LoadingItem/onCompleteHandler()
      at br.com.stimuli.loading.loadingtypes::ImageItem/onCompleteHandler()

This error is within a function in the swf I am preloading, my main MC.

Please find attached my code, I have tried it with Tweener and TweenLite and get similar errors, I have attached the TweenLite Version of the AS3 I have written. As soon as I take the tweening code out of the preInitSite() function it works, but obviously the required animations do not.

Can anyone spot what im doing wrong??

Many thanks

S
// Import Classes
import com.greensock.*;
import com.greensock.easing.*;

// Vars
var origLogoHeight:Number = preLogo.height;
var origLogoWidth:Number = preLogo.width;
var l:Loader;

function preInit():void {
	
	stage.frameRate = 30;
	stage.showDefaultContextMenu = false;
	stage.scaleMode = StageScaleMode.NO_SCALE;
	stage.align = StageAlign.TOP_LEFT;
	stage.addEventListener(Event.RESIZE, preResizeHandler);
	
	preLogo.strap.alpha = 0;
	preLogo.strap.mask = preLogo.strapMask;
	
	preLogo.letters.alpha = 0;
	//preLogo.letters.visible = false;	
	
	preLogo.brackets.left.alpha = 0;
	preLogo.brackets.right.alpha = 0;
	
	preLogo.brackets.left.x = (preLogo.brackets.width / 4) - preLogo.brackets.left.width ;
	preLogo.brackets.right.x = (preLogo.brackets.width / 4);

	preLogo.percent.alpha = 0;
	
	preLogo.width = 100;
	preLogo.scaleY = preLogo.scaleX;
	
	preLogo.x = (stage.stageWidth / 2) - (preLogo.width / 2);
	preLogo.y = (stage.stageHeight / 2) - (preLogo.height / 2);
	
	var bracketTimeline:TimelineLite = new TimelineLite({onComplete:preInitLoad});
	bracketTimeline.insertMultiple([
		new TweenLite(preLogo.brackets.left, 0.5, {delay: 2, alpha: 1, x: 0, ease:Circ.easeOut}),
		new TweenLite(preLogo.brackets.right, 0.5, {delay: 2, alpha: 1, x: ((preLogo.brackets.width/2) - preLogo.brackets.right.width), ease:Circ.easeOut }),
		new TweenLite(preLogo.percent, 1.5, {delay: 2.5, alpha: 1, ease:Circ.easeOut})
	]);

}

function preInitLoad():void {
	l = new Loader();
	l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, preLoadProgress);
	l.contentLoaderInfo.addEventListener(Event.COMPLETE, preLoadComplete);
	l.load(new URLRequest("AntMain.swf"));
}


function preResizeHandler(e:Event):void
{
	positionLoader();
}

function positionLoader():void {
	preLogo.x = (stage.stageWidth / 2) - (preLogo.width / 2);
	preLogo.y = (stage.stageHeight / 2) - (preLogo.width / 2);
}

function preLoadProgress(e:ProgressEvent):void
{
	var perc:Number = e.bytesLoaded / e.bytesTotal;
	preLogo.percent.text = Math.ceil(perc*100).toString();
}

function preLoadComplete(e:Event):void
{
	var preLogoX:Number = (stage.stageWidth / 2) - (origLogoWidth / 2);
	var preLogoY:Number = (stage.stageHeight / 2) - (origLogoHeight / 2);
	

	var preLogoTimeline:TimelineLite = new TimelineLite({onComplete:preInitSite});
	preLogoTimeline.append(new TweenLite(preLogo.percent, 0.2, {alpha: 0}));
	preLogoTimeline.append(new TweenLite(preLogo, 1.5, {x:preLogoX, y:preLogoY, width:origLogoWidth, height:origLogoHeight, ease:Back.easeInOut}));
	preLogoTimeline.append(new TweenLite(preLogo.letters, 0.7, {alpha:1, onComplete: function() {
		preLogo.strapMask.x = preLogo.strapMask.x - preLogo.strapMask.width - 5;
		preLogo.strap.alpha = 1;
	}}));	
	preLogoTimeline.append(TweenLite.to(preLogo.strapMask, 1.5, {x:-1}));
	preLogoTimeline.append(TweenLite.to(preLogo, 2, {alpha:1, ease:Back.easeInOut}));
}

function preInitSite():void {
	removeChildAt(0);
	preLogo = null;
	addChild(l);
}

// Inititate Preloader
preInit();

Open in new window

0
socross
Asked:
socross
  • 7
  • 7
  • 2
1 Solution
 
blue-genieCommented:
Hello
you're referencing something that doesn't exist so you need to use breakpoints to check where it's failing.
first obvious question is where is preLogo. on the stage?

might be easier if you upload your file and the required imports.
0
 
socrossAuthor Commented:
Thanks blue-genie,

Its really strange, the movie that is loading in has no issues and run fine if accessed directly and also if I use the basic preloader that I mentioned earlier.

I really feel the issue is with the preLoadComplete function, if i replace the code in this function with a call to preInitSite(); the whole thing works fine! It must be something to do with clearing the stage and adding the new swf.

I have attached the fla for the loader, yes preLoader is on the stage.

Thanks

S
AntLoad.fla
0
 
blue-genieCommented:
hey, am i missing something, the problem doesn't appear to be in your preloader as i'm able to run it and load a file file
must be somethign in your antmain.swf.

upload the swf, lets' start with that and check if that's the problem.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
socrossAuthor Commented:
Blue Genie

That is really odd, when I run the file im trying to preload directly, as in open it publish it it runs fine, it also runs fun when I add it using a basic version of the preloader.

Im reluctant to upload the full site just yet.

Coould you take a look at this version of the preloader which loads exactly the same file but works absolutly fine!

Could it be something to do with the imports?

Thanks


AntLoadOld.fla
0
 
blue-genieCommented:
whoa wait, i'm not following.
the first file you uploaded that you say throws a null error is not throwing a null error.

this new file AntLoadOld is the file that you dont' want to use, so what exactly do you want me to assist with.
what's the ultimate thing you want to achieve?
0
 
socrossAuthor Commented:
Blue Genie,

I am sorry for the confusion caused, very new to AS3 so I will try and explain a little more clearly.

1.
I have the main project file which is coded using the document class, this file runs fine with no errors.

2.
I then created a preloder which loads this main movie onto the stage, this is the second file I uploaded (AntLoadOld.fla) and it loads the movie fine with no errors.

3.
AntLoad.fla is the file i am having trouble with, I need it to preload the movie and then add it to the stage.

The error gets thrown when i try and use the new preloader, it is just a slightly expanded version of the orignal preloader that i wrote.

I am, maybe incorrectly, assuming that the issue is with the new preloader and not with the main movie that i am loading.

I am a little confused as to why the main movie would cause any issues.

What do you need to see to debug this?

Is my issue any clearer?

Thanks

S


0
 
blue-genieCommented:
okay i understand point 1 and 2.

point 3. that's what i'm saying. that's the file you first uploaded. when i run it I AM NOT getting any errors, and i'm able to successfully load a file,  hence I'm thinking there's something wrong somewhere else. Looking at AntLoadOld is not going to help right because you're not using that file
in order to determine where the problem is, need your swf file.
if i then get the error then we know the error is in your main.swf and not preloader swf.

and then we can go from there.
0
 
socrossAuthor Commented:
Ok cool , that makes sence. Here is my swf file

Thanks
AntMain.swf
0
 
blue-genieCommented:
ok what does AntMain.swf on its own do?
is there actually something visual there?
I can still load it without the null error you're getting. I'm getting IOErrors for missing xml files which i'm assuming is right.
0
 
socrossAuthor Commented:
ANTMain loads some images, some xml files, and then adds everything to the stage, it is the whole website.

I cant figure out what is going on.

Is there anything else that might help get to the bottom of it?

Thanks

S
0
 
blue-genieCommented:
the source for you AntMain file
0
 
IqAndreasCommented:
This is a VERY common error! I get it at least several times per week on the kirupa forum.

Check out the solution here:
http://www.kirupa.com/forum/showpost.php?p=2549532&postcount=2

Also http://iqandreas.blogspot.com/2009/09/most-common-flash-questions-as3-faq.html
0
 
socrossAuthor Commented:
Thanks Guys,

I did infact fix this last night, it was the issue described referenced IqAndreas. The loaded swf trying to access the stage. I fixed it by adding the ADDED_TO_STAGE event listener to the main document class, see attached code.

Thanks

S
		public function AntMain():void
		{
			this.addEventListener(Event.ADDED_TO_STAGE, mainInit);
		}
		
		public function mainInit(e:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, mainInit);
			initLoad();
			
			initStage();
			initBackground();
			initCurtains();
			initHeader();
			initFooter();
			
			
		}

Open in new window

0
 
IqAndreasCommented:
Just one quick tip, your code will not work if the stage already exists, such as if you decide to run the SWF without preloading it. You can check if the stage already exists using the attached code.


Just curious, where did you get ahold of the solution? ;)


Also, is there a difference between closing a question and setting the answer to the last one you posted? I'm still a little shaky on how it all works...
public function AntMain():void
{
   if (this.stage)
      { mainInit(null); }
   else
      { this.addEventListener(Event.ADDED_TO_STAGE, mainInit); }
}

public function mainInit(e:Event):void {
   //Removing an event listener that you never added does
   //not cause any errors, so always remove it.
   removeEventListener(Event.ADDED_TO_STAGE, mainInit);
   
   initLoad();
   
   initStage();
   initBackground();
   initCurtains();
   initHeader();
   initFooter();
   
}

Open in new window

0
 
socrossAuthor Commented:
Hi

Great tip thanks, I struggled with this for nearly two days, but it was good in the end which led me to the solution:

http://www.flashandmath.com/intermediate/swfload/docclass.html
http://www.flepstudio.org/forum/tutorials/775-event-added_to_stage.html

Im hoping the question will stay avaiable with the solution avaiable to others??

Should I be giving out points to you guys for your input? Sorry if i should I.

Thanks

S
0
 
blue-genieCommented:
that is so odd because i did not get that null error.

@ig
"
Also, is there a difference between closing a question and setting the answer to the last one you posted? I'm still a little shaky on how it all works..."

if there are no comments, the asker can simply delete the question, however if there are, there are a few options.
1. if you got a solution from a volunteer then you can assign them the points and close it that way.
2. if you found the solution yourself you can post your solution and accept that as the answer, that way closing the question and the asker getting his points back.
3. if the asker found his own solution but did get some assistance they can choose to split the points.
4. if the asker point blank neglects the question, the clean up crew will force accept, close with no refund , or close and refund.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now