non static variales - need help

Hi,

I'm a student studying java at the mom and i need a bit of help with a java applet i've been trying to make. I'm trying to make a ping pong game. The code is kinda big so i'll shorten down a lot for you guys.

I have a main method, pitch, player and a ball method. The main method creates 1 ball, 1 pitch and 2 players. Now this all works fine and i got the ball to bounce of the pitch wall no bother. The only way that works is if i have the pitch sides as static variables.

As you can imagine if the ball is flying all over the place the variable y and variable x will change so i cant make the ball position change, and the same applies with the players. They'll be moving all over the place as well.

The probles arrives when i try and tell the ball where the players positions are. If i make the x's and y's variables public for ball and 2 players and output them as a string onto the screen, that works in main. ex. ("ball x pos :" + ball1.ball_x_pos, 30, 50); This seems to work grand.

But when i try and pass the ball's x and y pos into player 1 like so in main, player1.did_ball_bounce(ball1.ball_x_pos, ball_y_pos), player1 only sees them 2 variables as 0, but main can see them as what they are.

If you want to see my code i'll post it up for you guys, but it's a bit long, but very easy to follow. I like my codes easy to follow.

any help will do,

cheers,

Paul

[1447 byte] By [dj_twofacea] at [2007-10-3 8:46:48]
# 1

> I have a main method, pitch, player and a ball

> method.

None of these sound like good names for a method, which would normally include verbs. Player and Ball are perfect candidates for classes.

> The main method creates 1 ball, 1 pitch and

> 2 players.

By the way, if you mean static public void main(String[] args): applets don't have main methods.

> Now this all works fine and i got the

> ball to bounce of the pitch wall no bother. The only

> way that works is if i have the pitch sides as

> static variables.

Are you sure you know what "static" means?

> As you can imagine if the ball is flying all over

> the place the variable y and variable x will change

> so i cant make the ball position change, and the

> same applies with the players. They'll be moving all

> over the place as well.

Sounds like you don't have a consistant data model.

> The probles arrives when i try and tell the ball

> where the players positions are. If i make the x's

> and y's variables public

All attributes should be private.

> for ball and 2 players and

> output them as a string onto the screen, that works

> in main. ex. ("ball x pos :" + ball1.ball_x_pos, 30,

> 50); This seems to work grand.

> But when i try and pass the ball's x and y pos

> into player 1 like so in main,

> player1.did_ball_bounce(ball1.ball_x_pos,

> ball_y_pos), player1 only sees them 2 variables as 0,

Probably because you're not passing them correctly and there's a bug in your code.

> but main can see them as what they are.

> If you want to see my code i'll post it up for

> you guys, but it's a bit long,

Then only post the relevant parts.

CeciNEstPasUnProgrammeura at 2007-7-15 3:55:50 > top of Java-index,Java Essentials,Java Programming...
# 2

package ping;

import java.applet.*;

import java.awt.*;

import java.awt.Event;

import java.awt.Graphics;

import java.awt.Color;

public class main extends Applet implements Runnable

