Android のもぐらたたきゲームで MVC パターンを作成しようとしています。
内部クラスのスレッドを介してモデル内にモグラの位置を生成していますが、最終的にはそれをビューに渡して、そのスプライトを生成したいと考えています。
ビューがモデルから生成されたモルの位置を継続的に受け取る方法を作成するにはどうすればよいですか?
以下のコードを編集して、それらの背後にある本質を捉えました。
モデル :
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();
}
}
}
}
見る:
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
}
}