We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Reaching data from objects w/o it's name
Page Index Toggle Pages: 1
Reaching data from objects w/o it's name (Read 1083 times)
Reaching data from objects w/o it's name
Aug 12th, 2009, 6:49am
 
Hello!

I've got a for me very complicated problem but I'll try to explain it as simple as possible:

I am making a small game where I got several different kind of similar objects. Things they all got is xpos, ypos, direction, velocity, size but they also got other things that they don't share with each other. They differ too much for making them all in the same class. There for I got alot of different classes but sometimes I have to be able to reach data from all different classes.

For example, lets call my classes different fruit names. If I want the "AI" (It's not very intelligent, haha) to control my "bananaObject" and later on, or another AI at the same time, control my "appleObject" I don't want to write an AI which asks "if (that=Banana){ do this } else if (that=Apple) { do that }" because that won't work when I have pears, oranges, grapes, pineapples, raspberries, plums and peaches and 25 other fruits.

I also want to be able to do any number of any fruit, and as I want all fruits to be able collide with each other I need to be able to call any fruit "anyFruit[number]" or something like that. Any ideas?

By the way, fruits is an image for what those classes really are, I'll put up an applet so you can see what I mean. And sorry for not giving you any code, it contains too many different parts to explain in a simple manner.

Thanks for reading!
h
Re: Reaching data from objects w/o it's name
Reply #1 - Aug 12th, 2009, 7:48am
 
It sounds like you need to start using inheritance in your class design. Inheritance allows you to define a basic class, but then design other classes that use that basic design, and then add slightly different twists on it. So let's say you start with your Apple class (continuing on with your fruit example). The Apple class has a general size to it. Each Apple has a stem. Each Apple will get a certain number of seeds, etc. But then, let's say you want both Washington Red apples, and Granny Smith apples. Well those two apples have different skin colors and different tastes. Everything about them is pretty much the same, except the skin color is very much different, and one is sweeter and one is more sour. So, for those classes, they will inherit the properties of the Apple class (because they are Apples after all), but they will also each add their own special colors. So we might define our classes like this:
Code:

class WashingtonRed extends Apple {
    ...
}

class GrannySmith extends Apple {
    ...
}


That's all you really need to do to build off of a previous class. Then,  when you build the constructors for your extended classes, you can assign the the variables of the parent class using super(). You should take a look at this entry in the reference to learn more about inheritance (the example is also located in your examples folder in processing itself).

One last thing. It sounds to me as if you are getting one idea for a class, but then getting some other cool ideas to make it better, and so you make another class. Instead of having a bunch of different classes or a bunch of classes that inherit from a parent class, think about redesigning your original class to simply be more complex. Using overloaded constructors is another option, where one class has a bunch of different options, but which constructor you use when creating the object determines it's various properties.
Re: Reaching data from objects w/o it's name
Reply #2 - Aug 12th, 2009, 7:54am
 
Edit: ^ pipped to the post  Angry

You start with a base class which all these properties share. Including a method for when they get collected and for when the game checks up on them.

Let's call it GameObject.

Now you can have many objects that are GameObjects, but ones that are functionally very different you create a subclass of.

Banana extends GameObject

And then you enter methods again where you want a different result to occur.

http://java.sun.com/docs/books/tutorial/java/concepts/inheritance.html

Of course, when you've got simply Fruit, and most of it acts pretty much the same apart from the animation, then you take a different tack.

In each Fruit you would have an id for what fruit it is and an Animation or some data that the animation routines in GameObject can deal with.

In the games I make I have a list of GameObjects all with similar properties. These then break up into respective families, depending on what I need to do with them. However, I am still able to iterate through the whole lot, considering them "GameObjects" as I go. If I need to treat them as their proper class, then I cast them to what they should be.
Re: Reaching data from objects w/o it's name
Reply #3 - Aug 12th, 2009, 1:37pm
 
Okey, this sounds exactly what I am looking for. I tried to use it yesterday but failed, but I'll give it another try. I'll read the reference.

Is this right:

objFruit is made out of super class Fruit.
objApple, objBanana, etc is made out of classes extending Fruit.

Where do I make objects such as apples and banana? Inside Fruit objects or anywhere? If I want to find out lets say objApples xpos do I find it by calling objFruit[id] where I know that that "id" is an apple?

Thanks alot for you help!

Here is the current state of my game:

http://www.hampusberndtson.com/gameapplet/

Use arrow keys to move around, fire with 'x' and speed up with 'z'. Notice that this requires energy but energy is recharging all the time. Use 1-8 for upgrading your ship, one upgrade / wave. Upgrades won't be given to you until next wave and if you forget to upgrade that upgrade will be lost. Use '0' to cheat and skip to next wave. At wave 5-6 or something there will appear warp-holes.

And remember that this is not finnished at all.-)

h

