java - 玩家丢失时如何启动 "new Game.class"?

标签 java android

我知道我的标题很困惑,但不知道如何以最容易理解的方式提出这个问题。希望你明白。

在我的游戏中有2个类

  1. ma​​inActivity”游戏 View 。
  2. Game”扩展了 SurfaceView 并实现了 Runnable(游戏循环)。

第一个屏幕显示“开始新游戏”按钮,一旦您单击该按钮,setContentView 将替换其显示 View game.class。

目前一切正常。现在,假设玩家输了,我想返回主屏幕,只要他点击“开始新游戏”,游戏就会从头开始。

当前点击返回键“从0重新开始游戏”

这是我的类(class):

主要 Activity :

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);
    }

}

请帮忙!!我已经一个半星期了,无法解决:((

最佳答案

我想当它到达 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);
}

}

关于java - 玩家丢失时如何启动 "new Game.class"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28822791/

相关文章:

java - 在 JTable 中将数字格式化为货币?

java - 使用 com.sun.net.httpsserver 的客户端证书身份验证

android - 将 getX() 和 getY() 中的值用于不同分辨率的设备(相对比例)

java - 一个 API 来处理添加和更新文件

android - 使用智能手机时,检测加载图像是否直接从相机拍摄

android - 如果要向Android Debounce添加条件,该怎么办?

java - Libgdx 2d 地板动画

java - 具有多个范围的托管 Bean

java - JSF - prependId 不工作?

java - HashMap/ArrayList 不会为每个键取多个值