Jump to content
Search Community

Array question

ShortBus test
Moderator Tag

Recommended Posts

Hi All-

 

If I have an array containing thumbnail and fill size assets, but when targeting either I'm getting null reference errors.

I'm loading my assets & putting them into an array in the order I want in my xmlCompleteHandler and cleaning up preloader children and tweening in my displays in the mainCompleteHandler. Up through that point it's doing exactly what I want.

Much of how to get to this point was answered in this thread http://forums.greensock.com/viewtopic.php?f=6&t=6240

 

Now I'm at a loss as to how to properly access the thumb swfs to make them clickable and target the corrosponding main swfs in the node. I'd like to use the swapChildren method but I haven't been able to successfully get to properly triggering from the thumbs... I've used

for (var i:int = 0; i < pntLoaders.length-4; i++)

in my setupListeners function which will get rid of the null errors but in the end result of this project the nodes may range from 2 to 6 depending on the product.

 

I've read many threads & tutorials but haven't found anything yet that helps me in this situation.

The XML:

<?xml version="1.0" encoding="iso-8859-1"?> 





The AS:

import com.greensock.events.LoaderEvent;
import com.greensock.loading.display.ContentDisplay;
import com.greensock.loading.LoaderMax;
import com.greensock.loading.data.LoaderMaxVars;
import com.greensock.loading.SWFLoader;
import com.greensock.loading.data.SWFLoaderVars;
import com.greensock.loading.XMLLoader;
import com.greensock.TweenMax;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.text.TextField;

var mainHolder:MovieClip;
var xml:XML;
var xmlList:XMLList;
var pntLoaders:Array = [];

//Preloader stuff
var txtProgress:TextField = new TextField();
txtProgress.autoSize = TextFieldAutoSize.CENTER;
txtProgress.x = 187.5;
txtProgress.y = 190;
txtProgress.selectable = false;
var txtProgressFormat:TextFormat = new TextFormat();
txtProgressFormat.color = 0x888888;
txtProgressFormat.size = 18;
plWheel.visible = true;

function progressHandler(event:LoaderEvent):void {
plWheel.visible = true;
txtProgress.visible = true;
this.addChild(plWheel);
this.addChild(txtProgress);
var perc:Number = event.target.progress;
txtProgress.text = Math.floor(perc*100).toString() + "%";
txtProgress.setTextFormat(txtProgressFormat);
}
//End preloader stuff

var xmlLoader:XMLLoader = new XMLLoader("assets/data.xml", {name:"assetList",
									onComplete:xmlCompleteHandler});
xmlLoader.load();

function xmlCompleteHandler(event:LoaderEvent):void{
trace("XML loaded");
var xml = xmlLoader.content;
var xmlList = new XMLList(xml.swf);
//Setup the mainLoader with all swfs
var mainLoader = new LoaderMax(new LoaderMaxVars()
							   .maxConnections(4)
							   .onProgress(progressHandler)
							   .onComplete(mainCompleteHandler)
							   );
for (var p:int = 0; p < xmlList.length(); p++){
	//Setup the large swfs into mainLoader
	var productLoader:SWFLoader = new SWFLoader("assets/products/" + xmlList[p].@url,
												new SWFLoaderVars()
												.name(xmlList[p].@name)
												.x(0)
												.y(0)
												.scaleX(.25)
												.scaleY(.25)
												);
	mainLoader.append(productLoader);
}
for (var t:int = 0; t < xmlList.length(); t++){
	//Setup the thumbnail swfs into mainLoader
	var tWidth = 80;
	var tSpace = 18;
	var tCols = 4;
	var thumbnailLoader:SWFLoader = new SWFLoader("assets/thumbnails/" + xmlList[t].@url,
												new SWFLoaderVars()
												.name(xmlList[t].@thumbName)
												.x((t % tCols) * (tWidth + tSpace))
												.y(415)
												);
	mainLoader.append(thumbnailLoader);
}
mainLoader.load();

//Build an array of product and thumbnail swfs in the proper order
pntLoaders = mainLoader.getChildren();
var i = pntLoaders.length; 
while (--i > -1) {
	mainHolder.addChild(pntLoaders[i].content);
}
TweenMax.to(mainHolder, 0, {alpha:0});
}

