View Full Version : New to AS3 - Question regarding custom class
Andy69
February 23rd, 2009, 01:44 PM
Hey folks, I looked around to see if a similar question has been asked/answered but did not see anything, apologies if I missed something.
I am building a class, mostly as a learning tool, and I want to be able to keep track on the class level of the numerous instantiated objects of that class.
Ultimately I want one instance of the class to be able to control others to prevent more than one to be playing its content.
eg.
Inside a class method I want to be able to reference a different object of the same class and invoke its stop() method.
I suspect what I am trying to do is trivial but so far its not coming to me, any help would be appreciated.
Thanks for any assistance you can provide!
Andy
bLasTamos
February 23rd, 2009, 02:09 PM
Well, your class could have a static variable of that type and check for that in the constructor. In other words:
public class YourClass {
private static var _instance:YourClass;
public function YourClass() {
if (_instance != null) _instance.stop();
_instance = this;
}
}
Andy69
February 23rd, 2009, 02:18 PM
There will likely be 4 instances of the class, lets call them
ss1, ss2, ss3 and ss4
I think I'd need to put them in an array, but I am trying to figure out how to tell if the array is null so I can initialize it ( ssList = new Array() ) for the first object before it gets pushed.
?
Thanks,
Andy
bLasTamos
February 23rd, 2009, 02:27 PM
The same way I test if _instance is null, I guess?
Andy69
February 23rd, 2009, 03:02 PM
Yep, I was confused because I was putting the initialization in the constructor, which of course is wrong for a class variable. Thanks, I have it working now!
Much appreciated,
Andy
McGuffin
February 23rd, 2009, 03:07 PM
You should look into the Singleton design pattern. Here:
http://www.gskinner.com/blog/archives/2006/07/as3_singletons.html
You can only have one instance of the class, but can have multiple references to that one instance. This is probably what you want, not multiple instances with some capabilities shut off. A good example would have a music player. Instead of having instances of the Song class being capable of being played, and having to check if one is already playing, you have a PlaySong singleton class so that you can be sure that only one song can play at any given time.
andriusain
February 23rd, 2009, 04:31 PM
Hello all, I have a similar issue but I can't figure out how to apply your solutions to my particular case.
I have a few links that when clicked will instantiate a photo gallery class. A variable of the current link clicked is passed through to this class and this class uses this var to retrieve the images from a particular XML node.
I was playing with the following code for a while before I saw your post. Obviously there are many problems with it. How would you remove a previous instantiation of the class, if there is any, before the new one is called?
function loadGallery(e:MouseEvent):void {
if (currentPage != null) {removeChild(gallery);}
currentPage=MovieClip(e.currentTarget);
var gallery:Gallery=new Gallery(currentPage);
addChild(gallery);
}Thanks for your help, I'm a n00b.
andriusain
February 23rd, 2009, 04:45 PM
Got it, don't bother. Just had to look a bit better. :)
bLasTamos
February 23rd, 2009, 05:14 PM
A good example would have a music player. Instead of having instances of the Song class being capable of being played, and having to check if one is already playing, you have a PlaySong singleton class so that you can be sure that only one song can play at any given time.
That doesn't even make any sense, why would you want a singleton for that? You have the music player, which is responsible for whatever is playing.
Edit: I actually use singletons when I want some kind of concrete class deriving from an abstract static class, such as movement for a game character. The functions of getPosition and getVelocity only depend on the type of movement, so I have a superclass Movement and each sub-class is a singleton.
McGuffin
February 23rd, 2009, 05:21 PM
That doesn't even make any sense, why would you want a singleton for that? You have the music player, which is responsible for whatever is playing.
And what, exactly, is the music player? What's to stop me, or another one of X number of developers on the same team, from instantiating a new instance of the music player class and starting a new song, causing them to possibly to play over each other?
bLasTamos
February 23rd, 2009, 06:14 PM
Having two music players wouldn't make sense, so that should be your singleton, not the music being played.
McGuffin
February 23rd, 2009, 06:41 PM
Having two music players wouldn't make sense, so that should be your singleton, not the music being played.
Which is exactly what the singleton in my example was... perhaps you need to re-read what I wrote.
bLasTamos
February 23rd, 2009, 07:07 PM
If that's what you were saying then sorry I misunderstood you, you were talking about songs playing, as if independently from the player.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.