Log in

View Full Version : Preload a Resizing Movie



punstc
09-16-2008, 05:31 PM
I'm trying to preload a movie by using a separate movie as the preloader and the with a loader load in my main movie. I'm doing this because the document class in cs3 can't be exported anywhere but the first frame in AS3 so with all the code and assets i have the file size is large and the preloader on frame one of my main movie wouldnt show up until like 98%. which kinda defeats the purpose. Lee brimelow just did a tutorial on this technic so i thought i'd give it a try. The movie I'm trying to preload is using percents to take up all of the screen and has a resizing function so the positions update. I took the preloader off my main movie so its just one frame. I'm getting errors from my preloader movie telling me it can't reference some of the classes i have set up in my main movie.

heres the errors:


TypeError: Error #1009: Cannot access a property or method of a null object reference.
at main2_fla::MainTimeline/frame1()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at main2_fla::newsScroller_12/frame1()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at main2_fla::shows_scroller_15/frame1()


Here is the simple code i'm using in my preloader movie to load in my main movie:


stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;

var l:Loader = new Loader();
var b:LoadBar = new LoadBar();

b.height = 5;
b.y = stage.stageHeight/2 - b.height/2;
addChild(b);

l.load(new URLRequest("main2.swf"));
l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loaderProgress);
l.contentLoaderInfo.addEventListener(Event.COMPLETE, done);

function loaderProgress(e:ProgressEvent):void {
var perc = e.bytesLoaded/e.bytesTotal;
b.width = perc * stage.stageWidth;
}

function done(e:Event):void {
removeChild(b);
addChild(l);

}

Is there anything special that i need to do to make it work with the way i have my movie setup. I dont get any errors from just the main movie.

I hope I'm being clear enough on the problem let me know if i'm not.
I'll post my main movie code in a reply below it says them one is to long.

punstc
09-16-2008, 05:32 PM
here's my main movie code. from the movie that is being loaded in by the preload movie.



import flash.net.URLRequest;
import flash.events.*;
import flash.display.*;
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.*;

stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;

stage.addEventListener(Event.RESIZE, resizing);
stage.dispatchEvent(new Event(Event.RESIZE));

var movieLoader:Loader = new Loader();
var movieDisplayed:Boolean = false;
var movieToLoad:URLRequest;
var currentMovie:String = "home";
var newstxtFade:Tween;
var showstxtFade:Tween;
var newsTitleFade:Tween;
var showsTitleFade:Tween;
var newsScrollerFade:Tween;
var showsScrollerFade:Tween;
var newsbgFade:Tween;
var showsbgFade:Tween;
var latestFade:Tween;
var tw:Tween;

addChild(movieLoader);
movieLoader.y = 95;
fogScroll.cacheAsBitmap = true;
bg_mc.cacheAsBitmap = true;

function fadeOut() {
newstxtFade = new Tween(news_txt, "alpha", None.easeNone, news_txt.alpha, 0, .5, true);
showstxtFade = new Tween(shows_txt, "alpha", None.easeNone, shows_txt.alpha, 0, .5, true);
newsTitleFade = new Tween(news_title, "alpha", None.easeNone, news_title.alpha, 0, .5, true);
showsTitleFade = new Tween(shows_title, "alpha", None.easeNone, shows_title.alpha, 0, .5, true);
newsScrollerFade = new Tween(news_scroller, "alpha", None.easeNone, news_scroller.alpha, 0, .5, true);
showsScrollerFade = new Tween(shows_scroller, "alpha", None.easeNone, shows_scroller.alpha, 0, .5, true);
newsbgFade = new Tween(news_bg, "alpha", None.easeNone, news_bg.alpha, 0, .5, true);
showsbgFade = new Tween(shows_bg, "alpha", None.easeNone, shows_bg.alpha, 0, .5, true);
//latestFade = new Tween(latest, "alpha", None.easeNone, latest.alpha, 0, .5, true);
//trace(latest_txt.alpha);
}
function fadeIn() {
newstxtFade = new Tween(news_txt, "alpha", None.easeNone, news_txt.alpha, 1, .5, true);
showstxtFade = new Tween(shows_txt, "alpha", None.easeNone, shows_txt.alpha, 1, .5, true);
newsTitleFade = new Tween(news_title, "alpha", None.easeNone, news_title.alpha, 1, .5, true);
showsTitleFade = new Tween(shows_title, "alpha", None.easeNone, shows_title.alpha, 1, .5, true);
newsScrollerFade = new Tween(news_scroller, "alpha", None.easeNone, news_scroller.alpha, 1, .5, true);
showsScrollerFade = new Tween(shows_scroller, "alpha", None.easeNone, shows_scroller.alpha, 1, .5, true);
newsbgFade = new Tween(news_bg, "alpha", None.easeNone, news_bg.alpha, 1, .5, true);
showsbgFade = new Tween(shows_bg, "alpha", None.easeNone, shows_bg.alpha, 1, .5, true);
//latestFade = new Tween(latest, "alpha", None.easeNone, latest.alpha, 1, .5, true);
//trace(latest_txt.alpha);
}