edit:
The applet wasn't working but now it should, as I made all print()s comments. I forgot to tell you what the different upgrades do: 1: Ship's hull, 2: Battery size, 3: Battery recharge speed, 4: Number of shots fired, 5: Firerate (no symbol yet), 6: Shot colour/damage, 7: Shot bounce, 8: Turret level (When you play multiplayer, when the game will support game pads, you become a turret at your friends ship as you die).
Re: Reaching data from objects w/o it's name
Reply #4 - Aug 12th, 2009, 2:07pm
 
Here's what I don't get:

What's the difference between the fruits? What do they do?

The effect they have on the game determines how they should be coded.
Re: Reaching data from objects w/o it's name
Reply #5 - Aug 12th, 2009, 2:20pm
 
Okey lets move on from fruits to space ships :ı

Different ships like different fruits will have different animations ofcourse, different speed, acceleration, gun levels, different special abilties etc. Different kinds of the same ship will have different upgrade levels, this is already working. I have an array[ship-id][upgrade-number] which upgrades the ship in the constructor.

edit:
I realised it's much more convenient to upgrade the ship with a function instead, so I can make it in-game, without rebuilding the ship.


I will also do totally different ships like "motherships" and "destroyers" or something like that with multiple turrets and repairbays and other modules mounted onto them. This will be for the more RTS-like part of the game.

For the multiplayer/wave-mode there will be alot of different enemy-fruits, like enemy bananas and such. I'm planning on making 4-6 different playable ships for the wave-mode.

Thanks for reading and helping.
h
Re: Reaching data from objects w/o it's name
Reply #6 - Aug 12th, 2009, 3:03pm
 
I see.

Okey dokey. Best way I can explain this is with a demo of how I set up a project.

In this game we have RedThings and BlueThings. They are extended from GameObject and override certain methods to behave differently. But they also can access the original methods of GameObject with the "super" keyword.

Code:

ArrayList objects;

void setup(){
size(400, 400);
smooth();
objects = new ArrayList();
for(int i = 0; i < 100; i++){
spawnObject();
}
}

void draw(){
background(0);
for(int i = 0; i < objects.size(); i++){
GameObject object = (GameObject)objects.get(i);
if(object.active){
object.main();
} else {
objects.remove(i);
i--;
}
}
}

void spawnObject(){
if(random(1) > 0.5){
objects.add(new RedThing(random(width), random(height), random(10, 20)));
} else {
objects.add(new BlueThing(random(width), random(height), random(10, 20)));
}
}

void mousePressed(){
spawnObject();
}

class RedThing extends GameObject{
RedThing(float x, float y, float radius){
super(x, y, radius);
}
void move(){
x += 2;
super.move();
}
void draw(){
fill(255,0,0);
super.draw();
}
}

class BlueThing extends GameObject{
BlueThing(float x, float y, float radius){
super(x, y, radius);
}
void move(){
y += 2;
super.move();
}
void draw(){
fill(0,0,255);
super.draw();
}
}

class GameObject{
float x, y, radius;
boolean active;
GameObject(float x, float y, float radius){
this.x = x;
this.y = y;
this.radius = radius;
active = true;
}
void main(){
move();
draw();
if(collision()){
active = false;
}
}
void move(){
if(x > width + radius) x = -radius;
if(x < -radius) x = width + radius;
if(y > height + radius) y = -radius;
if(y < -radius) y = height + radius;
}
void draw(){
ellipse(x, y, radius * 2, radius * 2);
}
boolean collision(){
return (mouseX - x) * (mouseX - x) + (mouseY - y) * (mouseY - y) <= radius * radius;
}
}
Re: Reaching data from objects w/o it's name
Reply #7 - Aug 12th, 2009, 3:53pm
 
This is awesome .-)
Thanks alot, I'll re-structure my game and return to you later.

Thanks again!
h
Re: Reaching data from objects w/o it's name
Reply #8 - Aug 13th, 2009, 10:17am
 
I've now restructured my game and it works fine. I love the array lists, teaching about them should be step one when teaching someone programming, haha.

I still got one more question about this (and I know I could try and find out the answer very fast, but I am too lazy):

Can I do sub classes inside sub classes?

Like this:
class Tree > class Fruit extends Tree > class Apple extends Fruit

Again, thanks for helping out St33d.
h
Re: Reaching data from objects w/o it's name
Reply #9 - Aug 14th, 2009, 4:14am
 
Yes, you can extend to your heart's content.

The super keyword will call only the immediate parent class. For two levels back you would need super.super and so on.

Technically a subclass isn't inside. It's more like an add on. It's better to think of it that way because you can have nested classes - which is how Processing makes it easy for beginners to code.

http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html

Try not to get carried away with the new toy though. The important thing is to keep the code easy to read where speed is not important and above all make it easy to maintain (meaning you can come back to it years later and not wonder what the hell is going on).
Page Index Toggle Pages: 1