[java] Passing data to arrays at level change

Started by
7 comments, last by nowan 22 years, 6 months ago
When I have a level-up in a game I am working on, I need to pass new x and y coordinates to an array which holds this information for the positioning of objects. The array is declared like this at the beginning of the class: public static int pos_x[] = {210,310,40,190,95,246,13,115}; I''ve tried doing the following: pos_x[0]= 202; pos_x[1]= 250; etc... I have tested to see that the program recognises the new coordinates but doesn''t actually change the objects positions. Is ther a way to change all the data in the array in one statement rather than changing each one individually? I''m new to this so bear with me. Thanks
Advertisement
Hmmm...a quick way that comes to mind is something like this:

  public static int level_1_x_pos[] = {210,310,40,190,95,246,13,115};public static int level_2_x_pos[] = {202,250,70,130,1,296,63,185};public static int current_x_pos = level_1_x_pos;public static void levelUp (){  level = level + 1;  if (level == 2)  {    current_x_pos = level_2_x_pos;  }}  


This is a simplistic example but you get the idea. This will also be a lot faster because you only have to copy a single refrence instead of an entire array.
Could you elaborate on: "but doesn''t actually change the objects positions"?

"If consquences dictate our course of action, it doesn''t matter what''s right, it''s only wrong if you get caught."
- Tool

"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
Thanks WayfarerX but shouldn''t I be declaring current_x_pos as an array as well?
no, you should be declaring "current_x_pos" as a pointer to "int" (i.e. int *current_x_pos). an array in simplistic form is a constant pointer to whatever ever type the array is.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.
In java you cannot use: int* pos

so the correct way is this:
public static int current_x_pos[] = level_1_x_pos;

/Mankind gave birth to God.
/Mankind gave birth to God.
Yeah, my bad, typo. Good eye nowan.

"If consquences dictate our course of action, it doesn''t matter what''s right, it''s only wrong if you get caught."
- Tool

"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
Another way to copy an array or portion of an array:

System.arraycopy(Object src, int src_position,
Object dst, int dst_position, int length)

Where "src" is your source array and "dst" is your destination
array.

It probably won''t be as fast as defining your arrays at compile
time and changing your array reference to the next level but it
is a useful method in many cases.

Tyler.


Yeeeesssss! it works. Thanks everyone, I can now port between levels with all my objects happily re-positioning.
JOY!
Another way is:

pos_x = new int[]{202,250,70,130,1,296,63,185};

Again, this is not as fast as storing the data in initialised arrays but will use less memory. But I wouldn''t have thought that speed would be of huge importance when loading a level as it''s not done inside a loop 30 times a second.

- Kaijin

"If you find a job that you love you''ll never have to work again."

This topic is closed to new replies.

Advertisement