2013-09-06 3 views
0

임 간단한 게임을 libGDX로 작성하려하지만이 문제는 해당 게임을 만드는 모든 과정을 중지시킵니다. 두 클래스가 있습니다.배열에서 단일 문자열 반환

public class Question { 

private static float getFontX() { 
    return Assets.font.getBounds(Database.getText()).width/2; 
} 

private static float getFontY() { 
    return Assets.font.getBounds(Database.getText()).height/2; 
} 

public static void draw(SpriteBatch batch) { 

    Assets.font.draw(batch, Database.getText(), 
          TOFMain.SCREEN_WIDTH/2 - getFontX(), 
          getFontY() + 250 + TOFMain.SCREEN_HEIGHT/2); 
      //drawing some text from database on screen in the middle of screen; 

} 

두 번째 클래스는 질문

public class Database { 

private static String questions[] = new String[2]; 
{ 
    questions[0] = "Some question1"; 
    questions[1] = "Some question2"; 
} 

static public String getText() { 
    return questions[0]; 
} 
} 

return questions[0] 

에 문제가 포함되어 데이터베이스입니다 내가 예를

return "This will work"; 

모든 것을 거기에 작성하는 경우 때문에 괜찮아.

+0

libgdx와 관련이 있습니다 –

답변

1

초기화 블록을 정적 초기화 블록으로 변경해야합니다.

static { 
    questions[0] = "Some question1"; 
    questions[1] = "Some question2"; 
} 

이 같은 데이터베이스 클래스의 새 인스턴스를 생성하지 않는 경우 :

Database db = new Database(); 

동적 초기화 블록이 호출되지 않습니다를. 이것이 클래스와 함께 호출되는 정적 초기화 블록을 사용해야하는 이유입니다.

public class Database { 

private static String questions[] = new String[]{ 
    "Some question1", "Some question2" 
}; 


static public String getText() { 
    return questions[0]; 
} 

} 

그런 다음 당신이 원하는 String 반환

+1

고마워, 간단한 실수. 나는 그것을 알고있다 : P –

+0

@Evelan 당신을 진심으로 환영합니다. – Sajmon

1

당신은으로 Database 클래스의 배열을 선언 할 수 있습니다.

+0

그래,하지만 나에게있어 코드의 형식이 좀 더 읽기 쉽다. :) 이것이 오류의 원인이었다 : S –