Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Bullets are not showing up across network client screen in game

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Bullets are not showing up across network client screen in game

    Hi all so I a tank war game thats ran over a network and is multiplayer, however whenever I fire a bullet it shows on my screen but not the other player's screen. Can you help, here's the code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.util.*;
    import java.net.*;
    import java.io.*;
     
    /*
     * Implement a simple 2-player
     * tank battle game.
     *
     
     */
     
    public class WarPanel extends JPanel implements Runnable {
     
      Thread anim = null;  // animation thread
     
      public static ArrayList<Rock> rocks; // obstacles on the field
      public static ArrayList<Tank> tanks;
      public static final int RED = 0;
      public static final int BLUE = 1;
     
      int playerID = -1; // my subscript in tank array
     
      Image offscreen = null;
      Graphics offgr;
     
      Image redtank;
      Image bluetank;
     
      public static final int PWIDTH = 800;
      public static final int PHEIGHT = 600;
     
      static boolean roundOver = true;
      static int loser;
     
      static boolean ready = false;
     
      Font font = new Font("Monospaced",Font.BOLD,30);
     
      private Socket sock;
      private static PrintWriter out;
     
    	private static final int PORT = 1234;     // server details
    	private static final String HOST = "localhost";
     
     
      public WarPanel() {
    	super();
    	setPreferredSize(new Dimension(PWIDTH, PHEIGHT));
     
    	redtank = new ImageIcon("redtank.png").getImage();
    	bluetank = new ImageIcon("bluetank.png").getImage();
    	addKeyListener(new KeyL());
    	addMouseListener(new MseL());
     
    	makeContact();
      }
     
        private void makeContact()
        // contact the NetWarServer
        {
          try {
            sock = new Socket(HOST, PORT);
            BufferedReader in  = new BufferedReader(
      		  		new InputStreamReader( sock.getInputStream() ) );
            out = new PrintWriter( sock.getOutputStream(), true );  // autoflush
     
            new NetWarWatcher(this, in).start();    // start watching for server msgs
          }
          catch(Exception e)
          {  // System.out.println(e);
             System.out.println("Cannot contact the NetTankWar Server");
             System.exit(0);
          }
        }  // end of makeContact()
     
        public static void send(String msg){
    		// send a message to the other player (via server)
    		out.println(msg);
    	}
     
      void resetRound(){
        // Build semi-random rock field
    	rocks = new ArrayList<Rock>();
    	int edge = (PWIDTH + PHEIGHT)/20;
    	int halfW = PWIDTH/2;
    	int halfH = PHEIGHT/2;
    	placeRocks(40, edge, halfH, halfW, edge, 0.2);
    	placeRocks(40, halfW, PHEIGHT-edge, PWIDTH-edge, halfH, 0.2);
    	placeRocks(10, halfW, 0, halfW, PHEIGHT, 0.1);
     
    	// Place tanks
    	tanks = new ArrayList<Tank>();
    	tanks.add(new Tank(PWIDTH-edge, PHEIGHT-edge, Math.PI, RED, redtank));
      	tanks.add(new Tank(edge, edge, 0.0, BLUE, bluetank));
     
      	roundOver = false;
      	ready = true;
      }
     
      public void addNotify() {
    	super.addNotify();
     
        offscreen = createImage(PWIDTH, PHEIGHT);
        offgr = offscreen.getGraphics();
     
      	anim = new Thread(this);
      	anim.start();
      }
     
      public void setPlayerID(int id){
    	  playerID = id;
      }
     
      public int getPlayerID(){
    	  return playerID;
      }
     
      public void setRocks(String config){
    	  // Read rock string from other player
    	  stringToRocks(config);
    	  // Place tanks
    	  int edge = (PWIDTH + PHEIGHT)/20;
    	  tanks = new ArrayList<Tank>();
    	  tanks.add(new Tank(PWIDTH-edge, PHEIGHT-edge, Math.PI, RED, redtank));
    	  tanks.add(new Tank(edge, edge, 0.0, BLUE, bluetank));
     
      	  roundOver = false;
      	  ready = true;
      }
     
      public void sendRocks(){
    	  // Lay out rocks and send to other player
    	  resetRound();
    	  out.println("rocks "+rocksToString());
      }
     
      void placeRocks(int n, int x1, int y1, int x2, int y2, double aspect) {
    	  // place n rocks randomly located within distance r of
    	  // the line from (x1,y1) to (x2,y2) where r is the length of
    	  // this line times aspect.
    	  int x,y;
    	  double s, tx, ty;
     
    	  double len = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
    	  double r = len*aspect;
    	  for (int i = 0; i < n; i++){
    		  s = Rock.rockGen.nextDouble();
    		  tx = r * (Rock.rockGen.nextDouble() - 0.5);
    		  ty = r * (Rock.rockGen.nextDouble() - 0.5);
    		  x = (int)(x1 + s*(x2-x1) + tx);
    		  y = (int)(y1 + s*(y2-y1) + ty);
    		  rocks.add(new Rock(x, y, (int)(r/2), (int)r));
    	  }
     
      }
     
      public static void removeRock(int index){
    	  // Deactivate the index-th rock in rocks
    	  rocks.get(index).demolish();
      }
     
      public static String rocksToString(){
    	  // Return a string describing the rocks: x1 y1 d1;x2 y2 d2; ...
    	  StringBuilder sb = new StringBuilder();
    	  for (Rock r: rocks)
    		  sb.append(r.getIntX()+" "+r.getIntY()+" "+r.getDiameter()+";");
    	  return new String(sb);
      }
     
      public static void stringToRocks(String s){
    	  // read the rocks string and fill in the ArrayList
    	  int x,y,d;
     
    	  rocks = new ArrayList<Rock>();
    	  String specs[] = s.split(";");
    	  for (String sp: specs){
    		Scanner sc = new Scanner(sp);
    		x = sc.nextInt();
    		y = sc.nextInt();
    		d = sc.nextInt();
    		sc.close();
    		rocks.add(new Rock(x,y,d));
    	  }
      }
     
      public void processMove(String s){
    	  // Send command to tank not controlled by
    	  // this player
    	  tanks.get(1 - playerID).processMove(s);
      }
     
     
      public static	int hitAnItem(Ball b, ArrayList<? extends Ball> c){
    	  // Check if b has run into any element of c.  If so
    	  // return the index of the first item that was hit,
    	  // or -1 if nothing hit.
    	  // Use generics so the same code can check for collisions
    	  // with rocks and tanks.
      		for (Ball r: c){
      			if (!r.isAlive())
      				continue;
      			double dx = b.getX() - r.getX();
      			double dy = b.getY() - r.getY();
      			double bound = b.getRadius() + r.getRadius();
      			if ((dx*dx + dy*dy) < (bound*bound))
      				return c.indexOf(r);
      		}
      		return -1;
    	}
     
    	public static void tankHit(int k){
    		// If a tank is hit, the round is over
    		if (!roundOver){
    			roundOver = true;
    			loser = k;
    		}
    	}
     
      public void paintComponent(Graphics g)
      {
      	g.drawImage(offscreen,0,0,null);
      }
     
      public void frameRender(Graphics g) {
     
        // Draw a background
        g.setColor(Color.yellow);
        g.fillRect(0,0,PWIDTH,PHEIGHT);
     
      	  if (!ready){ // Just display the message and return
    		g.setColor(Color.black);
    		g.setFont(font);
    		g.drawString("Waiting for setup...",200, 250);
    		return;
    	  }
     
        // Draw rocks
        for (Rock r: rocks)
          r.paint(g);
     
        // Draw tanks (and their bullets)
        for (Tank t: tanks)
          t.paint(g);
     
          if (roundOver){
    		  g.setColor(Color.black);
    		  g.setFont(font);
          	g.drawString("Round Over: "+(loser==RED?"Blue":"Red")+" tank wins!",150,200);
          	g.drawString("Click mouse to start next round",150, 250);
          }
      }
     
      public void run() {
     
        while (anim != null)
        {
    		if (!roundOver){ // Freeze the action between rounds
    		  tanks.get(playerID).update(true);
    		  tanks.get(1 - playerID).update(false);
    		}
          frameRender(offgr);
          repaint();
          try {
          	Thread.sleep(40);
          } catch (InterruptedException e) {}
        }
      }
     
      public void stop() {
        anim = null;  // stop animation thread
      }
     
      class KeyL extends KeyAdapter {
    	  public void keyPressed(KeyEvent e){
    		  int c = e.getKeyCode();
    		  switch(c){
    			  case KeyEvent.VK_LEFT: tanks.get(playerID).turnL = true;
    										break;
    			  case KeyEvent.VK_RIGHT: tanks.get(playerID).turnR = true;
    								break;
    			  case KeyEvent.VK_UP: tanks.get(playerID).forth = true;
    								break;
    			  case KeyEvent.VK_SPACE: tanks.get(playerID).fire = true;
    					break;
    		  }
    	  }
     
    	  public void keyReleased(KeyEvent e){
    		  int c = e.getKeyCode();
    		  switch(c){
    			  case KeyEvent.VK_LEFT: tanks.get(playerID).turnL = false;
    								break;
    			  case KeyEvent.VK_RIGHT: tanks.get(playerID).turnR = false;
    								break;
    			  case KeyEvent.VK_UP: tanks.get(playerID).forth = false;
    								break;
    			  case KeyEvent.VK_SPACE: tanks.get(playerID).fire = false;
    								break;
    		  }
    	  }
      }
     
      class MseL extends MouseAdapter {
    	  public void mousePressed(MouseEvent e){
    		  requestFocus();
    	  }
      }
     
    }
     
    interface Ball {
    	// Description of common features for Rock, Tank, and Bullet
    	// Useful for WarPanel.hitAnItem()
    	double getX();
    	double getY();
    	double getRadius();
    	boolean isAlive();
    }
     
    class Rock implements Ball{
    	// Obstacles scattered about the play field.
    	// Create a random generator to use for rock sizes and
    	// placement
    	public static Random rockGen = new Random();
     
    	int locX, locY, diameter, radius;	// int properties easier to
    	boolean alive = true;				// send over network
     
    	public Rock(int x, int y, int minD, int maxD) {
    		locX = x;
    		locY = y;
    		diameter = minD + rockGen.nextInt(maxD-minD+1);
    		radius = diameter/2;
    	}
     
    	public Rock(int x, int y, int d){
    		locX = x;
    		locY = y;
    		diameter = d;
    		radius = diameter/2;
    	}
     
    	void demolish() {
    		// "Turn off" this rock - won't paint or be hit
    		alive = false;
    	}
     
    	public boolean isAlive() { return alive; }
     
    	public double getX(){ return (double)locX; }
    	public double getY(){ return (double)locY; }
    	public double getRadius() { return (double)radius; }
     
    	public int getIntX(){ return locX; }
    	public int getIntY(){ return locY; }
    	public int getDiameter(){ return diameter; }
     
    	void paint(Graphics g){
    		if (alive) {
    			g.setColor(Color.gray);
    			g.fillOval(locX - radius, locY - radius, diameter, diameter);
    		}
    	}
    }
     
    class Tank implements Ball {
     
    	double locX, locY, radius, angle;
    	int self; // index of this tank in WarPanel.tanks
    	public boolean turnL, turnR, forth, back, fire;
    	boolean prevtL, prevtR, prevfo;
    	Color color;
    	Image image;
     
        public static final double twoPi = Math.PI * 2.0;
        public static final double turnRate = Math.PI / 8;
        public static final double speed = 4.0;
        public static final int RELOAD = 8; // delay between bullets
        int count; // timer for reloading
     
    	public static final int MAXBULLETS = 7; // max simultaneous shots
    	Bullet bullets[] = new Bullet[MAXBULLETS];
     
    	AffineTransform saveAT; // place to hold current affine transform
     
    	public Tank(double x, double y, double a, int index, Image im){
    		locX = x;
    		locY = y;
    		angle = a;
    		self = index;
    		image = im;
    		radius = 22;
    		// create bullets for this tank
    		for (int i = 0; i < bullets.length; i++)
    			bullets[i] = new Bullet(self);
    	}
     
    	public double getX() { return locX; }
    	public double getY() { return locY; }
    	public double getRadius() { return radius; }
    	public boolean isAlive() { return true; }
     
    	void update(Boolean local) {
    		if (turnL)
    			turnLeft(turnRate);
    		if (turnR)
    			turnRight(turnRate);
    		if (forth){
    			moveForward();
    			// Check for rocks
    			if (WarPanel.hitAnItem(this, WarPanel.rocks) >= 0)
    				backUp();
    		}
    		if (local){
    			if (turnL != prevtL){
    			  WarPanel.send("turnL "+turnL+" "+locX+" "+locY+" "+angle);
    			  prevtL = turnL;
    			}
    			if (turnR != prevtR){
    			  WarPanel.send("turnR "+turnR+" "+locX+" "+locY+" "+angle);
    			  prevtR = turnR;
    			}
    			if (forth != prevfo){
    			  WarPanel.send("forth "+forth+" "+locX+" "+locY+" "+angle);
    			  prevfo = forth;
    			}
    		}
    		if (fire){
    			fireBullet();
    		}
    		// Update all of our bullets
    		for (Bullet b: bullets)
    			b.update();
    	}
     
    	public void processMove(String s){
    		// Update movement parameters based on s
    		Scanner sc = new Scanner(s);
    		// Get the flag change
    		String command = sc.next();
    		boolean value = sc.nextBoolean();
    		if (command.equals("turnL"))
    			turnL = value;
    		else if (command.equals("turnR"))
    			turnR = value;
    		else if (command.equals("forth"))
    			forth = value;
    		else
    			System.out.println("Unexpected move: "+command);
    		// then unpack position update
    		locX = sc.nextDouble();
    		locY = sc.nextDouble();
    		angle = sc.nextDouble();
    	}
     
    	void paint(Graphics g){
    		// Use the affine transform feature in Graphics2D
    		// to easily rotate the tank's image.
    		Graphics2D g2 = (Graphics2D)g;
    		saveAT = g2.getTransform();
    		g2.translate(locX, locY);
    		g2.rotate(angle);
    		g2.drawImage(image, (int)(-radius), (int)(-radius), null);
    		// Reset the transform (this is important)
    		g2.setTransform(saveAT);
    		// Then draw bullets
    		for (Bullet b: bullets)
    			b.paint(g2);
     
    	}
     
    	void fireBullet(){
    		// If it has been long enough since the last shot...
    		count--;
    		if (count > 0) return;
    		// ...and if all the bullets aren't currently in use...
    		int slot = getAvailableBullet();
    		if (slot < 0)
    			return;
    		// ...then launch a new bullet
    		bullets[slot].setLocation(locX, locY);
    		bullets[slot].setDirection(angle);
    		bullets[slot].reset();
    		// Reset the timer
    		count = RELOAD;
    	}
     
    	int getAvailableBullet(){
    		for (int i = 0; i < bullets.length; i++)
    			if (!bullets[i].isAlive())
    				return i;
    		return -1;
    	}
     
    	void turnRight(double a){
    		angle += a;
    		if (angle > twoPi)
    			angle -= twoPi;
    	}
     
    	void turnLeft(double a){
    		angle -= a;
    		if (angle < 0.0)
    			angle += twoPi;
    	}
     
    	void moveForward(){
    		locX += speed*Math.cos(angle);
    		locY += speed*Math.sin(angle);
    	}
     
    	void backUp(){
    		locX -= speed*Math.cos(angle);
    		locY -= speed*Math.sin(angle);
    	}
     
    }
     
    class Bullet implements Ball {
    	double locX, locY, dx, dy;
    	int tank; // index of tank that fired this bullet
    	boolean alive = false;
    	int ttl; // time to live
    	public static final int LIFETIME = 70;
    	public static final double SPEED = 8.0;
    	public static final int radius = 7;
     
    	public Bullet(int t){
    		tank = t;
    	}
     
    	void update(){
    		int i;
    		// Check if this bullet is worn out
    		ttl--;
    		if (ttl < 0)
    			alive = false;
    		if (!alive) return;
    		// If not worn out, update position
    		locX += SPEED*dx;
    		locY += SPEED*dy;
    		// check for collisions with rocks
    		i = WarPanel.hitAnItem(this, WarPanel.rocks);
    		if (i >= 0){
    			alive = false;
    			// Ask the game to deactivate this rock
    			WarPanel.removeRock(i);
    		}
    		// check for collisions with tanks (other than
    		// our tank)
    		i = WarPanel.hitAnItem(this, WarPanel.tanks);
    		if ((i >= 0) && (i != tank)){
    			alive = false;
    			// Tell game a tank was hit
    			WarPanel.tankHit(i);
    		}
    	}
     
    	public double getX() { return locX; }
    	public double getY() { return locY; }
    	public double getRadius() { return radius; }
     
    	void setLocation(double x, double y){
    		locX = x;
    		locY = y;
    	}
     
    	void setDirection(double angle){
    		dx = Math.cos(angle);
    		dy = Math.sin(angle);
    	}
     
    	void paint(Graphics g){
    		if (alive){
    			g.setColor(Color.black);
    			g.fillOval((int)(locX-radius), (int)(locY-radius), 2*radius, 2*radius);
    		}
    	}
     
    	public boolean isAlive() { return alive; }
     
    	void reset(){
    		ttl = LIFETIME;
    		alive = true;
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Bullets are not showing up across network client screen in game

    How are you debugging the code to see what it is doing?
    What do the println() statements show you when the code is executed?
    How is the info about a bullet fired on one screen transmitted to the other program so it can show it?
    Where in the path of sending that info is the info lost or not handled properly?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Bullets are not showing up across network client screen in game

    I haven't done too many games, but I have a question: isn't it generally discouraged to constantly loop the running animation? I mean, when you are interacting with the game via mouse and keyboard input, why wouldn't you just update the graphics during each mouse/keyboard event (instead of on a timer)? To me, it seems like: why repaint if nothing has changed?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Bullets are not showing up across network client screen in game

    Sleeping the thread is NOT how to accomplish animation in a Java Swing application. You should read more about Java game loops (there are several excellent articles on the topic), Java custom painting, and multithreading (or concurrency) in Swing. These are topics you need to understand better to get a program like this right. It's okay that you don't have it yet. We all started somewhere, most of us with early attempts that didn't work at all.

    One of the first statements when overriding the paintComponent() method should be:

    super.paintComponent( g );

    That statement refreshes that component's graphics object, allowing the animated objects to move in distinct steps rather than blur or slime across the screen.

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bullets are not showing up across network client screen in game

    Let me be more specific. I believe the problem is coming from this class.
    class Tank implements Ball {
     
    double locX, locY, radius, angle;
    int self; // index of this tank in WarPanel.tanks
    public boolean turnL, turnR, forth, back, fire;
    boolean prevtL, prevtR, prevfo;
    Color color;
    Image image;
     
    public static final double twoPi = Math.PI * 2.0;
    public static final double turnRate = Math.PI / 8;
    public static final double speed = 4.0;
    public static final int RELOAD = 8; // delay between bullets
    int count; // timer for reloading
     
    public static final int MAXBULLETS = 7; // max simultaneous shots
    Bullet bullets[] = new Bullet[MAXBULLETS];
     
    AffineTransform saveAT; // place to hold current affine transform
     
    public Tank(double x, double y, double a, int index, Image im){
        locX = x;
        locY = y;
        angle = a;
        self = index;
        image = im;
        radius = 22;
        // create bullets for this tank
        for (int i = 0; i < bullets.length; i++)
            bullets[i] = new Bullet(self);
    }
     
    public double getX() { return locX; }
    public double getY() { return locY; }
    public double getRadius() { return radius; }
    public boolean isAlive() { return true; }
     
    void update(Boolean local) {
        if (turnL)
            turnLeft(turnRate);
        if (turnR)
            turnRight(turnRate);
        if (forth){
            moveForward();
            // Check for rocks
            if (WarPanel.hitAnItem(this, WarPanel.rocks) >= 0)
                backUp();
        }
        if (local){
            if (turnL != prevtL){
              WarPanel.send("turnL "+turnL+" "+locX+" "+locY+" "+angle);
              prevtL = turnL;
            }
            if (turnR != prevtR){
              WarPanel.send("turnR "+turnR+" "+locX+" "+locY+"   "+angle);
              prevtR = turnR;
            }
            if (forth != prevfo){
              WarPanel.send("forth "+forth+" "+locX+" "+locY+" "+angle);
              prevfo = forth;
            }
        }
        if (fire){
            fireBullet();
        }
        // Update all of our bullets
        for (Bullet b: bullets)
            b.update();
    }
     
    public void processMove(String s){
        // Update movement parameters based on s
        Scanner sc = new Scanner(s);
        // Get the flag change
        String command = sc.next();
        boolean value = sc.nextBoolean();
        if (command.equals("turnL"))
            turnL = value;
        else if (command.equals("turnR"))
            turnR = value;
        else if (command.equals("forth"))
            forth = value;
        else
            System.out.println("Unexpected move: "+command);
        // then unpack position update
        locX = sc.nextDouble();
        locY = sc.nextDouble();
        angle = sc.nextDouble();
    }
     
    void paint(Graphics g){
        // Use the affine transform feature in Graphics2D
        // to easily rotate the tank's image.
        Graphics2D g2 = (Graphics2D)g;
        saveAT = g2.getTransform();
        g2.translate(locX, locY);
        g2.rotate(angle);
        g2.drawImage(image, (int)(-radius), (int)(-radius), null);
        // Reset the transform (this is important)
        g2.setTransform(saveAT);
        // Then draw bullets
        for (Bullet b: bullets)
            b.paint(g2);
     
    }
     
    void fireBullet(){
        // If it has been long enough since the last shot...
        count--;
        if (count > 0) return;
        // ...and if all the bullets aren't currently in use...
        int slot = getAvailableBullet();
        if (slot < 0)
            return;
        // ...then launch a new bullet
        bullets[slot].setLocation(locX, locY);
        bullets[slot].setDirection(angle);
        bullets[slot].reset();
        // Reset the timer
        count = RELOAD;
    }
     
    int getAvailableBullet(){
        for (int i = 0; i < bullets.length; i++)
            if (!bullets[i].isAlive())
                return i;
        return -1;
    }
     
    void turnRight(double a){
        angle += a;
        if (angle > twoPi)
            angle -= twoPi;
    }
     
    void turnLeft(double a){
        angle -= a;
        if (angle < 0.0)
            angle += twoPi;
    }
     
    void moveForward(){
        locX += speed*Math.cos(angle);
        locY += speed*Math.sin(angle);
    }
     
    void backUp(){
        locX -= speed*Math.cos(angle);
        locY -= speed*Math.sin(angle);
    }
    }

  6. #6
    Junior Member
    Join Date
    Sep 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bullets are not showing up across network client screen in game

    The game works fine.The code does what its suppose to do. Its up and running however when I fire a bullet it doesnt show up on the other player's screen, just mine.

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Bullets are not showing up across network client screen in game

    Because: when do you send the information to the other player that a bullet has been fired?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. #8
    Junior Member
    Join Date
    Sep 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bullets are not showing up across network client screen in game

    Thats what Im trying to figure out, I have been trying to solve this for 3 days now

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Bullets are not showing up across network client screen in game

    How are you trying to debug the code? I use println() statements to track the sending of the info about the bullet and the receiving that info on the client.
    what is printed on the sending side and what is printed on the receiving side? Where is the info lost?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Bullets are not showing up across network client screen in game

    The problem is that he isn't sending updates to the other user. I would think you would want to send an update when:
    1. The tank is moved
    2. A bullet is created

    There may be some other times as well.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  11. #11
    Junior Member
    Join Date
    Sep 2013
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Bullets are not showing up across network client screen in game

    Quote Originally Posted by aussiemcgr View Post
    The problem is that he isn't sending updates to the other user. I would think you would want to send an update when:
    1. The tank is moved
    2. A bullet is created

    There may be some other times as well.
    So am im updating this method
     
     public void processMove(String s){
    	  // Send command to tank not controlled by
    	  // this player
    	  tanks.get(1 - playerID).processMove(s);
     
      }
    or
    class Bullet implements Ball {
    	double locX, locY, dx, dy;
    	int tank; // index of tank that fired this bullet
    	boolean alive = false;
    	int ttl; // time to live
    	public static final int LIFETIME = 70;
    	public static final double SPEED = 8.0;
    	public static final int radius = 7;
     
    	public Bullet(int t){
    		tank = t;
    	}
     
    	void update(){
    		int i;
    		// Check if this bullet is worn out
    		ttl--;
    		if (ttl < 0)
    			alive = false;
    		if (!alive) return;
    		// If not worn out, update position
    		locX += SPEED*dx;
    		locY += SPEED*dy;
    		// check for collisions with rocks
    		i = WarPanel.hitAnItem(this, WarPanel.rocks);
    		if (i >= 0){
    			alive = false;
    			// Ask the game to deactivate this rock
    			WarPanel.removeRock(i);
    		}
    		// check for collisions with tanks (other than
    		// our tank)
    		i = WarPanel.hitAnItem(this, WarPanel.tanks);
    		if ((i >= 0) && (i != tank)){
    			alive = false;
    			// Tell game a tank was hit
    			WarPanel.tankHit(i);
    		}
    	}

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Bullets are not showing up across network client screen in game

    Where is the update sent to any of the clients?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. network: 2 client - 1 server
    By Askan in forum Java Networking
    Replies: 0
    Last Post: March 31st, 2012, 09:02 AM
  2. Replies: 0
    Last Post: August 30th, 2011, 08:23 AM
  3. java game bullets hit player 1 but not player 2
    By ajakking789 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 22nd, 2011, 08:19 AM
  4. Showing an Image on screen
    By doobybug in forum AWT / Java Swing
    Replies: 1
    Last Post: May 10th, 2011, 07:49 AM