thread error

Started by
138 comments, last by ChristianFrantz 11 years, 8 months ago
It's important to keep the mental distinction between updating and drawing/painting. Those are 2 separate things and you should always be treating them as such.

On the drawing/painting side of that question, you'll want to make sure that you're using some variables to determine the current position of the mainBall (which you're doing in mainBall's paint method by passing variables like x and y to the g.fillOval() call).

On the updating side of that question, you'll want to update the mainBall's x and y values based on what the player presses.
Advertisement
i added the xpos ypos xspeed and yspeed parameters to the mainBall and changed the variables in the keyDown and run method but it still did nothing

[source lang="java"]import java.applet.Applet;
import java.awt.Color;
import java.awt.Component;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class game extends Applet implements Runnable
{
static final int WIDTH = 450;
static final int HEIGHT = 450;
private Image dbImage;
private Graphics dbg;

public static long NEW_DOT_FREQ = TimeUnit.SECONDS.toMillis(3);
public long lastUpdateTime;
public long timeSinceLastNewDot;


public ArrayList<Ball> BALLS;

Color[] color = {Color.red, Color.blue, Color.green, Color.yellow, Color.magenta, Color.black};
int colorIndex;

static final int NUM_OF_BALLS = 4;

int i;
int t;

MainBall mainBall = new MainBall(100, 100, 10, 10, 100, 100, 0, 0);

Thread updateTime = new updateTime();

public void start()
{
lastUpdateTime = System.currentTimeMillis();

Thread th = new Thread(this);
th.start();//start main game

updateTime.start();
}

public void updateGame()
{
//Get the current time
long currentTime = System.currentTimeMillis();
//Calculate how much time has passed since the last update
long elapsedTime = currentTime - lastUpdateTime;
//Store this as the most recent update time
lastUpdateTime = currentTime;

//Create a new dot if enough time has passed
//Update the time since last new dot was drawn
timeSinceLastNewDot += elapsedTime;

if (timeSinceLastNewDot >= NEW_DOT_FREQ)
{
int newX = randomNumber();
int newY = randomNumber();

debugPrint("New dot created at x:" + newX + ", y:" + newY + ".");

BALLS.add(new Ball(newX, newY, 20, 20));

timeSinceLastNewDot = 0;
}
}

private void debugPrint(String value)
{
System.out.println(value);
}

public class updateTime extends Thread implements Runnable
{
public void run()
{
for(t = 0; ; t++)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e){}
}
}
}

public int randomNumber()
{
return (int)(Math.random() * 400);
}

public int getRandomColor()
{
return (int)(Math.random() * 6);
}

public class MainBall
{
int x;
int y;
int width;
int height;
int xpos = 100;
int ypos = 100;
int xspeed = 0;
int yspeed = 0;

public MainBall(int x, int y, int width, int height, int xpos, int ypos, int xspeed, int yspeed)
{
this.x = 100;
this.y = 100;
this.width = 10;
this.height = 10;
this.xpos = 100;
this.ypos = 100;
this.xspeed = 0;
this.yspeed = 0;
}

public void paintMainBall(Graphics g)
{
g.setColor(Color.black);
g.fillOval(x, y, width, height);
g.drawString(xpos + ", " + ypos, 20, 40);
}
}//mainBall

class Ball
{
int x;
int y;
int width;
int height;

public Ball(int x, int y, int width, int height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}//end ball

public void paint(Graphics g)
{
g.setColor(color[getRandomColor()]);
g.fillOval(x, y, width, height);
} //end paint


} //ball class