function resizing(e:Event) {
bg_mc.x = stage.stageWidth/2 - bg_mc.width/2;
bg_mc.y = 0;
title_mc.x = stage.stageWidth/2 - title_mc.width/2;
title_mc.y = 17;
home_btn.x = stage.stageWidth/2 - home_btn.width/2 - 420;
home_btn.y = 71;
bio_btn.x = stage.stageWidth/2 - home_btn.width/2 - 340;
bio_btn.y = 71;
pics_btn.x = stage.stageWidth/2 - home_btn.width/2 - 200;
pics_btn.y = 71;
video_btn.x = stage.stageWidth/2 - video_btn.width/2 + 235;
video_btn.y = 71;
contact_btn.x = stage.stageWidth/2 - contact_btn.width/2 + 315;
contact_btn.y = 71;
link_btn.x = stage.stageWidth/2 - link_btn.width/2 + 430;
link_btn.y = 71;
news_txt.x = 17;
news_txt.y = 200;
news_bg.x = 17;
news_bg.y = 200;
news_title.x = 17;
news_title.y = 184;
news_scroller.x = 225;
news_scroller.y = 185;
shows_txt.x = stage.stageWidth - shows_txt.width - 30;
shows_txt.y = 200;
shows_bg.x = stage.stageWidth - shows_txt.width - 30;
shows_bg.y = 200;
shows_title.x = stage.stageWidth - shows_txt.width - 30;
shows_title.y = 184;
shows_scroller.x = stage.stageWidth - shows_scroller.width - 10;
shows_scroller.y = 185;
latest_txt.x = stage.stageWidth/2 - latest_txt.width/2;
fogScroll.x = 0;
fogScroll.y = stage.height - fogScroll.height;
if (movieLoader !=null) {
movieLoader.x = stage.stageWidth/2 - movieLoader.width/2;
}
}

//button actions
home_btn.buttonMode = true;
home_btn.mouseChildren = false;
home_btn.button_txt.btn_txt.text = "home";
home_btn.addEventListener(MouseEvent.MOUSE_OVER, home_btn.over);
home_btn.addEventListener(MouseEvent.MOUSE_OUT, home_btn.out);
home_btn.addEventListener(MouseEvent.CLICK, goto);
bio_btn.buttonMode = true;
bio_btn.mouseChildren = false;
bio_btn.button_txt.btn_txt.text = "biography";
bio_btn.addEventListener(MouseEvent.MOUSE_OVER, bio_btn.over);
bio_btn.addEventListener(MouseEvent.MOUSE_OUT, bio_btn.out);
bio_btn.addEventListener(MouseEvent.CLICK, goto);
pics_btn.buttonMode = true;
pics_btn.mouseChildren = false;
pics_btn.button_txt.btn_txt.text = "photos";
pics_btn.addEventListener(MouseEvent.MOUSE_OVER, pics_btn.over);
pics_btn.addEventListener(MouseEvent.MOUSE_OUT, pics_btn.out);
pics_btn.addEventListener(MouseEvent.CLICK, goto);
video_btn.buttonMode = true;
video_btn.mouseChildren = false;
video_btn.button_txt.btn_txt.text = "video";
video_btn.addEventListener(MouseEvent.MOUSE_OVER, video_btn.over);
video_btn.addEventListener(MouseEvent.MOUSE_OUT, video_btn.out);
video_btn.addEventListener(MouseEvent.CLICK, goto);
contact_btn.buttonMode = true;
contact_btn.mouseChildren = false;
contact_btn.button_txt.btn_txt.text = "contact";
contact_btn.addEventListener(MouseEvent.MOUSE_OVER, contact_btn.over);
contact_btn.addEventListener(MouseEvent.MOUSE_OUT, contact_btn.out);
contact_btn.addEventListener(MouseEvent.CLICK, goto);
link_btn.buttonMode = true;
link_btn.mouseChildren = false;
link_btn.button_txt.btn_txt.text = "links";
link_btn.addEventListener(MouseEvent.MOUSE_OVER, link_btn.over);
link_btn.addEventListener(MouseEvent.MOUSE_OUT, link_btn.out);
link_btn.addEventListener(MouseEvent.CLICK, goto);

