thread error

Started by
138 comments, last by ChristianFrantz 11 years, 8 months ago
yeah i just want the ball to stop when it hits the fixedBallRectangle

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

Advertisement
Do you have the mainBall moving automatically or being moved by you? if moved by you, go ahead and post all of your code so I can see what else is going on.
the mainBall is being moved by the keyboard

[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; //set screen height
static final int HEIGHT = 450; // set screen width
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; //arraylist to store 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, 0, 0);
Ball fixedBall = new Ball(250, 250, 20, 20);

Rectangle mainBallRectangle = getMainBallBounds();
Rectangle fixedBallRectangle = getFixedBallBounds();

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 width;
int height;
int xpos;
int ypos;
int xspeed;
int yspeed;

public MainBall( int xpos, int ypos, int width, int height, int xspeed, int yspeed)
{
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(mainBall.xpos, mainBall.ypos, 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>();


System.out.println(getFixedBallBounds());

}

public void paint(Graphics g)
{
g.drawString("time: " + t, 20, 20);

mainBall.paintMainBall(g);

g.setColor(Color.blue);
fixedBall.paint(g);

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

updateGame();
}

public Rectangle getFixedBallBounds()
{
return new Rectangle (250, 250, 20, 20);
}


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


public boolean checkCollision()
{
if(mainBallRectangle.intersects(fixedBallRectangle))
{
mainBall.xspeed = 0;
mainBall.yspeed = 0;
mainBall.xpos = 100;
mainBall.ypos = 100;
}
return true;
}

}[/source]

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

I don't see where you're calling checkChollision anywhere. Am I just overlooking it?
no youre not. im just retarded. do i call it in the run method within the while loop?

just tried that and it didnt work. so ill try a few other places

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

You'll want to keep simplifying the run method more and more. Ideally that method should be pretty much what the run method was in that simple example code that I posted. When things are getting toyed with in fewer places, it's easier for you to know where to go when something isn't behaving like you want it to.

In game's paint method, I think you should move the updateGame() call to the beginning of the method instead of the end. Also, moving all of your game logic into updateGame will make this more straightforward. Right now you have some in updateGame and some in run.
that does look a lot nicer lol. but moving the checkCollision method to updateGame doesnt do anything so im thinking it has something to do with the actual method

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

Alright now that it's happening in one location, it's easier to make sure things happen in the order that we want.

You'll want to make sure that you do the checkCollision check (and setting the speed to 0), just before you update the mainBall position.

[source lang="java"]//Update mainBall Rectangle
mainBallRectangle = getMainBallBounds();

//Check for collisions
checkCollision();

//Update mainBall position
mainBall.ypos += mainBall.yspeed;
mainBall.xpos += mainBall.xspeed;

//Wrap mainBall position
if (mainBall.xpos < 1)
{
}
...
....
.....[/source]

Now you have the collision code that will potentially set the speed to 0 right before the location is updated based on that speed.
Still not working :/ i have no idea what im doing wrong

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

Let's have a look at what your code is looking like now.

I can say the first things I'll be looking at are where those two rectangles are getting set. The idea is to make sure they're being set when they have the legit information in them. Putting a breakpoint in the checkCollision method is also a good idea to confirm that intersects check is actually returning true like we expect it to. And after that to put breakpoints at the lines where the ball's position is actually updated (mainBall.xpos += mainBall.xspeed;) to confirm that it has the value I expect it to have. You should also take a look at those points. They seem like likely places for things to be going wrong to me.

It'll probably be tomorrow before I give any indepth look though. I'll take a look at the code for essentially the first step that I said because that one could be a quick one to notice. But since the others will take running and debugging code, I'll put those off in my tired state.

This topic is closed to new replies.

Advertisement