PDA

View Full Version : Can someone help me understand OOP a tad more? *caution! Long post*



nikomax
April 25th, 2008, 10:35 PM
Firstly ::
I know there are plenty of tutorials that will show what I'm wanting to know.
However I'm finding it difficult to grasp concepts as the tutorials are not related to what I am doing and then is for me to make a similar outcome relevant to my project!

Ok here we go::

Please trust me when I say I have done A LOT of reading before starting to use AS3 and while liking OOP to cars/bicycles/toasters/buildings/etc I still dont have a clue on how to effectively use classes I make.

So at this point would I be right in saying a class is a blueprint that I can make objects out of. So I could make a generic class shape, and then make a subclass that extends shape into something more specific like a square, the square will inherit all the properties and functions of square... right? So then Time and effort is saved because the functions of shape are already in square now, so recoding is not a problem...

And if I am right with that crude example, could you guys help me with an example more relevant to what I'm doing. I don't feel that thinking about shapes is helping me understand fully.

Basically I have a bar that slides onto the screen when the mouse has rolled over it and slides of nearly all of the way when the mouse rolls off. I have two of bars one of the left side of the screen and one for the bottom.

Now both bars perform the same functions but in terms of the information they carry their size and the positions they move to the are different.

If I was to make a class called Infobars that contained all of the properties and functions to make an Infobar, could I then use subclasses to extend the Infobars into different sidebars, calling on the functions that were defined in Infobars, but editing the size and positions of the bars?

Now if I hadn't typed enough...(sorry about this =S ) Could I ask you lovely people to help me make example scripts that demonstrate what I have been talking about providing of course what I have been typing is right.

Here is how I think the Infobars could be created::



package {
import flash.display.Sprite;
import flash.display.Graphics;
import flash.events.MouseEvent;
import flash.events.Event;

public class Infobar extends Sprite {

private var _slideTo:Number;

public function Infobar() {

var makeRect:Sprite=new Sprite ;
var dC:Graphics=makeRect.graphics;
dC.lineStyle(3,0x000000,1);
dC.beginFill(0xff0000,1);
dC.drawRect(0,0,200,500);
dC.endFill();
addChild(makeRect);

this.addEventListener(MouseEvent.ROLL_OVER,slideIn ,false,0,true);
this.addEventListener(MouseEvent.ROLL_OUT,slideOut ,false,0,true);
}
private function slideIn(evt:Event) {
this.addEventListener(Event.ENTER_FRAME, smoothSlide, false, 0,true);
_slideTo = 350;

}
private function slideOut(evt:Event) {
this.addEventListener(Event.ENTER_FRAME, smoothSlide, false, 0,true);
_slideTo = 500;

}
private function smoothSlide(evt:Event) {
var _speed = 5;
if (this.x > _slideTo || this.x < _slideTo ) {
var thisX:Number = this.x;
this.x += (_slideTo-thisX)/_speed;
}
}
}
}// note that when this movie starts you have to move your mouse over the rectangle before it will go to its rightful place. *shrugs* I don't know why, this didn't happen before




So that is class I want to base my future sidebars on. How would the subclasses be
created and made different shapes to this example. ans would the functions work automatically? BTW in reality the sidebar shapes are ones that I had made and exported, i changed it for this script to make things easier

I have already got my side bars working I'm just using them as a platform to understanding classes a bit more..

I hope people understand what I'm asking and can help. And if you have made it down here Thanks for reading!

ajcates
April 26th, 2008, 01:12 AM
take this code


var makeRect:Sprite=new Sprite ;
var dC:Graphics=makeRect.graphics;
dC.lineStyle(3,0x000000,1);
dC.beginFill(0xff0000,1);
dC.drawRect(0,0,200,500);
dC.endFill();
addChild(makeRect);


Put it in a rectangle class, and when you make an infobar object call it like this.



var myRectangle:Rectangle = new Rectangle();
var myInfobar:InfoBar = new InfoBar(myRectangle);


You should design your info bar class to be able to take any shape, this way the code is reusable. The whole idea behind oop is to stop you from having to reinvent the wheel every time. If you look online there many classes, and if you start collecting them, you will be witting code a lot faster because it will stop you from doing tedious things.

TheCanadian
April 26th, 2008, 02:47 AM
To have them slide to different positions, you would pass values into functions when they are called. Seeing as how you want it somewhat automatic, you would probably want to pass the values into the constructor when an instance is created. Something like this:

package {
import flash.display.Sprite;
import flash.display.Graphics;
import flash.events.MouseEvent;
import flash.events.Event;

public class Infobar extends Sprite {

public var slideFrom:Number;
public var slideTo:Number;
private var slide:Number;

public function Infobar(from:Number, to:Number) {

this.x = this.slideFrom = from;
this.slideTo = to

var makeRect:Sprite=new Sprite ;
var dC:Graphics=makeRect.graphics;
dC.lineStyle(3,0x000000,1);
dC.beginFill(0xff0000,1);
dC.drawRect(0,0,200,500);
dC.endFill();
addChild(makeRect);

this.addEventListener(MouseEvent.ROLL_OVER,slideIn ,false,0,true);
this.addEventListener(MouseEvent.ROLL_OUT,slideOut ,false,0,true);
}
private function slideIn(evt:MouseEvent):void {
this.addEventListener(Event.ENTER_FRAME, smoothSlide, false, 0,true);
this.slide = slideFrom;

}
private function slideOut(evt:MouseEvent):coid {
this.addEventListener(Event.ENTER_FRAME, smoothSlide, false, 0,true);
this.slide = slideTo;

}
private function smoothSlide(evt:Event) {
this.x += (this.slide-this.x)/5;
}
}
}

And then to create an instance:

var inf:InfoBar = new InfoBar(350, 500);
That way you can reuse the class for multiple instance of InfoBar by passing in different values for it's positioning or whatever else you want.

It's a rough, untested example but hopefully you get the idea.

mechanicaltimi
April 26th, 2008, 04:17 AM
The book, "Thinking in C++" is a beautiful book for thinking in OOP terms.

Bruce wrote it.

mechanicaltimi

ultraky
April 26th, 2008, 10:53 AM
I'd first think if it's nessesary to build subclasses. I know you prob want to use this as an oppertunity to learn, but to learn good OOP practices, think larger.

Everything you just mentioned could easily be handled in one class. If you REALLY want to practice OOP in this case, think larger. First just build a basic button class. Think of what a button does or might do. For example




public class MyButton
{
rollOver function {}
rollOut function {}
click function {}
attachArt function {}
makeArt function {}
addEventListeners function{}

}


Then, in your subclass you alter these function accordingly, then you subclass those, and add even more specific functions.

Think of a car manufactuer. Mercedes has an "S-Class", "R-Class", "M-class"...
these are different body styles (models), but they then offer features and upgrades (A/C bigger engine, stick shift, radio). These upgraded cars are not offered as new models, but as upgrades to more generic ones.

Right now, using the mercedes example, you're making an "S-Class", S2-Class", "S3-Class", "R-Class", "R2-Class", "R3-Class", "M-Class", "M2-Class"....

So make a gerenic buttoon class, (like the one above) then make you're Infobar Class, which would extend your custom button class you build.

For example, in your code. You are adding event listeners for ROLL_OVER and ROLL_OUT. This should be done in your basic button class. This is because EVERY button will have a ROLL_OVER and ROLL_OUT event.


Get it?