What is the way to construct and use functions of the same class (many

I know it uses a 'for' statement. And the []'s with the in

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 913 Views - Last Post: 26 March 2009 - 09:27 AM Rate Topic: -----

#1 azjherben  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 89
  • Joined: 25-March 09

What is the way to construct and use functions of the same class (many

Post icon  Posted 25 March 2009 - 02:19 PM

I do have a program that I need this for that I've made. But, this is just a quick basic question...

I know it would be like this:
for(t = 0; t < 100; t++){
sands[t] = new sand();
sands[t].setpos(t,100);
sands[t].fall();
}



I want something kinda like that that will make a "object" 100 times then do the functions for the class.

Like I though that would make 100 peices of sand set their position to
x = 't' and y = 100
then make them all fall. And yes I have made the fall and setpos functions already and they do work for one at a time classes.

This post has been edited by azjherben: 25 March 2009 - 02:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: What is the way to construct and use functions of the same class (many

#2 krum110487  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 291
  • Joined: 07-February 09

Re: What is the way to construct and use functions of the same class (many

Posted 25 March 2009 - 02:28 PM

this is because the fall has to be completed before the loop will continue.

you will have to make fall change one "movement" at a time. Meaning instead of having the sand fall all at once you will have to make it fall one pixel at a time.

And within the "fall" you will need a loop that goes through each sand and makes it fall by one.

you will also have to take the "sands[t].fall();" out and place it within an infinite loop, or a loop that is exited on a condition that fall is false?

something along those lines.
Was This Post Helpful? 0
  • +
  • -

#3 azjherben  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 89
  • Joined: 25-March 09

Re: What is the way to construct and use functions of the same class (many

Posted 25 March 2009 - 02:31 PM

Where do I define the "sands" part. The rest I made.
Was This Post Helpful? 0
  • +
  • -

#4 malus  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 21
  • Joined: 25-March 09

Re: What is the way to construct and use functions of the same class (many

Posted 25 March 2009 - 02:44 PM

View Postazjherben, on 25 Mar, 2009 - 01:19 PM, said:

I do have a program that I need this for that I've made. But, this is just a quick basic question...

I know it would be like this:
for(t = 0; t < 100; t++){
sands[t] = new sand();
sands[t].setpos(t,100);
sands[t].fall();
}



I want something kinda like that that will make a "object" 100 times then do the functions for the class.

Like I though that would make 100 peices of sand set their position to
x = 't' and y = 100
then make them all fall. And yes I have made the fall and setpos functions already and they do work for one at a time classes.

I understand... but as you must understand yourself, this piece of code won't suffice to grant you the answer...
and i'm just a n00b :|
Was This Post Helpful? 0
  • +
  • -

#5 azjherben  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 89
  • Joined: 25-March 09

Re: What is the way to construct and use functions of the same class (many

Posted 25 March 2009 - 02:52 PM

/* */

//The headers
#include "sdlazjh.h"
//The surface
int Dott;
SDL_Surface *dot = NULL;
SDL_Surface *screen = NULL;

//The dot that will move around on the screen
class Dot
{
	private:
	int x, y;
	int xVel, yVel;
	public:
	Dot(int x,int y);
	void fall();
	void move();
	void show();
	void setxy(int xx,int yy);
};

Dot::Dot(int X,int Y)
{
	//Initialize the offsets
	x = X;
	y = Y;

	//Initialize the velocity
	xVel = 0;
	yVel = 0;
}

void Dot::setxy(int xx,int yy){
	 x = xx;
	 y = yy;
	 }

void Dot::fall()
{
	 if(y > 490){
		  y = 20;
		  }
	y += 5;
}

void Dot::move()
{
	//Move the dot left or right
	x += xVel;

	//If the dot went too far to the left or right
	if( ( x < 0 ) || ( x + 20 > 500 ) )
	{
		//move back
		x -= xVel;
	}

	//Move the dot up or down
	y += yVel;

	//If the dot went too far up or down
	if( ( y < 0 ) || ( y + 20 > 500 ) )
	{
		//move back
		y -= yVel;
	}
}

void Dot::show()
{
	//Show the dot
	apply_surface( x, y, dot, screen );
}

int main( int argc, char* args[] )
{
	int t = 0;
	//Quit flag
	bool quit = false;



screen = SDL_SetVideoMode(500,500,32, SDL_SWSURFACE );
dot = load_image( "dot.bmp" );

	//While the user hasn't quit
	while( quit == false )
	{
   
		while( SDL_PollEvent( &event ) )
		{
		
	//If a mouse button was pressed
	if( event.type == SDL_MOUSEBUTTONDOWN )
	{
		//If the left mouse button was pressed
		if( event.button.button == SDL_BUTTON_LEFT )
		{
t++;
Dott[t] = new Dot(10,0);
Dott[t].setxy(event.button.x,event.button.y);

		}

		}
			if( event.type == SDL_QUIT )
			{
				quit = true;
			}
		}


Dott[t].fall();		//Move the dot
Dott[t].move();


SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
Dot[t].show();
SDL_Flip(screen);
SDL_Delay(50);
}}


ENTIRE CODE!
OLNY ONE PROBLEM DETECTED!
That's the prob I need help with.

This post has been edited by azjherben: 25 March 2009 - 02:54 PM

Was This Post Helpful? 0
  • +
  • -

#6 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 38
  • View blog
  • Posts: 376
  • Joined: 01-December 08

Re: What is the way to construct and use functions of the same class (many

Posted 25 March 2009 - 02:53 PM

You just need to declare it as
sand *sands[100];
for(t = 0; t < 100; t++){
sands[t] = new sand();
sands[t]->setpos(t,100);
sands[t]->fall();
}


Edit: wrong operator

This post has been edited by ericr2427: 25 March 2009 - 02:55 PM

Was This Post Helpful? 0
  • +
  • -

#7 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: What is the way to construct and use functions of the same class (many

Posted 25 March 2009 - 02:54 PM

If I'm not mistaken, you're wanting to use an array of objects which is simple! :)
Here is an array of integers:
int MyIntegerArray[] = { 0x00, 0x01, 0x03, 0x04, 0x05 };

for (int x = 0; x < 5; x++) {
    printf("%d\n", MyIntegerArray[x]);
}


The same can be applied to objects (classes and structures):
struct MyStruct {
    int Value;
    int Value2;
};

MyStruct OBJECTS[15]; /* an array of x15 objects */

/* Assigns each object's attribute (Value & Value2) a value of 15 & 2 */
for (int x = 0; x < 15; x++) {
    OBJECTS[x].Value = 15;
    OBJECTS[x].Value2 = 2;
}


Welcome, hope that helps! :)
Was This Post Helpful? 0
  • +
  • -

#8 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: What is the way to construct and use functions of the same class (many

Posted 25 March 2009 - 02:58 PM

I think this would depend on the implementation of fall()

//First create the objects, then set their positions
for ( int i = 0; i < 100; i++ ) {
	sands[i] = new sand;
	sands[i].setpos( i, 100 );
}

//Now, make all sand fall...
while ( 1 ) { //Representation of the game main loop
	for ( int i = 0; i < 100; i++ ) { //Iterate through each instance
		sands[i].fall();
	}
}


I'm assuming that fall() moves the object one pixel down.

So, the previous code will.... iterate through the entire array of sand, moving each object down by 1 pixel via fall()

Repeat, via while loop

I hope this post was helpful.
Was This Post Helpful? 0
  • +
  • -

#9 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: What is the way to construct and use functions of the same class (many

Posted 25 March 2009 - 03:06 PM

View Postericr2427, on 25 Mar, 2009 - 01:53 PM, said:

You just need to declare it as
sand *sands[100];
for(t = 0; t < 100; t++){
sands[t] = new sand();
sands[t]->setpos(t,100);
sands[t]->fall();
}


Edit: wrong operator


Wouldn't that do the same thing as his first post?

-Create new "sand instance"
-Set "sand instance" initial position.
-Move "sand instance" 2 pixels down (based on his fall() implementation).
-repeat 99 more times.

So basically, following this peice of code, you might as well do this:
sands[t] = new sand();
sands[t]->setpos( t, 102 );


Was This Post Helpful? 0
  • +
  • -

#10 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: What is the way to construct and use functions of the same class (many

Posted 25 March 2009 - 03:09 PM

e_barroga, There is an edit button for a reason.
You don't have to erase what you'd put, just put what you posted at the bottom. :/

Makes life simple.

This post has been edited by Hyper: 25 March 2009 - 03:09 PM

Was This Post Helpful? 0
  • +
  • -

#11 ericr2427  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 38
  • View blog
  • Posts: 376
  • Joined: 01-December 08

Re: What is the way to construct and use functions of the same class (many

Posted 25 March 2009 - 03:09 PM

If I understand correctly, his question was how to declare the sands[] array in the first place.

This post has been edited by ericr2427: 25 March 2009 - 03:10 PM

Was This Post Helpful? 0
  • +
  • -

#12 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: What is the way to construct and use functions of the same class (many

Posted 25 March 2009 - 03:10 PM

View Postazjherben, on 25 Mar, 2009 - 04:19 PM, said:

I want something kinda like that that will make a "object" 100 times then do the functions for the class.


That's what his original and only question was.

This post has been edited by Hyper: 25 March 2009 - 03:10 PM

Was This Post Helpful? 0
  • +
  • -

#13 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: What is the way to construct and use functions of the same class (many

Posted 25 March 2009 - 03:17 PM

Oops, my bad.

You could also do it like this:
sand sands[100];


since C++ treats arrays and pointers similar.
Was This Post Helpful? 0
  • +
  • -

#14 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: What is the way to construct and use functions of the same class (many

Posted 25 March 2009 - 03:25 PM

If you scroll up, I already explained that. :)
Was This Post Helpful? 0
  • +
  • -

#15 krum110487  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 291
  • Joined: 07-February 09

Re: What is the way to construct and use functions of the same class (many

Posted 25 March 2009 - 03:43 PM

View Postericr2427, on 25 Mar, 2009 - 02:09 PM, said:

If I understand correctly, his question was how to declare the sands[] array in the first place.


not exactly,

"And yes I have made the fall and setpos functions already and they do work for one at a time classes."


it was more along the lines of, I declared it fine, but it's not moving correctly, which is because of how he implemented his fall.

at least, that is how I understood it, maybe I am reading into it too much :-P

This post has been edited by krum110487: 25 March 2009 - 03:44 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2