Public Group

Archived

This topic is now archived and is closed to further replies.

how do i initialize an array if its part of a class?

This topic is 5195 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

high. im working on a pac-man clone, and im using 32x32 tiles. anyway, i have an array called map[19][25] which is an element for each tile to fill an 800x600 screen. right now i have it as a global variable, but i want to put it inside of one of my classes. the problem is, i want to assign values to it when i initialize it, IE, i want to do this
int map[19][25] =
{{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{ 1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1},
{ 1,0,1,1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,0,1},
{ 1,0,1,1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,0,1},
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,1},
{ 1,0,1,1,0,1,0,1,1,1,0,1,1,1,0,1,1,1,0,1,0,1,1,0,1},
{ 1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1},
{ 1,1,1,1,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,0,1,1,1,1},
{ 0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0},
{ 1,1,1,1,0,1,0,1,1,1,1,0,0,0,1,1,1,1,0,1,0,1,1,1,1},//10

{ 0,0,0,0,0,0,0,0,1,1,0,0,0,3,0,1,1,0,0,0,0,0,0,0,0},
{ 1,1,1,1,0,1,1,0,1,1,0,0,0,0,0,1,1,0,1,1,0,1,1,1,1},
{ 0,0,0,1,0,1,1,0,1,1,1,1,1,1,1,1,1,0,1,1,0,1,0,0,0},
{ 1,1,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,1},
{ 1,0,0,0,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,0,0,0,1},
{ 1,0,1,0,0,0,0,0,0,1,0,1,1,1,0,1,0,0,0,0,0,0,1,0,1},
{ 1,0,1,1,1,0,1,1,1,1,0,1,1,1,0,1,1,1,1,0,1,1,1,0,1},
{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};

i know how to use constructors, but how can i do something like map[what goes here?][what goes here?] = {{0,1,0,10} {more more more just as you saw above ^^^}} i just dont understand how i could assign values to every element in an array, AFTER it was initialized/declared. thanks for any help.

Share on other sites
int map[][];map[5][6] ={{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},{ 1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1},{ 1,0,1,1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,0,1},{ 1,0,1,1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,0,1},{ 1,0,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,1},{ 1,0,1,1,0,1,0,1,1,1,0,1,1,1,0,1,1,1,0,1,0,1,1,0,1},{ 1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1},{ 1,1,1,1,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,0,1,1,1,1},{ 0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0},{ 1,1,1,1,0,1,0,1,1,1,1,0,0,0,1,1,1,1,0,1,0,1,1,1,1},//10{ 0,0,0,0,0,0,0,0,1,1,0,0,0,3,0,1,1,0,0,0,0,0,0,0,0},{ 1,1,1,1,0,1,1,0,1,1,0,0,0,0,0,1,1,0,1,1,0,1,1,1,1},{ 0,0,0,1,0,1,1,0,1,1,1,1,1,1,1,1,1,0,1,1,0,1,0,0,0},{ 1,1,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,1},{ 1,0,0,0,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,1,0,0,0,0,1},{ 1,0,1,0,0,0,0,0,0,1,0,1,1,1,0,1,0,0,0,0,0,0,1,0,1},{ 1,0,1,1,1,0,1,1,1,1,0,1,1,1,0,1,1,1,1,0,1,1,1,0,1},{ 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};

Share on other sites
class whatever
{
int** map;
}

constructor(int map_width, int map_height)
{
map = new int[map_width][map_height];
}

and then methinks you can init in the same way

Or you can do the following.....
static int g_map[19][25] = your initial map

and keep a variable int** m_pmap in the class.
in the constructor m_pmap = g_map

[edited by - psamty10 on March 6, 2004 11:56:03 PM]

Share on other sites
You''re probably just filling the map array as quick way to see results, but you should really just write a map file loader and put it in a file, then you can call loadFile() in your constructor if you like. Its easy enough and you could even use a simple text file.

Ravyne, NYN Interactive Entertainment
[My Site][My School][My Group]

Share on other sites
Just write the values to a text file and read it in when your class constructor is called.

• 10
• 17
• 9
• 14
• 41
• Forum Statistics

• Total Topics
631070
• Total Posts
2997746
×