-
1. Re: referencing between custom classes
kglad Aug 25, 2014 6:58 AM (in response to rahex)yes, classes can communicate, but there are caveats and lots of different opinions. some people are dictatorial in their opinions about oop/class structure to the point (imo) that their preconceived notions of proper oop cause more problems than needed.
first, any function that has a private attribute, can NOT be called from another class, period. internal, protected and public functions can be called from another class (under certain conditions). this is non-controversial and fact.
generally, classes should communicate using events (when no data needs to be returned) or getters/setters (when data needs to be exchanged) so data can be error-checked. this is my opinion and there might be some disagreement, but not much.
finally, i find it helpful to use a static data class and/or singleton class as a central repository for data that can be accessed by all classes. this is definitely my opinion and some people think this is heresy and, if you're working with a large group (or maybe a small group) of other programmers that will share your classes, they are right. but i work alone and this works well for me.
-
2. Re: referencing between custom classes
dmennenoh Aug 25, 2014 7:45 AM (in response to rahex)I agree with kglad. I do all my projects with a similar structure - a main 'controller' class and then any screen in the app has a different class. All my classes, except for Main, extend EventDispatcher so that Main can listen for events and respond accordingly - ie call a method on the class. I don't use getters setters because I don't like how they appear as properties, I like a method to look like a method, but that's just personal preference. All my secondary classes then follow a similar structure - they all have a clip property - that is some movie clip from the library. They all have show and hide methods that Main can call, etc.
And I also will use singletons for data repositories when they make sense to my project - you can do without them, but in some cases they are very useful and save a lot of code.
-
3. Re: referencing between custom classes
moccamaximum Aug 25, 2014 8:05 AM (in response to rahex)package {
public class CustomClass1 {
public function CustomClass1() {
// constructor code
}
public function addMovieClips():void{
}
}
}
package {
public class CustomClass2 extends CustomClass1 {
public function CustomClass2() {
// constructor code
//because of inheritance CustomClass2 is able to call function from extended Class
super.addMovieClips();
}
}
}
-
4. Re: referencing between custom classes
dmennenoh Aug 25, 2014 8:11 AM (in response to moccamaximum)Sorry, I find that really ugly. I find the OOP paradigm of "Favor composition over inheritance" fits most projects unless you are building a framework and then inheritance is fine. But not here, all it does is make for unnecessary complication.
package {
public class Main{
private var screen1:CustomClass2;
public function Main() {
screen1 = new CustomClass2();
}
}
}
package {
public class CustomClass2 extends EventDispatcher {
private var clip:MovieClip;
public function CustomClass2() {
clip = new screen1(); //clip in the library
}
}
}
-
5. Re: referencing between custom classes
moccamaximum Aug 25, 2014 8:18 AM (in response to dmennenoh)The Op stated "I have not worked much with custom classes before, but want to learn more."
Inheritance is surely a thing you have to understand, even if you decide against it.
My example was not meant to criticize anything you said in your post.
It was just another possible option.
So you don`t have to be sorry at all ;-)
-
6. Re: referencing between custom classes
dmennenoh Aug 25, 2014 8:23 AM (in response to moccamaximum)Thanks, and I agree, you do need to understand inheritance, but you are exposed to it all the time just doing plain AS3 since the API is very much based on inheritance. I just find it hard to read when doing regular projects - I don't like having to dig four classes deep to find the method I'm calling. It might just be my brain too...
-
7. Re: referencing between custom classes
Amy Blankenship Aug 25, 2014 9:42 AM (in response to rahex)Personally, I think you're making this more complicated than it has to be. You can actually attach custom Classes to the Base Class of Library Symbols, then simply place them on stage. This eliminates the tedium of having to set x and y properties and make addChild calls. It also means that you don't have to compile in the definition of absolutely everything referenced in one way or another from your Main Document Class prior to Frame 1, which will help your movie appear to load more quickly.
If you name the instances when you place them on stage, the Flash player will build the instances and populate your properties with them. This is actually a great place for getters and setters, because when the Player populates the property, you have a function that can be called to do whatever you need to do when the instance is placed on stage (like populate a View with data for example). By doing this, you can avoid the problem many people have when they try to reference a stage object without truly knowing if it's on stage or not.
Note that any Class you reference in your "detailed code" as you call it is going to have to be compiled in, whether you use it or not, so sometimes it makes more sense to take a more pared-down, tailored approach. I actually refer to as many things as possible by Interface so that I can compile the actual implementation as late as possible (the Class definition will go prior to frame 10 in my case and the assets will compile in on the timeline where they are used).
I recommend against putting all your code in the same package, because as your code grows it will be hard to find what you need. Some people prefer to separate out out their packages according to functionality (model, view, controller, etc.), whereas others prefer to separate them out according to the part of the program they're for. I find that some projects lend themselves to one approach and others are easier to do the other way.
I wouldn't recommend getting dependent on Singleton. For one thing, it has been known to happen that your requirements change in such a way that you need more than one. For another, if you need to change the structure of your Singleton there are a ton of places in your project you'll have to go in and rip apart to make the change. I've been part of some projects where there were problems that flat couldn't be fixed because the use of Singletons and statics made it too risky. I think you'll be a better programmer if you avoid this anti-pattern, but you can make up your own mind.
-
8. Re: referencing between custom classes
rahex Aug 25, 2014 4:22 PM (in response to Amy Blankenship)Hi,
Thank you all for your replays.
Got some new terminology – Singelton, Setters and Getters.
Have not come across before:)
I do know the inheritance and have used it. And my little experience with it told me as well – it’s something you shouldn’t go crazy or fanatic with. Overusing it makes things complicated.
Amy, what you descibed is how I normally do use the classes – attach them to the Library symbols.
In my question I meant one concrete Movie Clip which is one sub-part of the application.
The Movie Clip is just an animated picture. That Movie Clip (girlSwim) is attached to the base class GirlSwim.
And that class is at the moment handling the animation for that girl swimming. It would be good to have that code on a sub-level symbol, like the Girl, but as a mask is swimming along as well, it got too complicated for me to code, cross reference the nested symbols. So the code ended up on top level symbol.
In that Movie Clip are other animating symbols as well and they are all handled with other custom classes.
When the girlSwim Movie Clip is activated, it’s on screen, dragonflies start to fly in and fishes start to swim in as well. They are not on the original picture, so not on stage.
And adding them is managed with timers and it continues as long as the Movie Clip is on screen.
At the moment I have all the stuff related to dragonflies and fishes on the first keyframe.
And it all functions nicely.
But as I’m practicing now using class files and keeping my timeline action script free (except some stop and goto staff to handle the timeline animations), I want to get that part of the code into class file as well.
I could but it all into the class GirlSwim and it would function.
But as the animation code for the girl is long enough, I though it would be good to have the code for dragonflies and fishes separate.
But it needs to be initiated by the GirlSwim class.
So … in my little-simple world I thought the following could work .. but it didn’t:)
1180: Call to a possibly undefined method timerStartDragonflies.
package
{
public class GirlSwim extends MovieClip
{
public function GirlSwim()
{
timerStartDragonflies ();
}
}
}
package
{
public class Dragonflies extends MovieClip
{
public function Dragonflies ()
{
//empty
}
internal function timerStartDragonflies():void
{
//code for starting timer
}
etc.
}
}
Is something like that possible or should I put it all into the GirlSwim class?
-
9. Re: referencing between custom classes
Amy Blankenship Aug 26, 2014 8:51 AM (in response to rahex)Is your actual question how to have a MovieClip that randomly makes dragonflies and one that randomly makes fish, but use the same Class for both and somehow tell it which you want at runtime?
There are two answers:
The way I would do it is to write the Class so it loops through its stage and uses getQualifiedClassName() and getDefnitionByName() to read off the Classes of any children it finds. Then I'd randomly create instances of those up until some maximum. Each would be set up to dispatch an Event when finished with its animation so it could be returned to the start point.
Another way to do it is to give it a property that accepts a Class or a Vector of Classes and then do the same as above. This is where using a getter and setter pair comes in in two places--the first is in the Class where this instance is (when the setter is triggered, that's where you pass in the Class(es)), and the second is where the property is set, that's where you want to start your process.
If it turns out this is not the question you were aiming for or you need more information/example code, post back.
-
10. Re: referencing between custom classes
rahex Sep 2, 2014 1:11 AM (in response to Amy Blankenship)Hi Amy,
thank you for your replay.
You are correct, it wasn't my question how to do it
My question was where to but the code for items which are not on stage, but added later.
But it got it all figered out for now. The dragonflies got an visible=false object on stage and the fishes got there own visible=false swimmingpool.
I would like to mark some of the answers here as helpfull (as I have seen some people doing), but I can't find such button.
-
11. Re: referencing between custom classes
AngryGamerProductions Sep 2, 2014 8:20 AM (in response to rahex)Heres how I do it, if I want to excecute a parent's function, I have a Global variable class, and I make it so that when I turn on a variable from the child, the parent excecutes a function whenever that variable is turned on, and then turns it off. I have the parent check with a timer. But if I want to excecute the child's function, I just call childHere.functionHere()
-
12. Re: referencing between custom classes
Amy Blankenship Sep 2, 2014 8:51 AM (in response to rahex)I'm not sure--I rarely ask questions, so I'm not sure what options are available once you've done it. I know that most of the answers I've seen marked helpful in the past few months were marked that way by an Adobe employee.
-
13. Re: referencing between custom classes
Amy Blankenship Sep 2, 2014 12:46 PM (in response to rahex)The actual answer to your restated question is that you can use a setter that will trigger when Flash populates your instance or you can watch ADDED_TO_STAGE (be sure to set use capture to true, since it doesn't bubble) and examine the newly added object to see if it's the one you care about. I find that the setter approach works best if there are few objects that need the same treatment and the ADDED_TO_STAGE approach works best if you are expecting to add many objects of the same type or they'll be added somewhere deep inside child MovieClips and I don't care to create a pathway that allows me to reference the object.
I would not suggest ever using globals if you can avoid it for reasons I've already linked to.




