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