handling multiple objects from the same class

handling multiple objects from the same class

Postby fra9000 » 10 Aug 2011, 10:27

Hi FreeGameDev Team,

I'm new to C++ programming and I'd like to ask you how you usually manipulate objects from the same class..
to better explain myself..here it is an example:


[header]

{l Code}: {l Select All Code}
class Missile  {

private:

int initial_x_position;
int x_position;

public:

Missile(int init_x);
Move();

...
...};


Missile::Missile(int init_x) {

initial_x_position = init_x;

}


Move::Missile() {
   
   x_position++;

}


[main]

{l Code}: {l Select All Code}
...

Missile Miss1(10);
Missile Miss2(20);
Missile Miss3(30);

[b][end][/b]

so how could I move them together without calling them 1 by 1? like..

Miss1.move();
Miss2.move();
Miss3.move();



thank you in advance..
fra9000
 
Posts: 12
Joined: 12 May 2010, 18:52
Location: Melbourne - Australia

Re: handling multiple objects from the same class

Postby Knitter » 10 Aug 2011, 10:54

The most obvious (simple) way would be to put them in some data structure (array, list, map) and iterate that structure in a loop calling the method you want.
Knitter
 
Posts: 237
Joined: 03 Jul 2011, 22:52
Location: Portugal

Re: handling multiple objects from the same class

Postby charlie » 10 Aug 2011, 11:02

Try to use the tags like [code] - I added it in for you.
Free Gamer - it's the dogz
Vexi - web UI platform
User avatar
charlie
Global Moderator
 
Posts: 2131
Joined: 02 Dec 2009, 11:56
Location: Manchester, UK

Re: handling multiple objects from the same class

Postby fra9000 » 10 Aug 2011, 12:32

@ knitter: could you be more specific please, do you have some example of code? I mean can I iterate class's objects in array applying them a function? could you show me how to? I would really appreciate it..


@ charlie: oh ty ;)
fra9000
 
Posts: 12
Joined: 12 May 2010, 18:52
Location: Melbourne - Australia

Re: handling multiple objects from the same class

Postby Pix3l » 10 Aug 2011, 18:43

fra9000 {l Wrote}:@ knitter: could you be more specific please, do you have some example of code? I mean can I iterate class's objects in array applying them a function? could you show me how to? I would really appreciate it..


@ charlie: oh ty ;)


Hi pal, what you need is explained here: Java 2D game programming tutorial
Here he makes a simple space invaders clone, that involve the handling of multiple objects.
It use a list, but you can use an Array if you like :]
http://www.pix3lworkshop.altervista.org/ - Your 8bit choice since 2006!
User avatar
Pix3l
 
Posts: 55
Joined: 10 Sep 2010, 21:00
Location: Italy

Re: handling multiple objects from the same class

Postby rogerdv » 10 Aug 2011, 20:38

STL is your friend. I extensively use it in my game project.
User avatar
rogerdv
 
Posts: 289
Joined: 10 Dec 2009, 18:26

Re: handling multiple objects from the same class

Postby Knitter » 10 Aug 2011, 21:55

@ knitter: could you be more specific please, do you have some example of code? I mean can I iterate class's objects in array applying them a function? could you show me how to? I would really appreciate it..


I'm not really a C++ user but sure I can post something, but if you're having problems with this I really feel you need to start with more basic stuff like arrays, loops and such.

{l Code}: {l Select All Code}
Missile missiles[] = { Missile(10), Missile(20), Missile(30) };
//or in two steps, you can also add the elements later on
//Missile missiles[3];
//missiles[0] = Missile(10);
//missiles[1] = Missile(20);
//missiles[2] = Missile(30);

for(int i = 0; i < 3; i++) {
    missiles[i].move();
}


That is an example using arrays. In your code you shouldn't be using arrays, or at least not these type (fixed) of arrays and should be using other data structures like lists or maps or something else. Again, C++ is not my usual language so I may have missed the syntax there, also I would follow on the STL as rogerdv mentioned.
Knitter
 
Posts: 237
Joined: 03 Jul 2011, 22:52
Location: Portugal

Re: handling multiple objects from the same class

