שאלה ב java

userit8

New member
שאלה ב java

levels = new GameLevel[Const.LevelsAmount];
levels[0] = new GameLevel(Const.level0, screenWidth , screenHeight, this.giftArr , start_lives);
levels[1] = new GameLevel(Const.level1, screenWidth , screenHeight, this.giftArr , start_lives);
levels[2] = new GameLevel(Const.level2,screenWidth , screenHeight, this.giftArr , start_lives);


יש לי עוד המון שלבים להוסיף..איך ניתן לעשות את זה בצורה יעילה.. for קצת בעייתי כי מה אכתוב במקום Const.level0 למשל? הרי זה לא סטרינג..
 

nocgod

New member
אם const הוא enum

אתה יכול לקחת מערך של values של הenum הזה, לקחת i מאותחל ל0
ואז לעשות לולאת foreach על הvalues להשתמש בi כאינדקס במערך וכל הזמן להגדיל אותו...
 

userit8

New member
אה..שכחתי לציין

Const זו מחלקה המכילה מערכים דו מימדיים

Const.level0 זה מערך דו מימדי למשל..
 

nocgod

New member
זאת אומרת...

אם יש לך n שלבים אז יש לך n שדות בתוך המחלקה Const שנגישים מבחוץ?
ואם תרצה להוסיף שלב? מה לשנות את הקוד כולו?
תעשה ש const יכיל ArrayList<Level> או משהו כזה...
 

ihovav

New member
define a class for const

it can be template or normal, depand on your needs.
delegate all 2 dims array properties you need and use it as a class instead of using 2 dims array

this way you can enforce registration of this class to its parent and by that get a list of all such classes from the parent and traverse them...

in general, when you get into a situation where the simple array/external class requires you to do hula hoops, wrap it with your own class
 
למעלה