Jump to content
Search Community

[SOLVED]Loading many things and applying them to a class.

mrEmpty test
Moderator Tag

Recommended Posts

Hello :)

 

Day 2 of my real attempt to get Actionscript to make sense to me. :) It's proving hard because all I do is play with TweenMax and wonder why it doesn't exist for iOS. Anyhoo...

 

Not sure if this is appropriate and if it is, if this is the correct place. So please delete and shout at me if needs be.

 

I have a custom class, a basic UI reusable test thing. It's class code:

 

package  {

import flash.display.MovieClip;
import flash.display.Sprite;


public class panel extends MovieClip {
	private var panelSprite:Sprite = new Sprite();

	public function panel(pText:String, pSprite:Sprite) {
		//create and place the sprite
		panelSprite.width = 320;
		panelSprite.height = 151;
		panelSprite.x = panelSprite.y = 0;
		addChildAt(panelSprite, 0);

		//populate my panel
		panelText.text = pText;
		panelSprite = pSprite;
	}
}

}

 

Now, as you can see it demands a sprite and some text. My XML contains both (shamelessly stolen from Jack)...

 

<?xml version="1.0" encoding="utf-8"?>






 

I want to create a new instance of my custom class and fill each one with an image and the corresponding string. So I load up the XML...

 

public function Panels()
	{
		//read the XML
		var xmlLoader:XMLLoader = new XMLLoader("assets/panels.xml", {onComplete:xmlCompleteHandler});
		xmlLoader.load();
	}

 

Then my thought would be to run through a loop and pass the stuff through. Here I hit a tree...

 

private function xmlCompleteHandler(event:LoaderEvent):void
	{
		//take the panel tags from the xml and put them in an xml list array
		var panels:XMLList = event.target.content.panel;
		var panelsText:XMLList = event.target.content.dText;
		var queue:LoaderMax = new LoaderMax();
		//run through a loop for each panel item
		for (var i:int = 0; i < panels.length(); i++)
		{
			//add an image loader for each panel item
			queue.append(new ImageLoader("assets/" + panels[i].@file, {}); //err, um, what happens now?
		}
	}

 

You see I thought I'd go through the panel tags, and for each one I'd create a new panel. panel wants a sprite which would be the image, but it also needs a string. I can get one or the other to work, but not both. Do I need to create two loaders, images and strings, then do another loop to create the panel classes and pass through the data from two arrays created by the original two loaders? Or what? This is making me crazy (in a good way) and the ark nights and nicely bubbling dumpling stew isn't helping me concentrate.

 

So please help otherwise my waistline will expand even more and my knowledge will stay small and shrivelled.

 

Thank you.

Link to comment
Share on other sites

Quite impressive progress you have made!

 

the missing puzzle pieces are:

1: you need to associate your text with each ImageLoader

 

- you can pass this into the vars object of the ImageLoader

 

2: you need to be able to access the image and text once the image is loaded

 

- you can assign an onComplete function to each ImageLoader so that you can grab the ContentDisplay object and read the text and pass it into your custom class.

 

3: you also need to tell your queue to load() ;)

 

- queue.load()

 

the following worked fine for me as a document class

 

package {

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


public class Panels extends MovieClip {


	public function Panels() {
		// constructor code
		var xmlLoader:XMLLoader = new XMLLoader("assets/panels.xml",{onComplete:xmlCompleteHandler});
		xmlLoader.load();
	}

	private function xmlCompleteHandler(event:LoaderEvent):void {
		//take the panel tags from the xml and put them in an xml list array
		var panels:XMLList = event.target.content.panel;
		var panelsText:XMLList = event.target.content.dText;
		var queue:LoaderMax = new LoaderMax();
		//run through a loop for each panel item
		for (var i:int = 0; i 				//add an image loader for each panel item
			queue.append(new ImageLoader("assets/" + panels[i].@file, {name:"panel" + i,  description: panels[i].@dText, onComplete:setupPanels}) );
		}
		queue.load();
	}

	private function setupPanels(e:LoaderEvent):void {
		trace("the loader is " + e.target);
		trace("the loader's ContentDisplay object is " + e.target.content);
		trace("the loader's text is " + e.target.vars.description +"\n");

		//i'll put each image directly on the stage just to prove it works 
		//but you can try to pass it into your Panel object

		var image:ContentDisplay = e.target.content;
		image.x = Math.random() * 100;
		image.y = Math.random() * 100;
		addChild(image);

	}

}




}

Link to comment
Share on other sites

Hello.

 