public void update(Graphics g) //double buffer don't touch!!
{
if(dbImage == null)
{
dbImage = createImage(this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics();
}

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

dbg.setColor(getForeground());
paint(dbg);

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

public boolean keyDown (Event e, int key)
{
if(key == Event.LEFT)
{
mainBall.xspeed = -5;
mainBall.yspeed = 0;
}

if(key == Event.RIGHT)
{
mainBall.xspeed = 5;
mainBall.yspeed = 0;
}

if(key == Event.UP)
{
mainBall.yspeed = -5;
mainBall.xspeed = 0;
}

if(key == Event.DOWN)
{
mainBall.yspeed = 5;
mainBall.xspeed = 0;
}
return true;
}

public void run()
{
while(true)
{
repaint();

if (mainBall.xpos < 1)
{
mainBall.xpos = 449;
}

if (mainBall.xpos > 449)
{
mainBall.xpos = 1;
}
if (mainBall.ypos < 1)
{
mainBall.ypos = 449;
}

if (mainBall.ypos > 449)
{
mainBall.ypos = 1;
}
mainBall.ypos += mainBall.yspeed;
mainBall.xpos += mainBall.xspeed;
try
{
Thread.sleep(20);
}
catch(InterruptedException ex){}
}
}

public void init()
{
this.setSize(WIDTH, HEIGHT);

BALLS = new ArrayList<Ball>();
}

public void paint(Graphics g)
{

g.drawString("time: " + t, 20, 20);

mainBall.paintMainBall(g);

for (Ball ball : BALLS)
{
ball.paint(g);
}

updateGame();
}


}[/source]

If you see a post from me, you can safely assume its C# and XNA :)

I see the problem in there. But I want to give you a chance to look over the code around the problem.

1. What values are you using to draw the mainBall? Look at the code where you actually handle drawing the mainBall.
2. What values do you update in your key handling code? Then follow those values to see what that values affects and what that value affects and so on.
3. Those are the 2 important actions that happen.. you update the values and then you use those values to draw an object. And you repeat those 2 steps over and over. As you're following through step 1 and 2, you need arrive at the same variable at some point. That is to say, you should ultimately be updating some variable and then using that same variable to draw the object.

So, see what you can find/ figure out about those pieces.
1. The values i;m using are the int variables i declared in the constructor. The drawing of the ball is within the class, but im then calling the method through my main paint method with mainBall.paintMainBall(g).

2. The values being updated in the key method are the mainBall.xspeed and mainBall.yspeed. And this part works fine because the coordinates do change when i press a key.

3. Actually i cant really find where im drawing the ball except in the class. and i thought that in the main run method it was being repainted but obviously it isnt.

holy shit youre a genius. i changed the variables in the paintMainBall method to xpos and ypos and it worked biggrin.png

i guess i was just repeatedly drawing the ball to x and y, but they never even changed.\

and now i can FINALLY move onto collision. im thinking:

1. create the public Rectangle getBounds() method with a for loop
2. the for loop will run everytime a new ball is created in the arraylist
3. within the for loop i use ballPositionX = balls.get(0).getx(); and ballPositionY = balls.get(0).gety(); to grab the coordinates of the ball
4. then i add return (something) to return the rectangle

i think thatll work for creating the rectangle around the ball, but in order to actually use it, i might store it in an array and then use (if (playerRectangle.intersects(enemyRectangle)) somewhere in order to stop the mainBall when it comes in contact with another ball

oh but in order to make it a little easier at first, im going to create a fixed ball at 250, 250 just so i can try out the method without getting for loops at first

another issue i just realized is that i have no idea how to use the getBounds method -.-

public Rectangle getBounds()
{
//what do i put in here in order to get the bounds of a circle at point 250, 250 and 20 height and 20 width?

// and this returns a value too so im making an arraylist for this too?
}

If you see a post from me, you can safely assume its C# and XNA :)


i changed the variables in the paintMainBall method to xpos and ypos and it worked

There ya go! And if you're now using xpos and ypos, I'm not sure you even need what you previously called x and y. The last iteration of the code I looked at, you were setting them in the constructor then using them for the draw position, but not using them anywhere else.

1. You say create the method with a for loop. I'm not sure what you're meaning. Do you mean create the method once in the Ball class and then call it in a for loop?
2. It sounds like with this one you're saying, when you add, for example, ball 6, you're gonna run a loop over all of the balls? How does ball 6 being created affect ball 1 (talking in a conversational way, why is the creation of ball 6 related to the long existing ball 1)?
3. Don't forget to grab a width and a height too.
4. Returning a rectangle would be something like return new Rectangle(x, y, width, height);. Of course the magic happens in you getting the appropriate values into x, y, width and height.

Good idea about trying to get your approach working with 1 item rather than jumping right into it working on a growing array of items. :)
alright forget what i said about that for now lol. im gonna start with the simple getBounds of the fixedBall i created.

this is what i have.. but it cant be this simple right?
[source lang="java"] public Rectangle getFixedBallBounds()
{
return new Rectangle (250, 250, 20, 20);
}
[/source]

If you see a post from me, you can safely assume its C# and XNA :)

wow ran into an issue already -.-

[source lang="java"] public Rectangle getMainBallBounds()
{
return new Rectangle(mainBall.xpos, mainBall.ypos, 10, 10);
}[/source]

the position of my main ball is always going to be changing. so do i need a while loop or a for loop here to account for the changing xpos and ypos variables?

nevermind fixed that too. i should stop asking questions before i actually try to figure things out lol.

so for collision. oh boy.

-1. should i store each rectangle created as an object for now? before i work on the array part when thatd be hard to do lol
1. create a method checkCollision(). i dont think it needs to return anything but i could be wrong
2. (if (mainBallRectangle.intersects(fixedBallRectangle))
3. if its true, set mainBall.xspeed = 0 and same with the yspeed

will this work?

If you see a post from me, you can safely assume its C# and XNA :)

fixed ball
Yep it's that simple. Quite simple if nothing is changing. :)

main ball
Nope, no loop. The position of your ball does change often. But that's why when you return a rectangle you use those very values (xpos, ypos) that are changing.
A way to investigate that mentally is think about what you want to accomplish with this method. Its purpose is to return you the rectangle that encloses the mainBall right now. So it doesn't care if the position changed last frame or if it hasn't changed in 20 minutes. All this method is concerned with is returning you that rectangle that encloses the mainBall right now.
this did absolutely nothing lol

[source lang="java"] Rectangle mainBallRectangle = getMainBallBounds();
Rectangle fixedBallRectangle = getFixedBallBounds();[/source]
[source lang="java"] public Rectangle getFixedBallBounds()
{
return new Rectangle (250, 250, 20, 20);
}


public Rectangle getMainBallBounds()
{
return new Rectangle(mainBall.xpos, mainBall.ypos, 10, 10);
}


public void checkCollision()
{
if(mainBallRectangle.intersects(fixedBallRectangle))
{
mainBall.xspeed = 0;
mainBall.yspeed = 0;
}
}[/source]

If you see a post from me, you can safely assume its C# and XNA :)

So by the code, it looks like if the 2 rectangles collide, you set the mainBall to have 0 xspeed and 0 yspeed.

Is that what it's doing?

This topic is closed to new replies.

Advertisement