0

I need to create a multi-player game on-line in Java and for Android platform. It looks like a western game, and this class should handle the duel between two or more players. When a user press the "BANG" button, the app should send a message to the server, but it doesn't even print the message "****BANG PRESSED****" Someone please help me, this is a project for my university

package com.example.root.gbu;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.SystemClock;
import android.widget.ImageView;

public class DuelFrame {

    private Context context;
    private long startTime;
    private long endTime;
    private boolean timeToShot=false;
    private ImageView image;
    private AlertDialog alertDialog;

    public DuelFrame(String enemy, Context context){
        this.context = context;
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Duel against " + enemy);
        alertDialog.setButton(Dialog.BUTTON_POSITIVE,"OK",new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which) {
                System.out.println("***BANG PRESSED****");
                if(timeToShot) {
                    endTime = SystemClock.elapsedRealtimeNanos();
                    ConnectionStream.getOut().println("TIME:" + String.valueOf(endTime - startTime));
                }
                else
                    ConnectionStream.getOut().println("TIME:LOSE");
            }
        });
        image = new ImageView(context);
        image.setImageResource(R.drawable.duel_start);
        alertDialog.setView(image);
    }

    public void start(){
        ((Activity)context).runOnUiThread(new Runnable(){//mostro la finestra
            @Override
            public void run(){
                alertDialog.show();
            }
        });
        new java.util.Timer().schedule(//timer per cambiare l'immagine
                new java.util.TimerTask() {
                    @Override
                    public void run() {
                        ((Activity)context).runOnUiThread(new Runnable(){
                            @Override
                            public void run(){
                                image.getLayoutParams().height = image.getHeight();
                                image.getLayoutParams().width = image.getWidth();
                                image.setImageResource(R.drawable.bang);
                                startTime = SystemClock.elapsedRealtimeNanos();
                                timeToShot = true;
                            }
                        });
                    }
                }, 3000);
    }

    public void cancel(){
        ((Activity)context).runOnUiThread(new Runnable(){
            @Override
            public void run(){
                //alertDialog.dismiss();
                //alertDialog.cancel();
                alertDialog.hide();
            }
        });
    }
}

The class that calls DuelFrame looks like

public class GameActivity extends AppCompatActivity {

    private DuelFrame duelFrame;
    ....

    public void startDuel(final String enemy){
        duelFrame = new DuelFrame(enemy, this);
        duelFrame.start();
    }

    public void endDuel(){
        if(duelFrame!=null) {
            duelFrame.cancel();
            duelFrame = null;
        }
    }
    ....
}

And there is a class called Listener which is a thread that execute server's commands in this way

for (messages = ConnectionStream.getIn().readLine().split(":");
             !messages[0].equals("ENDGAME"); messages = (debug = ConnectionStream.getIn().readLine()).split(":")) {
            System.out.println(i++ + ": " + debug);
            switch (messages[0]) {//scelgo cosa fare a seconda del messaggio
                case "ADD"://ADD:NOMEGIOCATORE:NODODIPARTENZA:WHITE/BLACK
                    game.login(messages[1], messages[3].equals("white"));
                    game.movePlayer(game.getPlayer(messages[1]), messages[2]);
                    break;
                case "MOVE"://MOVE:GIOCATORE:NODO
                    game.movePlayer(game.getPlayer(messages[1]), messages[2]);
                    break;
                case "REMOVE"://REMOVE:GIOCATORE
                    if(messages[1].equals(MapAdapter.getUsername()))
                        throw new Exception("You didn't move for too much time!");
                    game.logout(game.getPlayer(messages[1]));
                    break;
                case "DUEL"://DUEL:GIOCATORENEMICO
                    game.startDuel(messages[1]);
                    break;
                case "ENDDUEL":
                    game.endDuel();
                    break;
                case "SETBULLETS"://SETBULLETS:INT
                    game.setBullets(Integer.parseInt(messages[1]));
                    break;
                case "ADDBULLETS"://ADDBULLETS:INT
                    game.addBullets(Integer.parseInt(messages[1]));
                    break;
                default:
                    throw new Exception("Server sent weird command: " + messages[0]);
}
Stefano Berti
  • 141
  • 1
  • 11
  • 2
    Where do you call `DuelFrame`? It could be an issue to do with the `Context` Your'e sending. Do you get any error mesages? – jampez77 Aug 29 '17 at 14:21
  • Did you try Log.d instead of System.out.println? https://stackoverflow.com/questions/2220547/why-doesnt-system-out-println-work-in-android – Michael Troger Aug 29 '17 at 14:27
  • Try using Log statements instead of println. It probably is firing. – Brian Aug 29 '17 at 14:28
  • Thanks everyone for answering, I just edited my question with more information and now I gonna try using Log.d – Stefano Berti Aug 29 '17 at 14:29
  • Also be aware that you should post minimal code to reproduce the issue. If your algorithm doesn't work you can consider this as a new question – Michael Troger Aug 29 '17 at 14:31
  • @MichaelTroger I am using Log.e to display errors, and "***BANG PRESSED****" still doesn't appear while outside that function Log.e works. It could be an issue due to the context like jampez77 said? – Stefano Berti Aug 29 '17 at 14:50
  • Please don't repeat questions. Simply editing your original post with any new information you have, any new code you've tried, or an explanation of why any posted answers aren't working will bump it to the top of the active queue. – Mike M. Aug 29 '17 at 18:16

0 Answers0