Copy link to clipboard
Copied
Hi guys
If I create a class (scroller class), and want that class to be accessed by lots of other classes, so that if one class adds a movieclip to the scroller, the previous movieclip in there (possibly from another class), is removed, how do you make it so that the scroller class is affected by all the other classes?
Do you have to
- instantiate the scroller class in your Document class
- inject it's instance into the constructors of all the classes that need access to that scroller
?
or
- create a global model to inject into all the classes, including the scroller, and populate a setter with the movieclip that you want to scroll, and have the scroller get that clip using the getter?
Because if I simply set up a var scroller:Scroller = new Scroller(); in each class, they will all have thier own exclusive scrollers, right? Which I don't want - I just want one that all the classes can affect.
Cheers guys.
Copy link to clipboard
Copied
create a singleton or static scroller class that contains a public method to which you can pass your objects that you want the scroller class to impact. eg, the scroller class can add listeners etc to the passed object. all the passed objects should have the same basic setup, eg a movieclip that "scroll" and a mask that reveals the scrolling movieclip in the region you want visible.
Copy link to clipboard
Copied
Cheers kGlad
But how do I give all of the classes that need it access to that scroller class?
The way I understood it, a static class that receives an object from class5 isn't going to react to receiving an object from class7.
Better way to explain how I'm seeing it:
Take a static class keeps adding numbers it receives to a var total:int;
Class5 sends it 5 - total is 5
Class5 sends it 7 - total is 12
Class8 sends it 3 - total is 3, as it's totally separate from class5.
Or are static classes able to be completely independent of each class, but react to many?
Copy link to clipboard
Copied
Singletons are bad practice! They are also not object-oriented, but instead are procedural. In other words, the main people who use them are people who actually can't get their heads around how to do Object Oriented Design and so have to revert to the horrible practices they can understand. Don't be that person.
The reason you're having this issue is because your design is bad at the root. All of these Classes should not be directly adding children to a scroller. Instead, they should be dispatching events that describe what has happened in them (that may or may not need to result in something being added to the scroller). The one Controller Class that knows about the scroller should be listening for these events and make a decision about what, if anything should be added to the scroller.
That Controller will then either directly remove/add children to the scroller or it will set a property, dispatch an event, or call a method on the scroller that will allow the scroller to handle whatever needs to be handled internally.
Copy link to clipboard
Copied
if you use a static class, it means all your objects will use that one static class. you won't have instances of the scroller class.
that class will handle all the events etc. eg,
package {
public class Scoller {
// import whatever needed
public function Scroller() {
}
static public function addScoller(obj:MovieClip):void{
obj.mask.addEventListner(MouseEvent.MOUSE_MOVE,f);
params(obj,0,obj.mask.y,obj.mask.height,obj.mask.y-obj.scrolled_mc.height+obj.mask.height);
}
private function f(e:MouseEvent):void{
obj.scrolled_mc.y=MovieClip(e.currentTarget.parent).m*localY+e.currentTarget.parent.b;
}
private function params(mc:MovieClip,x1:Number,y1:Number,x2:Number,y2:Number):void{
mc.m=(y1-y2)/(x1-x2);
mc.b=y1-mc.m*x1;
}
}
}
Copy link to clipboard
Copied
klgad: celebritymusic asked for an OOP solution, which Singleton is not and static Classes are not. You use Singleton/statics when you can't figure out how to get the right reference to the right object, so you just give everybody a reference to it and damn the consequences.
It also means that
In short, bad news. Did you read the links I posted about why global state is bad practice and not actually object oriented at all?
Copy link to clipboard
Copied
i know you are a very opinionated individual and i know you are convinced you are right.
Copy link to clipboard
Copied
I'm very protective of newbies, especially when it's obvious that they want to follow good practices. I've inherited enough code from people who think junk like Singletons and statics are ok that I feel like I owe it to my fellow developers to try to steer the new ones away from that.
If you want to write that kind of code, go ahead--that's between you and your employer.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now