Jump to content
Search Community

Selfloader for flash game as3

2slick test
Moderator Tag

Recommended Posts

I need to create a selfloader for my flash game. My game doesn't use any frames except the first frame. Most all of the code is contained in classes.

I was told that LoaderMax can solve this problem easily, but I can't find any tutorials on how to make selfloaders.

Any help would be appreciated,

 

Angelo

  • Like 1
Link to comment
Share on other sites

Hi and welcome to the GreenSock forums.

 

Here is a very basic example of implementing a SelfLoader

import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;


//create a SelfLoader
var loader:SelfLoader = new SelfLoader(this, {name:"self", onProgress:progressHandler, onComplete:completeHandler});


function progressHandler(event:LoaderEvent):void {
    trace("progress: " + event.target.progress);
}
function completeHandler(event:LoaderEvent):void {
    trace("complete");
}

 

Keep in mind, SelfLoader won't be able to initialize until frame 1 is loaded. Typically with a situation where you don't use external loading but want to track self loading, you would dump all your heavy assets on frame 2 of a timeline and put only your loading code and progress bar assets on frame 1 (keeping the initial load prior to the first elements rendering on screen as lean as possible). 

 

Get GSAP for AS3 here: http://www.greensock.com/?download=GSAP-AS3

Link to comment
Share on other sites

Thanks Carl, but what I really need is an explanation on how to make a complete selfloader preloader. I understand the concept, I just don't understand how to code it in as3 .I've been Googling this for a month now. All I can find are outdated methods that I can't get to work.

Link to comment
Share on other sites

The only addition you would need to the code above is really a way to adjust a progress bar using the current progress of the SelfLoader. Its easiest just to set the scaleX of a rectangle to match the progress (values 0-1);

 

Here is the full code in a working demo

import com.greensock.*;
import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;




//create a SelfLoader
var loader:SelfLoader = new SelfLoader(this, {name:"self", onProgress:progressHandler, onComplete:completeHandler});




function progressHandler(event:LoaderEvent):void {
    trace("progress: " + event.target.progress);
bar.scaleX = event.target.progress;
}
function completeHandler(event:LoaderEvent):void {
    trace("complete");
gotoAndStop(15);
}
  


stop();

So on Frame 1 you just have this code and your loader bar.

 

Dump all your other code and visual assets on frame 15 and you are good to go.

Here is the swf in action

 (i just have a 200kb image on frame 15 so that you can see the bar grow)

 

I attached a CS5 zip.

 

Its best to test online or use a version of flash (CS6 or lower) that has a bandwidth profiler.

SelfLoader_CS5.zip

  • Like 1
Link to comment
Share on other sites

Thanks Carl, Very helpful.

I created my game in the document class. So would I have to rewrite my scripts from the document class to the timeline on frame 15? Is there an easier way to do this? Maybe reference my document class on frame 15 somehow or something?

Link to comment
Share on other sites

I'm really not sure how to re-architect what you have.

I know there are ways to set certain classes to export on particular frames, not so sure how the Document class works, you may just have to use an import and then instantiate it manually on frame 15, something like

import com.mysite.Game
var game:Game = new Game();
Link to comment
Share on other sites

If you're using Flex compiler (Flash Builder, FlashDevelop etc) to build your project, then you can probably keep your document class as it is and add a preloader with some compiler directives. Note: this doesn't work if you are just exporting from Flash CS.

 

http://fortheloss.org/how-to-preloader-in-flash-builder-4-7/

http://pixelpaton.com/?p=4642

  • Like 1
Link to comment
Share on other sites

  • 3 months later...

Hi, I'm in a similar situation in that my main timeline is blank and all my code is in the document class and other classes. The bulk of my Flash's file size comes from several audio and artwork assets in the library. I want to have a progress bar as my Flash loads, and I want everything in a single swf rather than using a second preloader swf.

 

I put all my selfLoader code on frame 1, but when I test it the screen just stays blank until the entire swf is loaded, and then my progress indicator suddenly shows up at 100%. Any ideas on what I'm doing wrong?

 

I'm using NetBalancer to limit my browser download bandwidth to like 10 KBps to test this.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...