Shakedown the issue to begin with is how to acsess
some_array[y_coord][x_coord];
the y_coord and x_coord so it would load into
draw(x,y)
I could never get it to work so I thought I was using the wrong data type.
Was I wrong thincking this?
If not how do I load y_coord to the y and the x_coord to the x?
Thanks for every ones replys it is helping me understand the problem better so I can find the answer or at least ask the better question.
What is the best way to store X and Y coords
I think you are confusing the indices of an array with the values stored in an array.
You may need to brush up on arrays: http://www.cplusplus.com/doc/tutorial/arrays.html
some_array[y_coord][x_coord] "points" to a specific value.
You don't get the x and y value from those indices - you access the data stored at that position in the array with them.
Say you use the structure I illustrated above:
You may need to brush up on arrays: http://www.cplusplus.com/doc/tutorial/arrays.html
some_array[y_coord][x_coord] "points" to a specific value.
int main(){ int some_array[2][2]; some_array[0][0] = 5; some_array[0][1] = 10; some_array[1][0] = 20; some_array[1][1] = 30; cout << some_array[1][0]; // prints '20'}
You don't get the x and y value from those indices - you access the data stored at that position in the array with them.
Say you use the structure I illustrated above:
struct Coord{ int x; int y;};int main(){ Coord pos[50]; // Object #0 pos[0].x = 5; pos[0].y = 5; // Object #1 pos[1].x = 10; pos[1].y = 10; // ... populate all of the positions however you wish ... // Draw all your objects at their position for ( int i = 0; i < 50; i++ ) { draw( pos.x, pos.y ); } return 0;}
Thanks CaspianB and Shakedown I took both your advice and came up with a way to store my x and y coords. next stop deleting the coords.
Just for testing purposes
TO load the coordinatesfor(int i=0;i<8; i++) { for(int l =0;l <12;l++ ) { int y_coord = i * 30; int x_coord = l * 50; pos[l].x = x_coord; pos.y = y_coord ; } }
Just for testing purposes
Reading the coordsfor(int i=0;i<8; i++) { for(int l = 0;l <12;l++ ) { cout<<pos.y<<" "<<pos[l].x<<endl; } }
while I tryd this to remove the coords that I didnt want
but it turns all elments that == x to 0 even if the elments dont == y
and vice versa
why would that be?
void remove_coord(int x, int y){ for(int i=0;i<8; i++) { for(int l =0;l <12;l++ ) { int y_coord = i * 30; int x_coord = l * 50; if((x_coord == x) && (y_coord == y)) { pos[l].x = 0; pos.y = 0; } } }}
but it turns all elments that == x to 0 even if the elments dont == y
and vice versa
why would that be?
You still don't seem to understand how arrays or structures work and I would hazard a guess you don't quite grasp how nested loops work either.
I strongly suggest you go back and look into C++ array tutorials in addition to looking up a tutorial on C++ structures.
For starters, the whole purpose of using a structure (at least in this particular situation here) is to have one single dimension array of coordinates. You won't use nested loops.
You are trying to store the x and y values at completely separate indices (pos[l].x and pos.y). Ideally, you would have a single loop (not nested loops) and do something like I outlined in the post above:
The point is to keep the x and y values together in the same array position.
I think, perhaps, you understand the basic concepts of arrays, but I'll try to make a short explanation anyway:
Example:
As I said, you may understand this concept. And I think you do. It's the mixing of structures in there that is throwing you off.
As I said, I very strongly suggest googling "C++ array tutorial" and "C++ structure tutorial" to get a stronger grip on these concepts.
I strongly suggest you go back and look into C++ array tutorials in addition to looking up a tutorial on C++ structures.
For starters, the whole purpose of using a structure (at least in this particular situation here) is to have one single dimension array of coordinates. You won't use nested loops.
You are trying to store the x and y values at completely separate indices (pos[l].x and pos.y). Ideally, you would have a single loop (not nested loops) and do something like I outlined in the post above:
for ( int i = 0; i < 50; i++ ){ pos.x = x_coord; pos.y = y_coord;}
The point is to keep the x and y values together in the same array position.
I think, perhaps, you understand the basic concepts of arrays, but I'll try to make a short explanation anyway:
Example:
// Say you want 10 variables of an integer type, all the sameint num0;int num1;int num2;... // etcint num9;num0 = 5;num1 = 5;...num9 = 5;// Or you can just use arraysint num[10];for ( int i = 0; i < 10; i++ ){ num = 5;}
As I said, you may understand this concept. And I think you do. It's the mixing of structures in there that is throwing you off.
struct Coordinate{ int x; int y;};int var; // This makes a SINGLE variable of type 'integer'var = 5;var = 10; // You can set it like thisCoordinate pos; // This makes a SINGLE variable of type Coordinatepos.x = 10; // Coordinate you defined above to hold two integers, x and ypos.y = 10; // You set them like thisCoordinate posArray[10]; // This makes an ARRAY variable of type CoordinateposArray[0].x // This accesses the array at position 0 for the x varposArray[0].y // and accessing y
As I said, I very strongly suggest googling "C++ array tutorial" and "C++ structure tutorial" to get a stronger grip on these concepts.
Do you understand what exactly you're doing? Because it looks like you're misunderstanding quite a few things.
Let's look at the loading code:
First of all, most of the time you're setting the coordinates of two different Coord structs! i is not the same as l, so pos[l] and pos do not refer to the same Coord instance. That's going to lead to quite some confusion later on (actually, it's already messing up your code). Just a note: avoid using i and l in the same scope, because they look too similar. It's easy to mess up with just a small type.
Second, you don't need the temporary x_coord and y_coord variabeles, because you can simply assign these values to pos.x and pos.y directly.
So, that piece of code would become:
Now onto 'removing' coordinates. I assume you want to reset the Coord instances that have the given x and y values? In that case, the following code would do the trick:
Let's look at the loading code:
// To load the coordinatesfor(int i = 0; i < 8; i++){ for(int l = 0; l < 12; l++) { int y_coord = i * 30; int x_coord = l * 50; pos[l].x = x_coord; pos.y = y_coord; }}
First of all, most of the time you're setting the coordinates of two different Coord structs! i is not the same as l, so pos[l] and pos do not refer to the same Coord instance. That's going to lead to quite some confusion later on (actually, it's already messing up your code). Just a note: avoid using i and l in the same scope, because they look too similar. It's easy to mess up with just a small type.
Second, you don't need the temporary x_coord and y_coord variabeles, because you can simply assign these values to pos.x and pos.y directly.
So, that piece of code would become:
for(int i = 0; i < 100; i++){ pos.x = i * 30; pos.y = i * 50;}
Now onto 'removing' coordinates. I assume you want to reset the Coord instances that have the given x and y values? In that case, the following code would do the trick:
void removeCoord(int x, int y){ for(int i = 0; i < 100; i++) { if(pos.x == x && pos.y == y) { pos.x = 0; pos.y = 0; } }}
I dont hinck I stated the problem correctly thne.
heres what I need
x = 50 y = 30
x = 100 y = 30
..
x = 550 y = 30
x = 50 y = 60
x = 100 y = 60
..
x=550 y = 60
x = 50 y = 90
ect
so the only way I can thinck of to have them corsponde like that is to use a nested loop for every y there needs to be 11 x's
like columes and rows
they are to repersent the x and y coords of the bricks for my game
there is 12 across and 8 down
so when I look for x = 550 and y = 60 and removes those two at the same time then the brick gets deleted
unless my logic is flawed some how. I tryed on for loop and could not get the above resultes when tested
heres what I need
x = 50 y = 30
x = 100 y = 30
..
x = 550 y = 30
x = 50 y = 60
x = 100 y = 60
..
x=550 y = 60
x = 50 y = 90
ect
so the only way I can thinck of to have them corsponde like that is to use a nested loop for every y there needs to be 11 x's
like columes and rows
they are to repersent the x and y coords of the bricks for my game
there is 12 across and 8 down
so when I look for x = 550 and y = 60 and removes those two at the same time then the brick gets deleted
unless my logic is flawed some how. I tryed on for loop and could not get the above resultes when tested
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement