ActiveDen

Classes confusion

7343 posts
  • Attended a Community Meetup
  • Community Moderator
  • Has been a member for 5-6 years
  • United Kingdom
  • Contributed a Tutorial to a Tuts+ Site
  • Won a Competition
  • Contributed a Blog Post
  • Beta Tester
  • Bought between 50 and 99 items
+4 more
MSFX moderator says

Recently ive started writing classes, getting the hang of it slowly…

Am writing one now that will take an array of image URLs and an empty array, load each one and place it into the empty array… and then signal that it is complete so whatever initialised it can move on to doing the next task…

I have a couple of questions…

I want to be able to literally import the class using:

import domain.name.AssetLoader1;

and then use

AssetLoader1.init(...);

A very similiar structure to Tweener…

How do I go about making it like this and secondly how can I signal once its loaded them all to the main timeline…?

Cheers :)

2309 posts
  • Beta Tester
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 10 and 49 users
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 100 and 1 000 dollars
  • United States
theflyingtinman says

You need to make your init() function static to call it like that. And I would personally rather have the function return an array of loaded images rather than pass in an empty array .. something like …

    public static function init(urls:Array):Array
    {
        var imgs:Array = new Array();
        for (var i:int=0; i< urls.length; i++){
            // load the images specified bythe urls into 
            // movieclips and push them onto the new imgs array
            ...
            ...
            imgs.push(...)
        }
        return imgs;
    }

On second thoughts you will have to change that a bit to account for waiting for urls to be loaded :)

7343 posts
  • Attended a Community Meetup
  • Community Moderator
  • Has been a member for 5-6 years
  • United Kingdom
  • Contributed a Tutorial to a Tuts+ Site
  • Won a Competition
  • Contributed a Blog Post
  • Beta Tester
  • Bought between 50 and 99 items
+4 more
MSFX moderator says

what about the constructor though, dont you HAVE to have a constructor.. or can you not bother with one if your creating static methods…?

Yeah your right it would much nicer to have it return an array of images.. but I get so confused about how to get that back to the main timeline code… :(

7343 posts
  • Attended a Community Meetup
  • Community Moderator
  • Has been a member for 5-6 years
  • United Kingdom
  • Contributed a Tutorial to a Tuts+ Site
  • Won a Competition
  • Contributed a Blog Post
  • Beta Tester
  • Bought between 50 and 99 items
+4 more
MSFX moderator says

ok so ive tried changing it to static and got all excitied that it might actually work but then it keep giving me this now:

1180: Call to a possibly undefined method setValues. 1180: Call to a possibly undefined method setup.

The init(...) passed an object with various attributes which are then passed into setValues(...) and then setup() is called…

2309 posts
  • Beta Tester
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 10 and 49 users
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 100 and 1 000 dollars
  • United States
theflyingtinman says
what about the constructor though, dont you HAVE to have a constructor.. or can you not bother with one if your creating static methods…? Yeah your right it would much nicer to have it return an array of images.. but I get so confused about how to get that back to the main timeline code… :(

No need to have a constructor. To get it back to the calling timeline you would jsut use somethng like

var imgArray:Array = AssetLoader1.init(urlArray);

but if you are waiting for the images to load in the class function before returning the new array it might take too long and time out. So you would need to implement an asynchronous solution in the class that dispatches an event when all the urls are loaded. Maybe make the class extend EventDisplatcher.

2309 posts
  • Beta Tester
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 10 and 49 users
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 100 and 1 000 dollars
  • United States
theflyingtinman says
ok so ive tried changing it to static and got all excitied that it might actually work but then it keep giving me this now:

1180: Call to a possibly undefined method setValues. 1180: Call to a possibly undefined method setup.

The init(...) passed an object with various attributes which are then passed into setValues(...) and then setup() is called…

Any functions you call from a static function must also be static . .they can be private static though, as long as you are not calling them from outside the class .. eg:

private static function setValues(...)
{
}
7343 posts
  • Attended a Community Meetup
  • Community Moderator
  • Has been a member for 5-6 years
  • United Kingdom
  • Contributed a Tutorial to a Tuts+ Site
  • Won a Competition
  • Contributed a Blog Post
  • Beta Tester
  • Bought between 50 and 99 items
+4 more
MSFX moderator says

ok so any function I want to call in the form ClassName.function() must be public AND static, and if its called within a static function it must also be static…

think Ive got it :)

so returning the array, yeah its got to wait until they have all loaded, which could be alot of thumbs… I was thinking can’t I just use dispatchEvent(...) once they have all loaded and listen for it from the timeline…?

7343 posts
  • Attended a Community Meetup
  • Community Moderator
  • Has been a member for 5-6 years
  • United Kingdom
  • Contributed a Tutorial to a Tuts+ Site
  • Won a Competition
  • Contributed a Blog Post
  • Beta Tester
  • Bought between 50 and 99 items
+4 more
MSFX moderator says

I also have a class to manage these thumbs once they are loaded, this class… arranges them, adds listeners and removes Listeners for the thumbs, which are passed through via an array… so I have about 4 functions, is it ok to make them all static or is this bad design or…? I’m happy to be enlightened :)

755 posts
  • Bought between 1 and 9 items
  • Elite Author
  • Europe
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 500 and 999 users
  • Sold between 100 000 and 250 000 dollars
skyplugins says

Using a separate class for loading images sounds OK to me but why would you need another class to add/remove listeners? Just add listeners where you need them and remove them after you finish your job. I can’t really tell much regarding the use of static methods since I don’t know what you really want to do, your requirements etc. If you think a method should belong to a class rather than the instances go with static.

7343 posts
  • Attended a Community Meetup
  • Community Moderator
  • Has been a member for 5-6 years
  • United Kingdom
  • Contributed a Tutorial to a Tuts+ Site
  • Won a Competition
  • Contributed a Blog Post
  • Beta Tester
  • Bought between 50 and 99 items
+4 more
MSFX moderator says

My reasoning is that I have made so many galleries for clients etc etc with different thumbnail presentations etc etc so I thought it would be a neat idea to wrap each seperate presentation within a class ie..

BasicThumbs BlackAndWhiteThumbs ScaleThumbs etc etc…

Therefore each of those classes will be passed an array of thumbnails to arrange and add listeners to… if they are within a SWF which is about to be unloaded the listeners etc should be removed first so I want to be able to remove the listeners as well..so the functions I want to be able to call from outside of the class are just…

init() addListeners() removeListeners()

But I have no need for creating an instance of the Class, hence asking about this static modifier… I just want to be able to use it like this:

BasicThumbs.init(...) BasicThumbs.addListeners(); etc etc…

by
by
by
by
by