2015-03-03 166 views
3

我知道我的标题很混乱,但不知道如何以最易懂的方式提出这个问题。希望你能理解。如何在玩家输掉时启动“新Game.class”?

在我的比赛有2类

  1. mainActivity” 游戏视图。
  2. 游戏”扩展了SurfaceView并实现了Runnable(游戏循环)。

第一个屏幕显示按钮“开始新游戏”,一旦你点击按钮setContentView替换它的显示视图game.class。

到目前为止一切工作正常。现在,假设玩家输了,我想回到主屏幕,一旦他点击“开始新游戏”游戏,就从头开始。

目前点击返回按钮 “重新从0游戏”

这里是我的课:

MainActivity:

public class MainActivity extends Activity implements OnTouchListener { 

    DialogPause pauseDialog; 
    public MyPreferences pref; 
    private TextView txCoins; 
    public Game game; 
    public static Music music; 
    ViewAnimator viewAnimator; 


    @SuppressLint("ClickableViewAccessibility") 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     pref = new MyPreferences(this); 
     music = new Music(this); 
     pauseDialog = new DialogPause(this); 

     setContentView(R.layout.menu); 

     view(); 
    } 

    public void view(){ 
     txCoins = (TextView)findViewById(R.id.txCoins); 
     txCoins.setText("Coins "+pref.getInt("coins")); 
    } 

    public void newGame(){ 
     if(game != null){ 
      game.destroyed(); 
     } 
     game = new Game(this); 
     game.start(); 
     game.setOnTouchListener(this); 
     setContentView(game); 
    } 

    public void click(View v){ 
      newGame(); 
    } 

    protected void onStart() { 
     super.onStart(); 
    } 

    @Override 
    protected void onStop() { 
     music.StopBackgroundMusic(); 
     super.onStop(); 
    } 

    @Override 
    public void onBackPressed() { 
     newGame(); 
    } 

游戏循环类:

public class Game extends SurfaceView implements Runnable{ 

    public static MyPreferences pref; 
    public static Levels level; 
    public static int SCREEN_W; 
    public static int SCREEN_H; 
    public static boolean running; 
    public static boolean pause = false; 
    private Thread thread = null; 
    private SurfaceHolder holder; 
    private Canvas canvas; 
    private Paint p = new Paint(); 
    public static Handler handler = new Handler(); 
    public static Resources res; 
    public Timer timer; 

    public Game(Context context) { 
     super(context); 
     res = getResources(); 
     holder = getHolder(); 
     level = new Levels(); 
     timer = new Timer(); 
     pref = new MyPreferences(context); 
     System.out.println("Create Game instance"); 
    } 

    public void run() { 
     init(); 

     long lastTime = System.nanoTime(); 
     double delta = 0; 
     double ns = 1000000000.0/30.0; 
     System.out.println("Run method" + running); 
     //Start looping... 
     while (running) { 
      //If it's not pause 

      long now = System.nanoTime(); 
      delta+=(now - lastTime)/ns; 
      lastTime = now; 

      if(!holder.getSurface().isValid()){continue;} 

      canvas = holder.lockCanvas(); 
      SCREEN_W = canvas.getWidth(); 
      SCREEN_H= canvas.getHeight(); 


      while (delta >-1) { 
       if(!pause)tick(); 
        delta--; 
      } 

      level.level1(); 
      render(canvas); 
      holder.unlockCanvasAndPost(canvas); 

      if(ControlPanel.PLAYER_POWER <=0){ 
       System.out.println("Loose!"); 
       ControlPanel.PLAYER_POWER = 100; 
       stop(); 
       start(); 
      } 
     } 
    } 

    private void render(Canvas c) { 
     handler.render(c); 
     textOnScreen(c); 
    } 

    public void tick(){ 
     handler.tick(); 
    } 

    public void init(){ 
     ControlPanel.COINS = pref.getInt("coins"); 
     MainActivity.music.backgroundMusic(); 
     handler.addSpaceShip(new Player(500, 500, 10, 10, handler, Sprite.getSprite(0))); 

//  handler.addKing(new King(Game.SCREEN_W/2, 100, 
//    40, 40, handler, Sprite.getSprite(18), 0)); 
    } 

    public void start() { 
     //If the tread are new one 
     if(thread==null){ 
     thread = new Thread(this); 
     thread.start();  
     timer.start(); 
     } 
     //if it not running 
     if(!running) { 
      pause = false; 
      running =true; 
     } 
    } 


    public void stop() { 
     pause = true; 
     running = false; 
     timer.stop(); 
     pref.putInt("coins", ControlPanel.COINS); 
    } 


    public void pause() { 
     pause = true; 
    } 

    public void destroyed() { 
     System.out.println("Stop"); 
     running = false; 
     this.stop(); 
    } 

    public void textOnScreen(Canvas c){ 
     p = new Paint(); 
     p.setColor(Color.YELLOW); 
     p.setTextSize(50); 
     c.drawText("POWER = " + ControlPanel.PLAYER_POWER, SCREEN_W/2 ,50 , p); 
     c.drawText("Time = " + timer.timeFormat(), 100 ,50 , p); 
     c.drawText("Coins = " + ControlPanel.COINS, 10 ,SCREEN_H - 10 , p); 
     p.setColor(Color.WHITE); 
     c.drawText("Bullets = " +ControlPanel.BULLET, SCREEN_W - 390,SCREEN_H - 10 , p); 
     p.setColor(Color.WHITE); 
     c.drawText(ControlPanel.Distance + " km ", SCREEN_W /2 - 200,SCREEN_H - 10 , p); 
    } 

} 