{

private Thread th;

private player player1;

private player player2;

private pitch pitch1;

private ball ball1;

private int playerUP = -2;

private int playerDOWN = 2;

private int playerRIGHT = 2;

private int playerLEFT = -2;

private boolean player1UP;

private boolean player1DOWN;

private boolean player1RIGHT;

private boolean player1LEFT;

private boolean player2UP;

private boolean player2DOWN;

private boolean player2RIGHT;

private boolean player2LEFT;

private Image dbImage;

private Graphics dbg;

int check = 0;

public void init()

{

setBackground(Color.black);

setForeground(Color.white);

player1 = new player(54,160);

player2 = new player(754,160);

pitch1 = new pitch();

ball1 = new ball();

}

public void start()

{

th = new Thread(this);

th.start();

}

public void run()

{

while(true)

{

ball1.move_ball();

repaint();

try

{

Thread.sleep(10);

}

catch (InterruptedException ex)

{

}

}

}

public boolean keyDown(Event e, int key)

{

return true;

}

public boolean keyUp(Event e, int key)

{

return true;

}

public void update(Graphics g)

{

if(dbImage == null)

{

dbImage = createImage(1210,1010);

dbg = dbImage.getGraphics();

}

dbg.setColor(getBackground());

dbg.fillRect(0,0,getSize().width, this.getSize().height);

dbg.setColor(getForeground());

dbg.drawString("player1 top" + player1.player_top, 50, 50);

dbg.drawString("player1 bottom" + player1.player_bottom, 50, 60);

dbg.drawString("player1 left " + player1.player_left, 50, 70);

dbg.drawString("player1 right " + player1.player_right, 50,80);

dbg.drawString("Ball_top " + ball1.ball_top, 400, 50);

dbg.drawString("ball_bottom " + ball1.ball_bottom, 400, 60);

dbg.drawString("ball_left " + ball1.ball_left, 400, 70);

dbg.drawString("ball_right " + ball1.ball_right, 400, 80);

dbg.drawString("player2 top" + player2.player_top, 700, 50);

dbg.drawString("player2 bottom" + player2.player_bottom, 700, 60);

dbg.drawString("player2 left " + player2.player_left, 700, 70);

dbg.drawString("player2 right " + player2.player_right,700,80);

paint(dbg);

g.drawImage(dbImage,0,0,this);

}

public void paint(Graphics g)

{

pitch1.drawPitch(g);

player1.draw_player(g);

player2.draw_player(g);

ball1.draw_ball(g);

}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

player:

package ping;

import java.awt.Graphics;

import java.awt.Color;

public class player

{

private int x_pos;

private int y_pos;

private int pitch_top = pitch.down_y;

private int pitch_bottom = ((pitch.down_y) + (pitch.height));

private int pitch_left = pitch.in_x;

private int pitch_right = ((pitch.in_x) + pitch.width);

private int player_lenght = 80;

private int player_width = 20;

public int player_top = y_pos;

public int player_bottom = (y_pos + player_lenght);

public int player_left = x_pos;

public int player_right = (x_pos + player_width);

public player(int x, int y)

{

x_pos = x;

y_pos = y;

}

public void move_playerX(int speed)

{

if((x_pos > pitch_left) && (x_pos < pitch_right - player_width))

{

x_pos = x_pos + speed;

}

else if((x_pos == pitch_left) && (speed > 0))

{

x_pos = x_pos + speed;

}

else if((x_pos == pitch_right - player_width) && (speed < 0))

{

x_pos = x_pos + speed;

}

}

public void move_playerY(int speed)

{

if((y_pos > pitch_top) && (y_pos < pitch_bottom - player_lenght))

{

y_pos = y_pos + speed;

}

if((y_pos == pitch_top) && (speed > 0))

{

y_pos = y_pos + speed;

}

if((y_pos == pitch_bottom - player_lenght) && (speed < 0))

{

y_pos = y_pos + speed;

}

}

public void draw_player(Graphics g)

{

update();

g.setColor(Color.white);

g.fillRect(x_pos,y_pos,20,80);

}

public void update()

{

player_top = y_pos;

player_bottom = y_pos + player_lenght;

player_left = x_pos;

player_right = x_pos + player_width;

}

public void ball_hit_player()

{

I really dont no what to put in here in order to get the ball_x and ball_y in order to make it bounce back prop of the player

}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

ball:

package ping;

import java.awt.Graphics;

import java.awt.Color;

public class ball

{

private static int radius = 20;

private int pitch_top = pitch.pitch_top;

private int pitch_bottom = pitch.pitch_bottom;

private int pitch_left = pitch.pitch_left;

private int pitch_right = pitch.pitch_right;

private int ball_x = ((pitch_left + pitch_right) / 2);

private int ball_y = ((pitch_top + pitch_bottom) / 2);

public int ball_x_speed = -3;

private int ball_y_speed = 0;

public int ball_top = 0;

public int ball_bottom = 0;

public int ball_right = 0;

public int ball_left = 0;

public void move_ball()

{

rules();

ball_x = ball_x + ball_x_speed;

ball_y = ball_y + ball_y_speed;

}

public void draw_ball(Graphics g)

{

update();

g.setColor(Color.white);

g.fillOval(ball_x,ball_y,radius,radius);

}

public void update()

{

ball_top = ball_y;

ball_bottom = ball_y + radius;

ball_left = ball_x;

ball_right = ball_x + radius;

}

public void bounce_of_wall()

{

if((ball_top + ball_y_speed) < pitch_top)

{

y_out_of_bounds(ball_top, ball_y_speed);

}

if((ball_bottom + ball_y_speed) > pitch_bottom)

{

y_out_of_bounds(ball_bottom, ball_y_speed);

}

if((ball_right + ball_x_speed) > pitch_right)

{

x_out_of_bounds(ball_right, ball_x_speed);

}

if((ball_left + ball_x_speed) < pitch_left)

{

x_out_of_bounds(ball_left,ball_x_speed);

}

}

public void x_out_of_bounds(int x, int y)

{

int distance_out_of_bounds = 0;

int side = x;

int speed = y;

distance_out_of_bounds = side + speed;

distance_out_of_bounds = side - distance_out_of_bounds;

ball_x = ball_x + ball_x_speed;

ball_x_speed = ball_x_speed * -1;

}

public void y_out_of_bounds(int x, int y)

{

int distance_out_of_bounds = 0;

int side = x;

int speed = y;

distance_out_of_bounds = side + speed;

distance_out_of_bounds = side - distance_out_of_bounds;

ball_y = ball_y + ball_y_speed;

ball_y_speed = ball_y_speed * -1;

}

public void rules()

{

bounce_of_wall();

}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

pitch:

package ping;

import java.awt.Graphics;

import java.awt.Color;

public class pitch

{

public static int in_x = 4;

public static int down_y = 4;

public static int width = 800;

public static int height = 400;

public static int pitch_top = down_y;

public static int pitch_bottom = (down_y + height);

public static int pitch_left = in_x;

public static int pitch_right = (in_x + width);

public void drawPitch(Graphics g)

{

g.setColor(Color.white);

g.drawRect(in_x,down_y,width,height);

g.drawRect(in_x,down_y,(width/2),height);

}

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

i dont know what to do in order to pass the ball1 x and y pos into player1 and player2. I know all my variables should be private but some are public because i want to see the values in order to debug the problem.

any help would do.

cheers.

dj_twofacea at 2007-7-15 3:55:50 > top of Java-index,Java Essentials,Java Programming...
# 3
Please try again. This time only the relevant parts, and formatted: http://forum.java.sun.com/help.jspa?sec=formatting
CeciNEstPasUnProgrammeura at 2007-7-15 3:55:50 > top of Java-index,Java Essentials,Java Programming...
# 4

I think you may be confusing methods and classes.

It makes sense to have Pitch, Ball and Player classes (class names should always be capitalised).

You create one instance of Pitch, one of Ball and two of Player.

The objects are connected with references. A Pitch object will point to the Ball and Player objects, and the Ball and Player objects will point to the pitch. So, when a Ball wants to know about a Player (or vica versa) it asks the Pitch object.

Probably the Pitch object will be responsible for creating the other objects as part of it's constructor, it will pass a reference to itself, as part of the constructor arguments of Ball and Player.

malcolmmca at 2007-7-15 3:55:50 > top of Java-index,Java Essentials,Java Programming...
# 5
Question: why would player 1 and 2 need to know the ball's position?
CeciNEstPasUnProgrammeura at 2007-7-15 3:55:50 > top of Java-index,Java Essentials,Java Programming...
# 6

cool, so instead of making an instance of ball and player in my main class, when i make my pitch instance, have my pitch create my ball and players instead of my main class?

how would i make player1 and player2 see where the ball is? The reason is i'm tying to make the ball bounce back of the player. I've tried making a method in player, calling the method in main and sending in the balls pos like so :

player1.Check_col(ball1.ball_left, ball1.ball_right........... ect.

that dosn't seem to work though.

dj_twofacea at 2007-7-15 3:55:50 > top of Java-index,Java Essentials,Java Programming...
# 7
> how would i make player1 and player2 see where the> ball is?Let the players ask the ball for its position when they need it.
CeciNEstPasUnProgrammeura at 2007-7-15 3:55:50 > top of Java-index,Java Essentials,Java Programming...
# 8
k, i'm only new to java and i cant seem to do that. What would i need to do?In player what would i need to write. I cant make a method saying get the values off ball1 because i get "ball1 cannot be resolved". any suggestions?
dj_twofacea at 2007-7-15 3:55:50 > top of Java-index,Java Essentials,Java Programming...
# 9

> k, i'm only new to java

That doesn't matter at all. Figuring out whoch components of your program would need to know what doesn't even need one single line of Java code.

> and i cant seem to do that.

> What would i need to do?

theBall.getPosition()

> In player what would i need to write. I cant make a

> method saying get the values off ball1 because i get

> "ball1 cannot be resolved".

>

> any suggestions?

How about declaring ball1 first, before using it?

CeciNEstPasUnProgrammeura at 2007-7-15 3:55:50 > top of Java-index,Java Essentials,Java Programming...
# 10
thanks man
dj_twofacea at 2007-7-15 3:55:50 > top of Java-index,Java Essentials,Java Programming...
# 11

The Player method needs to ask the Pitch object for info about the ball. The Pitch object, which you set when you first create the Player, either returns a reference to the Ball object, or it tells you, say, the ball position (a method in Pitch would ask the Ball for it's position and you'd call that method from Player).

malcolmmca at 2007-7-15 3:55:50 > top of Java-index,Java Essentials,Java Programming...