function goto(e:MouseEvent) {
if (e.currentTarget == bio_btn && currentMovie !="bio") {
fadeOut();
loadSWF(bioRequest);
currentMovie = "bio";
} else if (e.currentTarget == pics_btn && currentMovie != "photos") {
fadeOut();
//loadSWF(photoRequest);
currentMovie = "photos";
navigateToURL(new URLRequest("gallery.html"), "_self");
} else if (e.currentTarget == video_btn && currentMovie != "video") {
fadeOut();
loadSWF(videoRequest);
currentMovie = "video";
} else if (e.currentTarget == contact_btn && currentMovie != "contact") {
fadeOut();
loadSWF(contactRequest);
currentMovie = "contact";
} else if (e.currentTarget == link_btn && currentMovie != "link") {
fadeOut();
loadSWF(linkRequest);
currentMovie = "link";
} else if (e.currentTarget == home_btn && currentMovie != "home") {
fadeIn();
tw = new Tween(movieLoader, "alpha", Regular.easeOut, movieLoader.alpha, 0, .5, true);
tw.addEventListener(TweenEvent.MOTION_FINISH, unloadClip);
function unloadClip(e:TweenEvent = null) {
movieLoader.unload();
}
movieDisplayed = false;
currentMovie = "home";
}
}

var bioRequest:URLRequest = new URLRequest("bio.swf");
var photoRequest:URLRequest = new URLRequest("gallery.swf");
var videoRequest:URLRequest = new URLRequest("video.swf");
var contactRequest:URLRequest = new URLRequest("contact.swf");
var linkRequest:URLRequest = new URLRequest("links.swf");

function loadSWF(url:URLRequest) {
movieToLoad = url;
if (movieDisplayed == false) {
movieLoader.alpha = 0;
startLoad();
} else {
var tw:Tween = new Tween(movieLoader, "alpha", None.easeNone, movieLoader.alpha, 0, .5, true);
tw.addEventListener(TweenEvent.MOTION_FINISH, startLoad);
}
}

function startLoad(e:TweenEvent = null) {
movieLoader.load(movieToLoad);
movieLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, displayMovie);
}

function displayMovie(e:Event) {
movieLoader.x = stage.stageWidth/2 - movieLoader.width/2;
var tw2:Tween = new Tween(movieLoader, "alpha", Regular.easeOut, movieLoader.alpha, 1, .5, true);
movieDisplayed = true;
}
//text documents

var newsloader:URLLoader = new URLLoader();
newsloader.load(new URLRequest("news.txt"));
newsloader.addEventListener(Event.COMPLETE, newsComplete);
function newsComplete(event:Event):void {
news_txt.htmlText = event.target.data;
}

var showsloader:URLLoader = new URLLoader();
showsloader.load(new URLRequest("shows.txt"));
showsloader.addEventListener(Event.COMPLETE, showsComplete);
function showsComplete(event:Event):void {
shows_txt.htmlText = event.target.data;
}

var latestLoader:URLLoader = new URLLoader();
latestLoader.load(new URLRequest("latest.txt"));
latestLoader.addEventListener(Event.COMPLETE, latestComplete);
function latestComplete(event:Event):void {
latest_txt.htmlText = event.target.data;
}

