View Full Version : Object Oriented Programming : A simple.. "test"
APDesign
September 13th, 2003, 02:24 AM
I'm trying to learn a bit of OOP, I checked out this AWESOME site (here) (http://www.debreuil.com/docs/ch01_Intro.htm) which I found in the forums of course, and it really got me interested in it. I read through it all once, then went over parts that gave me a little trouble. I"m no master of it of course, in fact I'm a huge newb at it. I was looking for some sort of "challenge" i guess that involves OOP. I saw one on here by Senocular(Spelling?) and I was going to give that one a go but it seemed to advanced for my knowledge. So if someone has a somewhat "easy" little challenge I would like to hear it, because I'm trying to find ways to practice using OOP. Thanks a lot if you have any ideas.
~APD
grandsp5
September 13th, 2003, 03:43 AM
I dunno if you have acces to AS 2.0 (download the trial if you don't) but try writing a couple of classes with private and public sectors using strict data typing. I know something I always did in computer science was simulate the motion/behaviour of fish. We had fish objects with shape, color, size, species, breeding probability, etc etc. Try that
APDesign
September 13th, 2003, 04:46 AM
crap... AS 2.0 has changed the way OOP works in flash? THat would really suck since I've spent a few days now trying to learn it... I'll give the fish thing a try sooner or later though, I dont know what you mean by public and private sectors though.. do you just mean "public" being properties of ALL fish and "private" being instance specific properties?
~APD
senocular
September 13th, 2003, 09:28 AM
AS 2.0 didnt necessarily change OOP, it just gives a different format to present it in. Its organized differently and offers new stricter means of error protection so that you can't accidentally do things you really shouldn't be doing. The concepts, however, are the same. I wouldn't worry about AS 2.0. It's more for experienced programmers and people who've pretty much got 1.0 down.
ahmed
September 13th, 2003, 12:04 PM
Eric Jr. (http://www.kirupaforum.com/forums/member.php?action=getinfo&userid=2561)is working on some AS 2.0 OOP tutes, he told me they'll be up like next week.. those should help you :)
senocular
September 13th, 2003, 12:58 PM
Hmm, that still doesn't offer a solution to the original inquiry. I asume the example you were referencing was the OOP creatures thing? So something OOP but not as complicated...
Is there anything you don't feel comfortable with and would like to avoid? That example I think focused a lot on inheritance and movieclip control (though the movieclip control wasn't really part of the example I dont think). Or for that matter is there something you feel you need to focus on to help you learn oop?
In other words, what kind of example are you looking for? :)
APDesign
September 13th, 2003, 02:45 PM
Well, i just read a post you made awhile ago here (http://www.actionscript.org/forums/showthread.php3?s=&threadid=28710). It helped a lot.. but now I am a little confused.. is that just a type of inhertiance system (with the Geek.prototype = new Person();)? The way I learned to make sublcasses (I think) is like this...
Lifeform = function( name )
{
this.name=name;
}
Fish = function( name )
{
this.$_base = Lifeform;
this.$_base( name );
delete this.$_base;
}
Fish.prototype.__proto__=Lifeform.prototype;
Shark = function( name )
{
this.$_base = Fish;
this.$_base( name );
delete this.$_base;
}
Shark.prototype.__proto__ = Fish.prototype;
Shark.prototype.fins =3;
Shark.prototype.eyes ="beady";
Shark.prototype.speed =5;
Angel = function( name )
{
this.$_base = Fish;
this.$_base( name );
delete this.$_base;
}
Angel.prototype.__proto__ = Fish.prototype;
Angel.prototype.fins =2;
Angel.prototype.eyes ="buggy";
Angel.prototype.speed =5;
killer = new Shark( "Killer" );
peppy = new Angel("Peppy");
// test
trace("**********");
for(var i in killer){ trace( i + ":\t" + killer[i] ) }
trace("**********");
for(var i in peppy){ trace( i + ":\t" + peppy[i] ) }
trace(peppy.name);
trace(killer.name);
That was just a start of course. Is that just a different way of doing it? or are they completey differnt thing.. I guess I'm mainly just wondering if that "structure" is even a good aproach at OOP, because that's the way I started to work on the fish thing. I had the intentions of using movieclips, and I guess underestimated how difficult it would be for me. So I guess thats where I am at right now.. That was my first stab at making the sub classes, so I'm kind of confused I suppose. If they are just different ways, would you feel the one you gave in the Bee example is the better way to do it? And yes, I was talking about the OOP creature.
senocular
September 13th, 2003, 02:49 PM
the __proto__ assignment was for Flash 5 and has been all but abolished in Flash MX. Standards suggest against it though its still possible and works if used. Now its suggested you use
SuperClass = function(){}
... SuperClass definitions
SubClass = function(){}
SubClass.prototype = new SuperClass();
... SubClass definitions
Theres a lot that goes through that in the OOP links I have listed in my best of. I think theres one lengthy explanation in a Flashkit post that really tears apart inheritance structure and how new SuperClass and __proto__ plays its part in it all. Something to sink your teeth into if you havent already :)
APDesign
September 13th, 2003, 03:01 PM
Ok, thanks for clearing that up.. Too bad I wasted tons of time learning an outdated way of doing it, heh. Ohhhhhhh well. Your best of has helped me a lot though!! You should write some books! Looks like I'll try re-learning it the right way!
~APD
senocular
September 13th, 2003, 03:05 PM
basically its just changing
Angel.prototype.__proto__ = Fish.prototype;
to
Angel.prototype = new Fish();
The one thing you have to keep aware of is doing that clears out the current Angel prototype so it needs to be defined immediately - before any proto definitions are defined. You were doing that anyway so its no big deal :beam:
senocular
September 13th, 2003, 03:06 PM
oh yeah, and theres super() now as of MX, that will precent the need for all that $base junk ;) - there are some pretty good links about that in that listing too
APDesign
September 13th, 2003, 03:35 PM
Excelent =D
Lifeform = function(){}
Fish = function(name){
this.name=name;
}
Fish.prototype= new Lifeform();
Shark = function( name ){
super(name);
}
Shark.prototype=new Fish();
Shark.prototype.fins =3;
Shark.prototype.eyes ="beady";
Shark.prototype.speed =5;
Angel = function( name ){
super(name);
}
Angel.prototype=new Fish();
Angel.prototype.fins =2;
Angel.prototype.eyes ="buggy";
Angel.prototype.speed =5;
killer = new Shark( "Killer" );
peppy = new Angel("Peppy");
// test
trace("**********");
for(var i in killer){ trace( i + ":\t" + killer[i] ) }
trace("**********");
for(var i in peppy){ trace( i + ":\t" + peppy[i] ) }
Correct use of Super right? Thanks a lot, you are the man!! Quick responses with awesome info.
senocular
September 13th, 2003, 03:54 PM
you got it ;)
You can also use super to call methods:
super.method(params);
and, if you're slick, nest supers
super.super.super.method(params);
http://proto.layer51.com/d.aspx?f=928
^ I think theres a good post in actionscript.org going through that and some inheritance with mortimer.
APDesign
September 13th, 2003, 06:13 PM
Nice! I never even thought about super.super(); or using a super.method();
Time to try my hand at making unique movie clips for each fish, and make em swim around with different properties.
Thanks!
senocular
September 13th, 2003, 06:16 PM
post if you need help or direction :)
APDesign
September 13th, 2003, 09:36 PM
::sigh:: Can't believe I am already asking for help... but for some reason (which I cant figure out) its setting the x and y for all of them the same.. even though from what I understand it SHOULD be running a different random() each time in the Fish function. So basically its putting them all on the stage at a random position, but they are ALL going to one single random position.
tracer=function(){
trace("**********");
for(var i in sharpy){ trace( i + ":\t" + sharpy[i] ) };
trace("**********");
for(var i in killer){ trace( i + ":\t" + killer[i] ) };
trace("**********");
for(var i in peppy){ trace( i + ":\t" + peppy[i] ) };
trace("**********");
for(var i in angelica){ trace( i + ":\t" + angelica[i] ) };
}
Lifeform = function(){}
Lifeform.prototype.buddy=function(){}
Fish = function(name,typer){
k++;
this.name=name;
this.instNum=k-3;
//this.instNum = ++this.constructor.count;
}
Fish.prototype= new Lifeform(name);
Fish.prototype.createFish=function(name,typer){
_root.createEmptyMovieClip(name,this.instNum);
_root.attachMovie(typer,name+type,this.instNum);
this.setPos(random(500),random(400));
}
Fish.prototype.setPos=function(x,y){
_y=x;
_x=y;
}
Shark = function( name ){
super(name);
super.createFish(name, "shark");
}
Shark.prototype=new Fish(name);
Shark.prototype.strength="Strong"
Shark.prototype.stamina ="Poor";
Shark.prototype.speed =8
Angel = function( name ){
super(name);
super.createFish(name, "angel");
}
Angel.prototype=new Fish(name);
Angel.prototype.strength="Weak"
Angel.prototype.stamina ="Good"
Angel.prototype.speed =5;
Gold = function( name ){
super(name);
super.createFish(name, "gold");
}
Gold.prototype=new Fish(name);
Gold.prototype.strength="Weak"
Gold.prototype.stamina ="weak"
Gold.prototype.speed =3;
///////////////////////////////////////////////////////////
goldy=new Gold("Goldy");
sharpy=new Shark("Sharpy");
killer = new Shark( "Killer" );
peppy = new Angel("Peppy");
angelica=new Angel("Angelica");
// trace
tracer();
I've tried calling the setPos(x,y); function from the Gold, Shark, and Angel functions seperately, I've tried passing the random variables while creating the new <insertherefishname> also.
I'll attach the .FLA too. Anyone have some ideas? (if you see something that looks "fishy" (heh) or is a bad/incorrect way to do things, I would also love to hear it .. referancing the _root to create the movie clips seemed kind of not good to me.. but maybe thats how you have to do it...:azn:
senocular
September 13th, 2003, 09:38 PM
use this.property
APDesign
September 13th, 2003, 09:45 PM
for the this._x and this._y? I've tried that and just tried again. same results :( You are like the forum God. The all knowing all seeing, always around guy.
senocular
September 13th, 2003, 09:46 PM
well so much for my 3 second skim the question answer. Gimme some time to actually look at it and Ill see what I can find :)
senocular
September 13th, 2003, 10:02 PM
alright, you got your classes set up pretty well but you have no association set with your movieclip objects. There are a couple of ways of doing this (which are layed out in the links in the best of ;)) one being the use of object.registerClass. This lets you make it so when a movieclip is attached, it automatically has an association, or really, is inherently an instance of a class which you specify. The problem with this is that making a class means not saying myInstance = new Class(), but rather just by using attachMovie where the created MovieClip instance is the instance of your class. With this, also, you would need to be sure to have the MovieClip class within your inheritence heirarchy (in your case with the LifeForm class or Lifeform.prototype = new MovieClip(); ).
Alternatively, which MIGHT be a better solution given your current setup, you can just have a property in your class to be the reference to the movieclip the class effects. With that, instead of saying something like this._x = value, youd use this._mc._x = value, where _mc represents the reference to the movieclip that instance controls. Note _mc is an arbitrary property name. I use _mc because it gives code hints ;)
and with that you get something like:
tracer=function(){
trace("**********");
for(var i in sharpy){ trace( i + ":\t" + sharpy[i] ) };
trace("**********");
for(var i in killer){ trace( i + ":\t" + killer[i] ) };
trace("**********");
for(var i in peppy){ trace( i + ":\t" + peppy[i] ) };
trace("**********");
for(var i in angelica){ trace( i + ":\t" + angelica[i] ) };
}
Lifeform = function(){}
Lifeform.prototype.buddy=function(){}
Fish = function(name,typer){
k++;
this.name=name;
this.instNum=k-3;
//this.instNum = ++this.constructor.count;
}
Fish.prototype= new Lifeform(name);
Fish.prototype.createFish=function(name,typer){
this._mc = _root.attachMovie(typer, name+typer, this.instNum);
this.setPos(random(500),random(400));
}
Fish.prototype.setPos=function(x,y){
this._mc._y=x;
this._mc._x=y;
}
Shark = function( name ){
super(name);
//this.instNum = ++this.constructor.count+1;
super.createFish(name, "shark");
}
Shark.prototype=new Fish(name);
Shark.prototype.strength="Strong"
Shark.prototype.stamina ="Poor";
Shark.prototype.speed =8
Angel = function( name ){
super(name);
//this.instNum = ++this.constructor.count;
super.createFish(name, "angel");
}
Angel.prototype=new Fish(name);
Angel.prototype.strength="Weak"
Angel.prototype.stamina ="Good"
Angel.prototype.speed =5;
Gold = function( name ){
super(name);
//this.instNum = ++this.constructor.count;
super.createFish(name, "gold");
}
Gold.prototype=new Fish(name);
Gold.prototype.strength="Weak"
Gold.prototype.stamina ="weak"
Gold.prototype.speed =3;
///////////////////////////////////////////////////////////
goldy=new Gold("Goldy");
sharpy=new Shark("Sharpy");
killer = new Shark( "Killer" );
peppy = new Angel("Peppy");
angelica=new Angel("Angelica");
// test
tracer();
APDesign
September 13th, 2003, 10:09 PM
ahhhh, I see. I was looking through the best of and came across a super hero example that talked about the alternative way of referancing the movie clip.. which is actually what I tried to base my code off of, along the way I must have just not realized why the _mc was relevant, but now I understand :beam: thanks again! you are the man! I'll have to check out the other way of doing it too, just for the knowledge. I noticed you used the Lifeform.prototype = new MovieClip(); in your version of the OOP Creature thing, but when I was looking at it I couldn't figure out how it worked. Now it is more clear. :azn:
senocular
September 13th, 2003, 10:14 PM
yeah its a little hidden in the creatures example, but in Environment.addLifeform, theres the Object.registerClass(ID,kind); line which associates a movieclip with a class. Then, instead of the class instance being created with the new keyword, the movieclip is simply attached and the movieclip is pushed on the lifeforms array. That movieclip then handles the events and the methods associated with the class ;)
APDesign
September 14th, 2003, 01:18 AM
Well, now that I'm getting into it I think I might as well just go for an all out enviornment type thing after all =D. I don't think it will be as in depth as yours with weather and all, but It'll be something to pass the time in school while I learn about the difference between raster images and vectors :sleep: *yawn*
grandsp5
September 14th, 2003, 02:37 AM
in C++, we had to make fish classes, environment classes, species classes, and get them to all interact and update a matrix holding the "ocean" so the fish would swim around in a matrix, breed with other fish if they touched, and avoid predators. God I hate fish now. I never want to see a fish again.
APDesign
September 14th, 2003, 04:24 AM
That grandsp5 is exactly what I intend to do! I don't know about the whole matrix thing though.. and I am of course doing it in flash and not C++. I think I will have the sharks reside in the lower water, and the fish will stick on top.. but you see.. the food is going to be plants on the bottom of the sea so I plan on having them know when to go to avoid the sharks.... I also am going to give them a "hide" property relative to their size and color, and maybe even throw in some coral down there that will boost it for them. And I always thought fish were boring pets!! I would like to make it look really nice and slicked over.. but I think I will have a hard time doing that =( I wonder if it can hold my attention long enough to actually complete it though. I figure anything I make now I can use for my portfolio later though, assuming it is good enough.. so thats some motivation to do this kind of stuff.
Wish me luck!
~APD
grandsp5
September 14th, 2003, 05:09 AM
Good luck man. Here is some encouragement.
APDesign
September 14th, 2003, 06:33 AM
Heh, nice fluid motion on the tweens :azn:
APDesign
September 16th, 2003, 03:42 AM
Here is a little update. I've got a lot of stuff up and running as far as the graphical part goes.. Have random movement but its probably just temporary until I make some better functions for it (I have a feeling when I start getting into it more I'll have to rewrite a lot of this, but hey.. its my first time =D) Although I am not actually 'stuck' on anything yet, I am kind of fearing how I'm going to make the "sonar" function (see FLA) which mainly is going to involve some hit testing between different classes which is a little intimidating. I'll give it my all though!! :thumb:
~APD
oh yeah, if you just want to take a look at the .swf then check this out. (http://home.wi.rr.com/therocket/enviorn.swf) That might be a tad outdated, but probably nothing visual. Hit F11 for full screen "aquarium" effect, hit F5 to refresh. Notice each species has its own depth in the water and speed =D. Those graphics are not final.. I plan on adding dynamic background stuff also, instead of just that lame "sand" at the bottom.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.