Thanks :) But not really. Google plus your tutorials and the stuff on the LoaderMax page have really helped. It's now working. In the end I wrote something similar for iOS, then wrote that using standard AS3 syntax from the Obj-C code, then took that and replaced the standard stuff with the LoaderMax stuff. Hey, if it works!

 

But the goldmine you gave me was the text in the Vars. Wow. I like that.

 

And LoaderMax (which assume simply extends the default XML stuff then adds loads of cool stuff?) seems faster. That may just be my excitement though.

 

I'm having real trouble purchasing the Extremely Green, wondering if it's my ISP as when I choose to purchase my browser just sits there. But I 100% am buying. I wanna trow me some stuff around and split up some text along with loading many things from an XML file. :D

 

Have to ay I didn't want to move to Flash but this is a giggle.

Link to comment
Share on other sites

glad you got it working.

 

I don't know if I'm interpreting your

 

Thanks But not really.

 

correctly. I've attached the files I made incase you want to see them in action.

 

I can't help you with your purchase troubles but I'm sure they will be addressed.

 

-c

Link to comment
Share on other sites

Oh sorry, yeah that kinda reads bad! I meant thanks to saying I'd come a long way progress wise, when really it's all just a mix of Google and your help. So I wrote it to give across the opposite of what I intended. So apologies for that. You've been great.

 

Think it's my ISP. I borrowed my neighbours wifi and that works, but it's slow and buggy. I'll try from work tomorrow.

 

So now the basics are working, I better think up something pretty to do with it, fill it with cool stuff and test it on an Android tablet. :)

 

Thanks again for all the help. And apologies for my innate ability to offend.

Link to comment
Share on other sites

One thing, this code:

 

package
{

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


public class Panels extends MovieClip
{
	public var maxComs:Number = 2;
	public var pWidth:Number;
	public var pHeight:Number;

	public var newMC:MovieClip = new MovieClip();

	public function Panels()
	{
		var xmlLoader:XMLLoader = new XMLLoader("assets/panels.xml", {onComplete:xmlCompleteHandler});
		xmlLoader.load();
		pWidth = 320;
		pHeight = 181;
		addChild(newMC);

		newMC.addEventListener(MouseEvent.MOUSE_DOWN, _mouseDownHandler, false, 0, true);
		newMC.addEventListener(MouseEvent.MOUSE_UP, _mouseUpHandler, false, 0, true);
		stage.addEventListener(MouseEvent.MOUSE_UP, _mouseUpHandler, false, 0, true);
	}

	private function xmlCompleteHandler(event:LoaderEvent):void
	{
		var panels:XMLList = event.target.content.panel;
		var panelsText:XMLList = event.target.content.dText;
		var queue:LoaderMax = new LoaderMax();
		for (var i:int = 0; i < panels.length(); i++)
		{
			queue.append(new ImageLoader("assets/" + panels[i].@file, {name:"panel" + i, description:panels[i].@dText, numb:[i], onComplete:setupPanels}) );
		}
		queue.load();
	}

	private function setupPanels(event:LoaderEvent):void {
		trace(event.target.vars.numb);
		var pane:panel= new panel(event.target.content, event.target.vars.description);
		pane.alpha = 1;
		pane.y = (event.target.vars.numb * pHeight);
		pane.alpha = 0;
		newMC.addChild(pane);

		TweenMax.to(pane, 1,{alpha:1});
	}

	private function _mouseDownHandler(event:MouseEvent):void
	{
		newMC.startDrag();
	}

	private function _mouseUpHandler(event:MouseEvent):void
	{
		newMC.stopDrag();
	}


}

}

 

which loads the images and the text, puts them into the container class and put them into an empty movieclip in a column, traces the number i as it loads each image. It traces 1, 2, 3, 4, 0 but never 5. And it traces them in order: 1, 0, 3, 2, 4. Why is this?

Link to comment
Share on other sites

assuming you still have 5 panels in your xml, when you loop through your XMLList the first panel is at an index of 0 and the last panel is at an index of 4.

for that reason i will never have a value of 5 if the loop condition is

 

(i

 

the way you have things set up now is fine but you could do

 

numb:[i + 1] in your ImageLoader vars.

 

for the load order being non-sequential keep in mind that by default LoaderMax has a maxConnections property set to 2 which means that it will attempt to load 2 things at once.

also, the order in which items are added into a LoaderMax does not dictate the order in which they get downloaded. due to network connectivity and filesize variances it's entirely possible that the second item will show up before the first.

 

in order to enforce each item loading in order you can set the maxConnections to 1.

 

var queue:LoaderMax = new LoaderMax( { maxConnections:1 } );

 

-carl

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