我試圖在我的android whack-a-mole遊戲中創建一個MVC模式。從模型傳遞數據通過控制器查看
我通過內部類的線程在模型中生成摩爾位置,並且最終要將它傳遞給視圖,以便它會爲它生成一個精靈。
如何爲我的視圖創建一種方式,以不斷從我的模型中接收生成的痣位置?
我編輯了下面的代碼來捕捉它們背後的要領。
MODEL:
public class GameModel{
public GameModel(){
spawner = new MoleSpawner();
spawner.start();
}
.
.
.
private class MoleSpawner extends Thread{
private int location;
public void run() {
location = new Random().nextInt(20);
try{
sleep (1000);
} catch (InterruptedException ex){
ex.printStackTrace();
}
}
}
}
VIEW:
public GameView{
.
.
.
public void createMoleSprite(int newlocation){
//create sprites here
//newlocation should come from the MODEL
//this method must be triggered everytime the MODEL creates a new location
}
}
我會盡量誠實和樂於助人 - 第一次嘗試MVC時,你將無法正確地遵循它。我知道我沒有。所以,不要太在意MVC - 專注於使其工作。一旦你開始工作,你會注意到你怎麼可能更多的MVC;但不要讓「我不遵循MVC」的想法成爲代碼中的阻止塊。 – Shark
@鯊魚,非常真實!我從MVC開始經歷過很多次,將一些模型代碼放在控制器中,一些放在模型中,一些放在視圖中,一旦我把所有東西都移到正確的位置加工。 –