Postby fra9000 » 11 Aug 2011, 13:33

Thank you guys for the quick and complete replies..it was exactly what I needed ;)
fra9000
 
Posts: 12
Joined: 12 May 2010, 18:52
Location: Melbourne - Australia

Re: handling multiple objects from the same class

Postby fra9000 » 14 Aug 2011, 08:20

here I am..again :)

The method described above has worked for some basic iteration..but not in the following case:

target.h

{l Code}: {l Select All Code}
#include <string>


class Target {
   
   
   private:
   
   
   sf::Sprite sprite;
   
   float pos_x;
   float pos_y;
   
   
   public:

   
   Target(float x, float y, std::string n);
   
   void move_sprite(float t);
   sf::Sprite get_sprite();
   
};


Target::Target(float x, float y, std::string n) {
   
   
   static sf::Image img;
   if (!img.LoadFromFile(n)) {};
   
   sprite.SetImage(img);
        sprite.SetPosition(x, y);
   
   
}


void Target::move_sprite(float t) {
   
   
   sprite.Move(150.f*t, 0);


}


sf::Sprite Target::get_sprite() {
   
   
   return sprite;
   
   
}


// create targets
Target Target_1(   0.f, 240.f, "target.png");
Target Target_2( -60.f, 240.f, "target.png");
Target Target_3(-120.f, 240.f, "target.png");
Target Target_4(-180.f, 240.f, "target.png");
Target Target_5(-240.f, 240.f, "target.png");
Target Target_6(-300.f, 240.f, "target.png");
Target Target_7(-360.f, 240.f, "target.png");
Target Target_8(-420.f, 240.f, "target.png");


Target targets[8] = { Target_1, Target_2, Target_3, Target_4, Target_5, Target_6, Target_7, Target_8 };




main.cpp

{l Code}: {l Select All Code}

ElapsedTime = App.GetFrameTime();
   
// targets movement system [WIP]   
for(int i = 0; i < 9; i++) { targets[i].move_sprite(ElapsedTime); }




giving me a segmentation fault due to a stack overflow (I think)..


furthermore using:

{l Code}: {l Select All Code}
   Target_1.move_sprite(ElapsedTime);
   Target_2.move_sprite(ElapsedTime);
   Target_3.move_sprite(ElapsedTime);
   Target_4.move_sprite(ElapsedTime);
   Target_5.move_sprite(ElapsedTime);
   Target_6.move_sprite(ElapsedTime);
   Target_7.move_sprite(ElapsedTime);
   Target_8.move_sprite(ElapsedTime);


works perfectly..

I don't find a way to fix the problem..any advice? :(
fra9000
 
Posts: 12
Joined: 12 May 2010, 18:52
Location: Melbourne - Australia

Re: handling multiple objects from the same class

Postby oln » 14 Aug 2011, 08:56

Try:
main.cpp

{l Code}: {l Select All Code}

ElapsedTime = App.GetFrameTime();
   
// targets movement system [WIP]   
for(int i = 0; i < 7; i++) { targets[i].move_sprite(ElapsedTime); }


The array has a size of 8, and you were trying to access 10 elements.
Also, consider using std::vector rather than a basic array.
User avatar
oln
 
Posts: 1020
Joined: 26 Oct 2010, 22:16
Location: Norway

Re: handling multiple objects from the same class

Postby Knitter » 14 Aug 2011, 13:41

Not 7 nor 9, 8 :)

Arrays start at index 0, if you have na array with size 2, it will have position 0 and position 1. Since you created your array with size 8, it will have positions ranging from 0 to 7, thus your loop condition will have to be i < 8 or i <= 7.

When a C or C++ seg faults while using an array it's almost always caused by trying to access an invalid position.
Knitter
 
Posts: 237
Joined: 03 Jul 2011, 22:52
Location: Portugal

Re: handling multiple objects from the same class

Postby fra9000 » 15 Aug 2011, 12:09

oh..what a stupid error..ty again guys
fra9000
 
Posts: 12
Joined: 12 May 2010, 18:52
Location: Melbourne - Australia

Who is online

Users browsing this forum: No registered users and 1 guest

cron