Medyman
09-16-2008, 07:36 PM
Gotta love Adobe error messages. The error means that you lost your scope somewhere. You're referring to non-existent properties and/or that object has not been initiated yet.

The error doesn't offer any clues as to where this is happening. If you want to attach your .fla, I can take a closer look.

punstc
09-16-2008, 08:35 PM
I figured thats what was happening but i didnt understand why exactly.

i included the preload fla and my main2 fla as well as the text files that the main2 fla is loading in when it starts up.

let me know if you want the rest of the swfs that the main movie loads in seperately from the navigation

http://www.uploading.com/files/TQ7BHT3Z/Archive.zip.html

thanks for the help i appreciate it

Medyman
09-16-2008, 09:54 PM
Are you using a self-defined Document class? Or are you referring to one that's automatically created when you compile?

If you've written your own, post it here. I'll show you how to make the changes in one spot that'll get rid of the problems.

I just viewed Lee's new tutorial on preloading and a lot of what he says is misleading or at the very least incomplete. If you're using this preloading technique, you need to check that the Document class has loaded before you execute any actions. Bascially, in the preloding SWF, you're check to see if main_2.swf has loaded, not necessarily that it's been initiated. When the AS in main_2.swf fires, the stage has not yet been initiated. So, you're getting 1009 errors.

This happens because of the event listeners you're adding to the stage on Frame 1 of main timeline as well as on your scroll bars.

I'm guessing that you've not written your own document class based on how the SWF is set up. In which case, you would do this:

1. On your scrollbars
move the stage.AddEventListener inside the onClick function.


function onClick(e:MouseEvent)
{
knob_mc.startDrag(false, new Rectangle(track_mc.x+1,track_mc.y,0,track_mc.height - knob_mc.height));
stage.addEventListener(Event.ENTER_FRAME, update);
pressed = true;
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
}

2. On the main timeline:

Add the following to the top:

isLoaded();
function isLoaded():void {
addEventListener(Event.ADDED_TO_STAGE, init);
};

function init(e:Event):void {
trace ("Document Class Fully Loaded");
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.addEventListener(Event.RESIZE, resizing);
stage.dispatchEvent(new Event(Event.RESIZE));
};

That should do it.

punstc
09-16-2008, 10:14 PM
I only have the code on frame one. never wrote an external document class. I'm in class right now so i'll make the adjustments you said and let you know how it works. Thanks for the help and the explanation i really appreciate it

punstc
09-17-2008, 03:06 AM
Everything works perfect, your definitely a genious.

Could you take a little time an explain a little further exactly what the Event.ADDED_TO_STAGE is doing and why it is really working. I looked it up in the help files to try to get some good info and to understand exactly whats going on, but there isn't much there that really explains anything.

thanks again I really appreciate your help.

Medyman
09-17-2008, 03:02 PM
Sure...

First, lets break down what's really happening with you're preloading. You're only preloading the .swf file. You're not initiating the .swf file. Initiating means having the assets within the .swf file available to manipulate. If you're familiar with JS, this is similar waiting until the DOM is ready to manipulate it. It's not enough that the .swf is visible, it's objects have to be on the display list.

If you know AS2, you'll recall that this is similar to onLoadComplete() and onLoadInit() of the MovieClipLoader class. onLoadComplete fired when the .swf was loaded and onLoadInit fired when the 1st frame of the .swf ran. What you need here is onLoadInit() and you're doing onLoadComplete()...so to speak.

So, what I asked you to add was a simple check.

addEventListener(Event.ADDED_TO_STAGE, init);

The ADDED_TO_STAGE event fires when (obviously) something is added to the stage. When something is added to the stage, it's in the display list and available to actions to manipulate.

So, this event will fire when the Document class (a.k.a. the main timeline, in this case) is loaded and initiated.

The problem you were having before was that the stage hadn't yet been initialized and you were adding event listeners to it. This makes sure that it is initialized, and then adds the event listeners.

Does that make sense?

punstc
09-17-2008, 03:56 PM
Perfect sense!. Thanks a lot medy I really appreciate the time and helping me understand what really is happening. The flash help files are lacking a bit in that sense.

thanks a lot