PDA

View Full Version : Communicating between classes.



sceneshift
June 12th, 2007, 03:50 PM
I am sure this is more simple than it seems. Say I have two classes, the main app class (in this instance called Main) and a imageLoader class in com.site.library which has been imported into the Main class via import com.site.libary.imageLoader.

How do I then pass variables from my main class to the imageLoader class, for example in order to tell it which image to load.

I am sorry for the retarded question, I have searched the internet and forums for ages but can't find any examples of this.

efnx
June 12th, 2007, 05:30 PM
You can have classes talk to each other using the class's functions. Just call one class function through your main class and pass your variable as a parameter [argument] of that function.
So if your Main.as looked something like this:



package yourpath.yourpackage
{
import yourpath.yourpackage.imageLoader;

public class Main
{
var image:imageLoader = new imageLoader();
public function Main():void
{
image.loadImage("nameofimage.jpg");
}
}
}
Then write imageLoader.as to have a function named "loadImage" that takes a string as a parameter:


package yourpath.yourpackage
{
import flash.display.Sprite;

public class imageLoader
{
public function imageLoader():void
{
//initialize imageLoader however you want
}

public function loadImage(imageName:String):void
{
trace(imageName);//traces the name of the image passed in your main class
doStuffHere();
}
}
}
Hope that helps.

sceneshift
June 13th, 2007, 05:49 AM
Thank you very much, very appreciated.

sceneshift
June 13th, 2007, 08:34 AM
This is driving me insane! How do I addChild(loader) between classes? If I try to add an addChild method after loading the image, it returns a compile error.

Charleh
June 13th, 2007, 11:14 AM
This is driving me insane! How do I addChild(loader) between classes? If I try to add an addChild method after loading the image, it returns a compile error.

Do you want to call addChild on the imageloader class to add children to it? If so then you either need to extend a class which already implements addChild or create a new method called addChild which performs the function you require in the imageloader class.

sceneshift
June 13th, 2007, 11:22 AM
My loaderClass.as file looks like this:



package com.test.library {
import flash.display.*;
import flash.net.URLRequest;
import flash.events.Event;

public class doLoad {

public function doLoad():void {
}

public function loadInit (imageFile:String) {
var loader:Loader = new Loader();
addChild(loader);
loader.load(new URLRequest (imageFile));
}
}
}


Then in my main file I reference it like so:



...
loadImage:doLoad = new doLoad();
loadImage.loadInit("image.jpg");


Thanks, I am sure this is really simple and daft.

Charleh
June 13th, 2007, 11:25 AM
My loaderClass.as file looks like this:



package com.test.library {
import flash.display.*;
import flash.net.URLRequest;
import flash.events.Event;

public class doLoad {

public function doLoad():void {
}

public function loadInit (imageFile:String) {
var loader:Loader = new Loader();
addChild(loader);
loader.load(new URLRequest (imageFile));
}
}
}


Then in my main file I reference it like so:



...
loadImage:doLoad = new doLoad();
loadImage.loadInit("image.jpg");


Thanks, I am sure this is really simple and daft.

Ahh right I see...you are trying to run addChild on a class which you haven't implemented an addChild method in - I'm guessing you want to use the regular functionality of MovieClips addChild (though I'm not sure in AS3.0 what the correct syntax is)

try adding 'extends MovieClip' after your public class doLoad

though I doubt that will give you the functionality you seek - are you trying to keep a list of loader objects that the doLoader class has loaded?

sceneshift
June 13th, 2007, 11:31 AM
No, the class itself is incomplete. What I eventually want it to do is take all the imagefiles it is given, load them as movieclips, place them in an array and then display them onto the stage. The purpose of the class is so that from my main class I can just type:

loadImage.loadIinit("whateverimage.jpg"); each time I want to load a new image.

Sorry if I am not making much sense, perhaps this isn't even the best way to do this! Classes are a relatively new feature for me.

Charleh
June 13th, 2007, 11:46 AM
So yeah you are trying to add a list of objects to the main object.

What you need to do is implement an addChild method

Declare an array in the main class - then in your addChild method on the main class pass in the object and use yourArray.push(yourObject) to add it to the array.

Then you can write some find and remove functions to find objects in the list or remove objects

example

declare


var listLoaded:Array;

in the constructor for the class you need to instantiate the array


listLoaded = new Array();

the addChild method would be


public function addChild(newChild:Loader) {
listLoaded.push(newChild);
}

then you can call it as you have been trying

efnx
June 13th, 2007, 01:24 PM
public function addChild(newChild:Loader) {
listLoaded.push(newChild);
}then you can call it as you have been trying

The only thing I'd be worried about is that pushing the image into the array won't actually attach it to a displayObjectContainer and make it visible, you'll still have to run through that array and addChild(listLoaded[i]) while i<listLoaded.length... ...it would be simpler to have the class extend a displayObjectContainer that already has addChild() included, like you had mentioned before. If you don't need the timeline functionality of a MovieClip, I'd suggest extending Sprite(), for better performance.

sceneshift
June 13th, 2007, 01:39 PM
I am very confused! Currently, both of my classes extend sprite. Is this not correct? Should my main class not extend anything? Cheers.

efnx
June 13th, 2007, 01:57 PM
scene, if your classes are extending Sprite() then you won't have to re-define addChild (effectively overriding it). Inside your doLoad class, use addChild(loader) to add the image to your doLoad then after you call


loadImage.loadInit("image.jpg");
in your main file, use addChild(loadImage) to add the instance of your class to the display list. This should work, sorry for the mix-up.

sceneshift
June 13th, 2007, 02:38 PM
Fantastic! Thanks for the help guys I am so glad to get this sorted.

dthought
June 15th, 2007, 03:41 AM
Don't forget that in certain cases if you want access to the main stage from within a class' scope, you'll need to pass the stage as a reference. This only really applies to classes that are not added to the display list, though. Display list-added instances should automatically inherit references to stage and root.

sceneshift
June 15th, 2007, 04:04 AM
Don't forget that in certain cases if you want access to the main stage from within a class' scope, you'll need to pass the stage as a reference. This only really applies to classes that are not added to the display list, though. Display list-added instances should automatically inherit references to stage and root.

I see, can you please explain how you go about adding a class to the Display List? What are the advantages of doing this over adding the stage as a reference.

Aquilonian
June 15th, 2007, 04:06 AM
Hi guys, long time no see
If i pass stage as a reference, does it counts as if am i passing too a reference for all the
other objects inside stage?

Aquilonian
June 15th, 2007, 04:17 AM
I see, can you please explain how you go about adding a class to the Display List? What are the advantages of doing this over adding the stage as a reference.
Its
addChild(myInstance);
addChild(that);
after that they have the root property running ok
There is a tip of the day for that
The advantage?None.Its just that when you are using movieclips or Sprites its another way to acess the root