Jump to content
Search Community

Re-using a sequence array

ash allen test
Moderator Tag

Recommended Posts

Hi,

I have found the following code to loop through an array of swf's - which works really smoothly (thanks).  I would like to re-use this code to load other arrays of swf's.  Could I make a separate class so I could re-use the code in a more streamlined way rather than pasting it over and over (any advice how to do this would be great)?   Also how can I center the x and y co-ordinates of the loader?

 

Thanks!

 

import com.greensock.*;
import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;

 

private var loaderIndex:Number = -1;
private var currentLoader:SWFLoader;

private var urls:Array = ["movie0.swf","movie1.swf","movie2.swf",];
private var swfs:LoaderMax = new LoaderMax({onComplete:completeHandler,onChildComplete:childCompleteHandler});
 

 

private function startMovieOne(event:MouseEvent):void
        {
            homePage_mc.visible = false;

           // Loop through urls and create a queue of swf loaders
            for (var i:Number = 0; i < urls.length; i++)
            {
                swfs.append( new SWFLoader(urls, {container:movieTwo_mc, autoPlay:false, width:480, height:480, scaleMode:"proportionalInside"}) );
            }
            swfs.load();

           btnListener();

 

       }

        function childCompleteHandler(e:LoaderEvent):void
        {
            trace(e.target + " loaded");
            e.target.content.visible = false;

        }

        function completeHandler(e:LoaderEvent):void
        {
            trace("all swfs loaded");

            initCurrentLoader();
            addEventListener(Event.ENTER_FRAME, trackSWFPlayback);
        }

        function initCurrentLoader()
        {
            loaderIndex++;
            
            if (loaderIndex == swfs.numChildren)
            {
                //reset back to 0 if the last swf has already played
                loaderIndex = 0;
            }

            //dynamically reference current loader based on value of loaderIndex
            currentLoader = swfs.getChildAt(loaderIndex);

            //make the content of the current loader visible
            currentLoader.content.visible = true;

            //tell the current loader's swf to to play
            currentLoader.rawContent.gotoAndPlay(1);
        }

        function trackSWFPlayback(e:Event):void
        {
            //trace(currentLoader.rawContent.currentFrame);
            //detect if the loaded swf is on the last frame
            if (currentLoader.rawContent.currentFrame == currentLoader.rawContent.totalFrames)
            {
                trace("swf done");
                //hide and stop current swf
                currentLoader.content.visible = false;
                currentLoader.rawContent.stop();
                //set up and play the next swf;
                initCurrentLoader();
            }
        }

 

private function btnListener():void
        {
            home_btn.addEventListener(MouseEvent.CLICK,resetScreens);
        }

 

public function resetScreens(event:MouseEvent):void
        {
            //unload content
                currentLoader.content.visible = false;
                currentLoader.rawContent.stop();
                trace("unloaded");
                homePage_mc.visible = true;
 
        }

 

 

 

Link to comment
Share on other sites

Hi, 

 

I imagine you could make a class that you pass an array of urls to. Unfortunately, figuring out how to scale these types of projects in detail isn't the type of assistance we can offer right now.

 

As for your other question, Do you want to physically center the ImageLoader's content on the stage or literally move its registration point? 

 

By default ImageLoader's have centerRegistration set to true which effects scaling but not positioning. 

 

You could nest your content in another Sprite and offset its position by negative half width and height. I'm not aware of other methods for changing the location of the registration point of a display object.

Link to comment
Share on other sites

Hi Carl - thanks for your suggestions.  I want to center the ImageLoaders content on the stage.  Previously I was using the below code but I'm not sure how to write that for loadermax.

loader.x = (stage.stageWidth/2 - loader.width/2);
loader.y = (stage.stageHeight/2 - loader.height/2);

 

I'm new to coding and using classes, I've been playing around with them today but can't seem to get a second array to load.  I'll keep hunting for more tutorials!

 

cheers,

Ashleigh

Link to comment
Share on other sites

Your centering code is close, you just have to make sure that the object you are centering is the content of the SWFLoader

currentLoader.content.x = (stage.stageWidth/2 - currentLoader.width/2); 
currentLoader.content.y = (stage.stageHeight/2 - currentLoader.height/2);
Link to comment
Share on other sites

Hi again - I tried replacing the code, but have come up with this error:

 

1119: Access of possibly undefined property width through a reference with static type com.greensock.loading:SWFLoader.
 

Is there a shorthand way to add it in here with x:  , y:  ?:

 

swfs.append( new SWFLoader(urls, {container:movieTwo_mc, autoPlay:false, width:480, height:480, scaleMode:"proportionalInside"}) );

 

I've also been trying all day to get a second array of swfs to load.  I have tried various things including splicing the array and creating a second swfLoader - but the original array is always there.  It stops playing when I go back to the homepage but when I press the second button I get the original array plus the new swfs.  Any advice on how to get a second array to play would be greatly appreciated.
           

Thanks.

Link to comment
Share on other sites

Ah - thanks for pointing that out. 

 

Also I found where I went wrong in my code and have managed to re-use the loader code correctly - yay!

 

Actually I'm still having problems.   I can get a second array of swfs to play by replicating all the code, but then I get this message:

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

 

and it breaks.  I'm not sure how to fix this?

 

Here is how I have replicated the code (using code listed above).  I have been going through other forum posts to see if I can re-use the loaders but this is the only way I have made it work.

 

private var currentLoader:SWFLoader;
private var currentLoader2:SWFLoader;
private var swfs1:LoaderMax = new LoaderMax({onComplete:completeHandler,onChildComplete:childCompleteHandler});
private var swfs2:LoaderMax = new LoaderMax({onComplete:completeHandler2,onChildComplete:childCompleteHandler2});
private var urls:Array = ["movie0.swf","movie1.swf","movie2.swf"];
private var urls2:Array = ["swf0.swf", "swf1.swf", "swf.swf"];

 

//I use this code to switch between screens/movies

 

public function resetScreens(event:MouseEvent):void
        {
            if (level == "ONE")
            {
                swfs1.unload();
            }
            if (level == "TWO")
            {
                swfs2.unload();
            }
            
            homePage_mc.visible = true;
        }

Thanks.

Link to comment
Share on other sites

Sorry, but its very difficult to see where an error can be by comparing 2 blocks of code in different posts.

 

I would recommend going into the trackSWFPlayback() method and add some traces to see which object is null.

 

start with 

trace("currentLoader = " + currentLoader);

 

 

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...