2013-10-29 65 views
1

的我有這樣的(正常工作):紋理數組在Java中

public static Texture bubble1 = new Texture(Gdx.files.internal("Bubble1.png")); 
public static Texture bubble2 = new Texture(Gdx.files.internal("Bubble2.png")); 
public static Texture bubble3 = new Texture(Gdx.files.internal("Bubble3.png")); 
public static Texture bubble4 = new Texture(Gdx.files.internal("Bubble4.png")); 

,我嘗試它(不要以任何方式工作):

String fileName; 
Texture[] bubble; 
bubble = new Texture[4]; 

for (int i = 0; i < 4; i++){ 
    fileName = String.format("Bubble%1d", i+1); 
    bubble[i] = new Texture(Gdx.files.internal(fileName)); 
} 

有什麼不對?在維護代碼的簡單性和通用性的同時,我可以做些什麼來完成這項工作? 非常感謝!

回答

2

也許你忘了.png

for (int i = 0; i < 4; i++) 
{ 
    fileName = String.format("Bubble%1d.png", i+1); 
    bubble[i] = new Texture(Gdx.files.internal(fileName)); 
} 
+0

不是「Bubble%d.png」還是「Bubble%1 $ d.png」? –

+0

這取決於你的任務。 – Alexmelyon