请帮忙!!我已经过了一周半的时间,并且无法解决它:((

+1

你能还包括你的布局?还有,你有没有任何理由你没有两个活动inst一个吗?那么,您的MainActivity会包含启动屏幕,而GameActivity会包含实际的游戏?最后,'System.out.println(“Loose!”);'应该是'System.out.println(“Lost!”);':) – 2015-03-03 02:41:11

回答

0

我想你想在返回System.out.println("Loose!");时想回到你的第一个菜单,因为你说它现在已经很好用了,我没有时间阅读所有的代码,但如果它是你想要的,那么你可以尝试这种解决方案我添加处理程序变量每班重新启动程序

public class MainActivity extends Activity implements OnTouchListener { 

DialogPause pauseDialog; 
public MyPreferences pref; 
private TextView txCoins; 
public Game game; 
public static Music music; 
ViewAnimator viewAnimator; 
Handler uiHandler = new Handler(){ 
    @override 
    public void handleMessage (Message msg){ 
     restart(); 
    } 
} 


@SuppressLint("ClickableViewAccessibility") 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    pref = new MyPreferences(this); 
    music = new Music(this); 
    pauseDialog = new DialogPause(this); 

    setContentView(R.layout.menu); 

    view(); 
} 


private void restart(){ 
    setContentView(R.layout.menu); 
    view(); 
} 

public void view(){ 
    txCoins = (TextView)findViewById(R.id.txCoins); 
    txCoins.setText("Coins "+pref.getInt("coins")); 
} 

public void newGame(){ 
    if(game != null){ 
     game.destroyed(); 
    } 
    game = new Game(this, handler); 
    game.start(); 
    game.setOnTouchListener(this); 
    setContentView(game); 
} 

public void click(View v){ 
     newGame(); 
} 

protected void onStart() { 
    super.onStart(); 
} 

@Override 
protected void onStop() { 
    music.StopBackgroundMusic(); 
    super.onStop(); 
} 

@Override 
public void onBackPressed() { 
    //newGame(); //comment out this 
} 

和游戏类:。

public class Game extends SurfaceView implements Runnable{ 

private Handler handler; 
public static MyPreferences pref; 
public static Levels level; 
public static int SCREEN_W; 
public static int SCREEN_H; 
public static boolean running; 
public static boolean pause = false; 
private Thread thread = null; 
private SurfaceHolder holder; 
private Canvas canvas; 
private Paint p = new Paint(); 
public static Handler handler = new Handler(); 
public static Resources res; 
public Timer timer; 

public Game(Context context, Handler handler) { 
    super(context); 
    this.handler = handler; 
    res = getResources(); 
    holder = getHolder(); 
    level = new Levels(); 
    timer = new Timer(); 
    pref = new MyPreferences(context); 
    System.out.println("Create Game instance"); 
} 

public void run() { 
    init(); 

    long lastTime = System.nanoTime(); 
    double delta = 0; 
    double ns = 1000000000.0/30.0; 
    System.out.println("Run method" + running); 
    //Start looping... 
    while (running) { 
     //If it's not pause 

     long now = System.nanoTime(); 
     delta+=(now - lastTime)/ns; 
     lastTime = now; 

     if(!holder.getSurface().isValid()){continue;} 

     canvas = holder.lockCanvas(); 
     SCREEN_W = canvas.getWidth(); 
     SCREEN_H= canvas.getHeight(); 


     while (delta >-1) { 
      if(!pause)tick(); 
       delta--; 
     } 

     level.level1(); 
     render(canvas); 
     holder.unlockCanvasAndPost(canvas); 

     if(ControlPanel.PLAYER_POWER <=0){ 
      System.out.println("Loose!"); 

      ControlPanel.PLAYER_POWER = 100; 
      stop(); 
      handler.sendEmptyMessage(0);//restart the game menu as u want 
      //start();//Comment out this 
     } 
    } 
} 

private void render(Canvas c) { 
    handler.render(c); 
    textOnScreen(c); 
} 

public void tick(){ 
    handler.tick(); 
} 

public void init(){ 
    ControlPanel.COINS = pref.getInt("coins"); 
    MainActivity.music.backgroundMusic(); 
    handler.addSpaceShip(new Player(500, 500, 10, 10, handler, Sprite.getSprite(0))); 

//  handler.addKing(new King(Game.SCREEN_W/2, 100, 
//    40, 40, handler, Sprite.getSprite(18), 0)); 
} 

public void start() { 
    //If the tread are new one 
    if(thread==null){ 
    thread = new Thread(this); 
    thread.start();  
    timer.start(); 
    } 
    //if it not running 
    if(!running) { 
     pause = false; 
     running =true; 
    } 
} 


public void stop() { 
    pause = true; 
    running = false; 
    timer.stop(); 
    pref.putInt("coins", ControlPanel.COINS); 
} 


public void pause() { 
    pause = true; 
} 

public void destroyed() { 
    System.out.println("Stop"); 
    running = false; 
    this.stop(); 
} 

public void textOnScreen(Canvas c){ 
    p = new Paint(); 
    p.setColor(Color.YELLOW); 
    p.setTextSize(50); 
    c.drawText("POWER = " + ControlPanel.PLAYER_POWER, SCREEN_W/2 ,50 , p); 
    c.drawText("Time = " + timer.timeFormat(), 100 ,50 , p); 
    c.drawText("Coins = " + ControlPanel.COINS, 10 ,SCREEN_H - 10 , p); 
    p.setColor(Color.WHITE); 
    c.drawText("Bullets = " +ControlPanel.BULLET, SCREEN_W - 390,SCREEN_H - 10 , p); 
    p.setColor(Color.WHITE); 
    c.drawText(ControlPanel.Distance + " km ", SCREEN_W /2 - 200,SCREEN_H - 10 , p); 
} 

}