function mainCompleteHandler(event:LoaderEvent):void{
trace("mainLoader loaded, mainCompleteHandler triggered");
//Tween out the preloader stuff & remove the children
TweenMax.to(plWheel, 0.5, {alpha:0});
TweenMax.to(txtProgress, 0.5, {alpha:0});
this.removeChild(plWheel);
this.removeChild(txtProgress);
trace("The number of children in the mainHolder is " + mainHolder.numChildren);
TweenMax.to(mainHolder, 1, {alpha:1});
setupListeners();
}

function setupListeners(){
for (var i:int = 0; i < pntLoaders.length; i++){
	var thumbArr = LoaderMax.getContent("assets/thumbnails/product" + (i + 1) + ".swf");
	var thumbButton:ContentDisplay = thumbArr;
	thumbButton.addEventListener(MouseEvent.CLICK, thumbClick);
	thumbButton.buttonMode = true;
}
}

function thumbClick(event:MouseEvent):void{
trace("The thumbnail was clicked");
trace("The current target is= " + event.target.loader);
trace("The mainHolder how has " + mainHolder.numChildren + " children"); //
}

 

Have I painted myself into a corner again? :lol:

 

Any ideas appreciated!

Thanks

Karl

Link to comment
Share on other sites

it seems you are jamming all your thumbnail and fullsize loaders into the same arrays, LoaderMaxes or whatever.

then when you try to loop through the thumbnail loaders to assign eventlisteners, you are looping through all the loaders and your var thumbArr = LoaderMax.getContent("assets/thumbnails/product" + (i + 1) + ".swf"); is failing when it hits a full size loader.

 

you can do 2 things:

 

add all your thumbnail loaders to their own array or sub-LoaderMax so that you can easily find them and loop through ONLY the thumbnail loaders

 

OR

 

just assign the CLICK eventListeners as soon as the thumnail loaders are created

 

for (var t:int = 0; t       //Setup the thumbnail swfs into mainLoader
     var tWidth = 80;
     var tSpace = 18;
     var tCols = 4;
     var thumbnailLoader:SWFLoader = new SWFLoader("assets/thumbnails/" + xmlList[t].@url,
                                      new SWFLoaderVars()
                                      .name(xmlList[t].@thumbName)
                                      .x((t % tCols) * (tWidth + tSpace))
                                      .y(415)
                                      );

//try adding this

thumbnailLoader.content.addEventListener(MouseEvent.CLICK, thumbClick);

     mainLoader.append(thumbnailLoader);
  }

 

from the greensock SWFLoader docs:

 

The SWFLoader's content refers to a ContentDisplay (a Sprite) that is created immediately so that you can position/scale/rotate it or add ROLL_OVER/ROLL_OUT/CLICK listeners before (or while) the swf loads. Use the SWFLoader's content property to get the ContentDisplay Sprite, or use the rawContent property to get the actual root of the loaded swf file itself. If a container is defined in the vars object, the ContentDisplay will immediately be added to that container).
Link to comment
Share on other sites

Thanks for the response Carl, yeah, I was jamming everything in the mainLoader. Jack had suggested loading it all at once and manipulating after. I know I missed something in the translation to my uneducated eye. LoaderMax creates it's own "array" of sorts doesn't it? And called through the content method?

I'll try your suggestion when I get in from outdoor work.

 

Thanks much

K

Link to comment
Share on other sites

Thanks for the tip Carl, it was so obvious once you showed me!

Now when I trace in that thumbClick function

trace("The current target is= " + event.currentTarget.name);

I get

The current target is= loader1
The current target is= loader2
The current target is= loader3
The current target is= loader4

or without .name is

The current target is= [object ContentDisplay]
The current target is= [object ContentDisplay]
The current target is= [object ContentDisplay]
The current target is= [object ContentDisplay]

Now I need to research how to swap the object CDs around per corrosponding thumbnail.

 

Thanks again, you're a great help

Karl

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