• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ChristianFrantz

thread error

139 posts in this topic

Haha, nice job working through the random number stuff. It sounds like you're getting pretty close to getting through the stuff that you had in mind when you started this post. Sounds like a nice few days of progress!
0

Share this post


Link to post
Share on other sites
Your method for a random number works but it keeps creating random numbers. That's kind of good since im looking to create a new one every 10 seconds but for this exercise i need to figure out how to stop it lol
0

Share this post


Link to post
Share on other sites
Hmm, what do you mean you want to stop it? You will only use the random numbers when you create the object. You'll just be storing it and re-using the same value when you draw it every time.
0

Share this post


Link to post
Share on other sites
well when i run the program it creates new numbers every 20 milliseconds when its updating the ball position when i move it with the keys. so maybe theres a way that i can just set it to every 10 seconds without effecting the draw update time of the ball
0

Share this post


Link to post
Share on other sites
Ok, so post what your game class is looking like right now.

It shouldn't be generating a new random number every 20 milliseconds if I'm understanding what you want to do correctly. You should use the random number in the init method where you create the ball. But you shouldn't be getting random numbers anywhere else.
0

Share this post


Link to post
Share on other sites
now it creates one number but gives me a thousand errors lol

[source lang="java"]

import java.applet.*;
import java.awt.*;
import java.util.Timer;

public class game extends Applet implements Runnable
{
int xpos = 100;
int ypos = 100;
int radius = 5;
int xspeed = 0;
int yspeed = 0;
static final int WIDTH = 450;
static final int HEIGHT = 450;
private Image dbImage;
private Graphics dbg;

public Ball ball[];

static final int NUM_OF_BALLS = 4;

int i;
int t;

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

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


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

public void paint(Graphics g)
{
for(i = 0; ilessthanNUM_OF_BALLS; i++)
{
g.setColor(ball[i].mycolor);
g.fillOval((int)ball[i].x, (int)ball[i].y, (int)ball[i].width, (int)ball[i].height);
}//end for
}//end paint
}//ball class

public void update(Graphics g)//doublebuffer dont 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 void init()
{
this.setSize(WIDTH, HEIGHT);


ball = new Ball[NUM_OF_BALLS];

ball[0] = new Ball(randomNumber(), randomNumber(), 20, 20, Color.red);
ball[1] = new Ball(randomNumber(), randomNumber(), 20, 20, Color.blue);
ball[2] = new Ball(randomNumber(), randomNumber(), 20, 20, Color.green);
ball[3] = new Ball(randomNumber(), randomNumber(), 20, 20, Color.magenta);

}

public void start()
{
Thread th = new Thread(this);
th.start();//start main game
}

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

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

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

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

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

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

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

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

public void paint(Graphics g)
{

g.drawString("Red ball: " + randomNumber() + ", " + randomNumber(), 20, 40);
g.drawString("blue ball: " + randomNumber() + ", " + randomNumber(), 20, 60);
g.drawString("green ball: " + randomNumber() + ", " + randomNumber(), 20, 80);
g.drawString("magenta ball: " + randomNumber() + ", " + randomNumber(), 20, 100);

for(i = 0; ilessthanNUM_OF_BALLS; i++);
{
ball[i].paint(g);
}

g.setColor(Color.black);
g.fillOval(xpos - radius, ypos - radius, 2 * radius, 2 * radius);
}
}[/source]
0

Share this post


Link to post
Share on other sites
no errors anymore. idk what i did to give me errors [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] but in the paint method where i display the coordinates of each ball, the numbers are changing really fast

well i figured out the timer

[source lang="java"]Thread updateBalls = new updateBalls();

public class updateBalls extends Thread implements Runnable
{
public void run()
{
for(t = 0; t < 10; t++)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e){}
}
if(t == 5)
{
randomNumber();
}
}
}
[/source]

now i need to repaint the screen every 5 seconds somehow Edited by burnt_casadilla
0

Share this post


Link to post
Share on other sites
You solved them with magic! :)

So, for one thing you need to work on that Ball class' paint method. There shouldn't be a loop in it. One of my longer posts a few things back talks about why and how.

Those number are changing fast in there because you do get a different number every time you call the randomNumber method. But you aren't actually use that number anywhere.. just printing it out on the screen.
0

Share this post


Link to post
Share on other sites
When i deleted the for loop, nothing worked at all. it drew one red ball on the screen and i couldnt close the app lol
0

Share this post


Link to post
Share on other sites
Well that's definitely not good for business, ha.
When you removed the loop from Ball's paint method, did you also change it so it wasn't doing any ball[i] stuff? There wouldn't need to be any array stuff in there.
And you kept the loop in game's paint method?
0

Share this post


Link to post
Share on other sites
oh yup now it works fine lol

[source lang="java"]class Ball
{
int x;
int y;
int width;
int height;
Color mycolor;

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

public void paint(Graphics g)
{
g.setColor(mycolor);
g.fillOval(x, y, width, height);
} //end paint
} //ball class
[/source]

[source lang="java"]public void paint(Graphics g)
{
g.drawString("time: " + t, 20, 20);
g.drawString("Red ball: " + randomNumber() + ", " + randomNumber(), 20, 40);
g.drawString("blue ball: " + randomNumber() + ", " + randomNumber(), 20, 60);
g.drawString("green ball: " + randomNumber() + ", " + randomNumber(), 20, 80);
g.drawString("magenta ball: " + randomNumber() + ", " + randomNumber(), 20, 100);


g.setColor(Color.black);
g.fillOval(xpos - radius, ypos - radius, 2 * radius, 2 * radius);

for(i = 0; ilessthanNUM_OF_BALLS; i++)
{
ball[i].paint(g);
}
}
}[/source]
0

Share this post


Link to post
Share on other sites
I wanna reiterate since I am throwing up different issues kind of all at once. Remember to take one issue and work on solving it (small steps). So don't try to change and update and add 3 or 4 things all at once.

For the thread code you added, I wanna say I think adding threads is only going to make this more complex, harder to maintain and harder to understand what's going wrong when something does. But if you insist on diving into threads, We'll need to see the code where you start or schedule that thread to run. For the code you posted, if (t == 5) would never be true. You have a for loop that runs from 0 through 9 and quits when t equals 10 so t will equal 10 when that loop is exited. If it were to ever be true, the line of code inside just generates a random number but doesn't do anything with it or put it anywhere so that you can use it (assuming the randomNumber method is the same as before).
0

Share this post


Link to post
Share on other sites
Excellent. Now that's a solid little Ball class. Does that leave you now working on getting them appearing every 10 seconds?
0

Share this post


Link to post
Share on other sites
yeah threads are really confusing but i figure i gotta learn them sometime lol. right now i have this

[source lang="java"] public void paint(Graphics g)
{
g.drawString("time: " + t, 20, 20);
g.drawString("Red ball: " + randomNumber() + ", " + randomNumber(), 20, 40);
g.drawString("blue ball: " + randomNumber() + ", " + randomNumber(), 20, 60);
g.drawString("green ball: " + randomNumber() + ", " + randomNumber(), 20, 80);
g.drawString("magenta ball: " + randomNumber() + ", " + randomNumber(), 20, 100);

g.setColor(Color.black);
g.fillOval(xpos - radius, ypos - radius, 2 * radius, 2 * radius);

for(i = 0; ilessthanNUM_OF_BALLS; i++)
{
ball[i].paint(g);
try
{
Thread.sleep(5000);

}catch(InterruptedException e){}
}
}
}[/source]
this completely stops my whole program for running but every 5 seconds a new ball will pop up on the screen. Kinda what wanted but im not sure what i did to make the whole program stop lol
0

Share this post


Link to post
Share on other sites
I wish I could say that after you've used Threads a little, they become easy. But they never do... Most programmers reach a point where they can write [u]good[/u] threaded code, but still not actually correct.. just that it's correct enough that it doesn't bite them in the ass all that often.

Alright, a couple of things:

1. Right now you're generating a new random number every time you do one of those drawString statements. That is just like it sounds like, a random number with no particular meaning or relation to anything. It's random. It looks like you're wanting to treat those statements like debugging kind of statements that tell you where each ball actually is. For that information, you'd want to look at the variables the ball has (like ball[0].x, ball[0].y, etc).

2. The reason your whole program "stops" for 5 seconds is you tell this program's thread to sleep for 5 seconds ([i]Thread.sleep(5000[/i]). So for 5 seconds this thread isn't responding or updating or drawing or anything. What you want probably isn't to do NOTHING for 5 seconds.. just that you have something you DO want to happen every 5 seconds. For help on that, I think you'll need to post all of your code, as this will be a time where I need to take in what you're doing on the various threads and to try to see what you might be doing that you don't mean to be doing on the various threads.
0

Share this post


Link to post
Share on other sites
ah thanks. i didnt understand that each time i wrote randomNumber() in the drawString i was creating a new number lol

the updateBalls thread is obviously wrong but ive been messing around with a few things in order to figure out what id have to change

[source lang="java"]public class game extends Applet implements Runnable
{
int xpos = 100;
int ypos = 100;
int radius = 5;
int xspeed = 0;
int yspeed = 0;
static final int WIDTH = 450;
static final int HEIGHT = 450;
private Image dbImage;
private Graphics dbg;

boolean run = true;

public Ball ball[];

static final int NUM_OF_BALLS = 4;

int i;
int t;

Thread updateTime = new updateTime();

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

updateTime.start();
}

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

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

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

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

public void paint(Graphics g)
{
g.setColor(mycolor);
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 class updateBalls extends Thread implements Runnable
{
public void run(Graphics g)
{
for(i = 0; i<NUM_OF_BALLS; i++)
{
ball[i].paint(g);
}
try
{
Thread.sleep(5000);
}catch(InterruptedException e){}
}
}*/

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

ball = new Ball[NUM_OF_BALLS];

ball[0] = new Ball(randomNumber(), randomNumber(), 20, 20, Color.red);
ball[1] = new Ball(randomNumber(), randomNumber(), 20, 20, Color.blue);
ball[2] = new Ball(randomNumber(), randomNumber(), 20, 20, Color.green);
ball[3] = new Ball(randomNumber(), randomNumber(), 20, 20, Color.magenta);
}

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

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

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

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

return true;
}

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

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

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

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

public void paint(Graphics g)
{
g.drawString("time: " + t, 20, 20);
g.drawString("Red ball: " + randomNumber() + ", " + randomNumber(), 20, 40);
g.drawString("blue ball: " + randomNumber() + ", " + randomNumber(), 20, 60);
g.drawString("green ball: " + randomNumber() + ", " + randomNumber(), 20, 80);
g.drawString("magenta ball: " + randomNumber() + ", " + randomNumber(), 20, 100);

g.setColor(Color.black);
g.fillOval(xpos - radius, ypos - radius, 2 * radius, 2 * radius);

for(i = 0; ilessthanNUM_OF_BALLS; i++)
{
ball[i].paint(g);
}
}
}[/source] Edited by burnt_casadilla
0

Share this post


Link to post
Share on other sites
I think if you're gonna stick with Threads so heavily, you'll need to go read up on some concurrency ([url="http://docs.oracle.com/javase/tutorial/essential/concurrency/"]http://docs.oracle.com/javase/tutorial/essential/concurrency/[/url]) to get a good idea of what goes on and what you want to go on.

I'll also write a more lengthy post about a good to do what you want to w/o threads.. it'll be an hour or so though probably before I can do that.
0

Share this post


Link to post
Share on other sites
Alright, sorry but I needed to support my gaming habit for a while. I just put this together and I did confirm that it runs. If you have any questions about why I did something, please do feel free to ask because I can't/won't say that everything is the best approach to a problem. I put it together to show you things being done based on time but not centered around running several threads within my program.

[source lang="java"]import java.applet.Applet;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class GameWithTime extends Applet implements Runnable
{
//Sets NEW_DOT_FREQUENCY as 3 seconds
private static long NEW_DOT_FREQUENCY = TimeUnit.SECONDS.toMillis(3);
private static final int SCREEN_WIDTH = 400;
private static final int SCREEN_HEIGHT = 400;

//Stores what the time was when updateGame last ran
private long lastUpdateTime = 0;
//Tracks how much time has passed since the last new dot was added
private long timeSinceLastNewDot = 0;
//List of all the enemy dots
private ArrayList<Dot> enemyDots;

private Thread animator;
private boolean isRunning;

//Initialize things for the game
public void init()
{
debugPrint("Initializing values.");
//Put 4 dots in the list initially
enemyDots = new ArrayList<Dot>();
enemyDots.add(new Dot(randomValue(), randomValue(), 10, 10));
enemyDots.add(new Dot(randomValue(), randomValue(), 10, 20));
enemyDots.add(new Dot(randomValue(), randomValue(), 20, 10));
enemyDots.add(new Dot(randomValue(), randomValue(), 20, 20));
}

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

animator = new Thread(this);
animator.start();
}

public void stop()
{
isRunning = false;
}

//Applet is closing, clean up
public void destroy()
{

}

public void run()
{
while (isRunning)
{
repaint();
try
{
Thread.sleep(15);
} catch (InterruptedException e)
{
}
}
}

//This update and paint approach is an alternative to double buffering to avoid flickering
public void update(Graphics g)
{
//Update variables
updateGame();

//Draw things to the screen
//Clear the screen
g.setColor(Color.BLACK);
g.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);

g.setColor(Color.cyan);
//Draw each dot in the list
for (Dot dot : enemyDots)
{
dot.draw(g);
}
}

//This update and paint approach is an alternative to double buffering to avoid flickering
public void paint(Graphics g)
{
update(g);
}

private 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;

//Process inputs
updateVariablesBasedOnUserInput();

//Create a new dot if enough time has passed
//Update the time since last new dot was drawn
timeSinceLastNewDot += elapsedTime;
//See if enough time has passed to create a new dot
debugPrint(Long.toString(timeSinceLastNewDot));

if (timeSinceLastNewDot >= NEW_DOT_FREQUENCY)
{
//Generate random x and y values for new dot
int newX = randomValue();
int newY = randomValue();

//Add a new dot to the collection of dots
debugPrint("New dot created at x:" + newX + ", y:" + newY + ".");
enemyDots.add(new Dot(newX, newY, 20, 20));

//Reset time since a new dot was created
timeSinceLastNewDot = 0;
}
}

private void updateVariablesBasedOnUserInput()
{
//Set speeds/velocities based on user inputs
}

//Returns a new random value each time a call is made
private int randomValue()
{
return (int) (Math.random() * 400);
}

//Prints passed value to the console
private void debugPrint(String value)
{
System.out.println(value);
}

private class Dot extends Ellipse2D.Float
{
public Dot(int xValue, int yValue, int width, int height)
{
this.x = xValue;
this.y = yValue;
this.width = width;
this.height = height;
}

public void draw(Graphics graphics)
{
graphics.drawOval((int) x, (int) y, (int) width, (int) height);
}
}
}
[/source] Edited by j-locke
0

Share this post


Link to post
Share on other sites
After a break from programming for 8 hours straight and a good nights sleep, this finally makes sense to me. thank you [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

oh and i like the array list. good thinking cuz im gonna want even more dots when its finished. my ultimate goal is to create dots every 5 seconds and keep creating them until the main dot comes in contact with one and then its game over Edited by burnt_casadilla
0

Share this post


Link to post
Share on other sites
in this block of code

[source lang="java"] public void update(Graphics g)
{

updateGame();

g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);

g.setColor(Color.cyan);

for (ball ball : ball)
{
ball.paint(g);
}
}
[/source]

im getting an error that says "can only iterate over an instance of an array" and i have no idea what that means lol
0

Share this post


Link to post
Share on other sites
It's talking about your line [i]for (ball ball : ball)[/i].

How that line is supposed to read is [i]for (<datatype> <variable-name> : <collection-object>)[/i].
So with you saying ball 3 times, I'm not sure what you intended to put there. With the usual Java naming convention, you should name your classes (which are data types that created by you) with a capital letter (which you had done last time I checked, except where you named the game class lowercase).

So maybe you meant [i]for (Ball ball : balls)[/i]. Since an array represents a collection of things (usually more than 1), I find it useful to name the collection as plural.

Directly to your error, it sounds like your array is not named ball so the third 'ball' in your statement should be changed to match the array's name.
0

Share this post


Link to post
Share on other sites
[quote name='burnt_casadilla' timestamp='1344791238' post='4968750']
After a break from programming for 8 hours straight and a good nights sleep, this finally makes sense to me. thank you [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

oh and i like the array list. good thinking cuz im gonna want even more dots when its finished. my ultimate goal is to create dots every 5 seconds and keep creating them until the main dot comes in contact with one and then its game over
[/quote]

You're welcome. Glad I could provide some help. And getting your eyes off the code can be the best approach to understanding sometimes! :)

Good to know what your game is actually about now. For the collision, [i]ball.getBounds() [/i]and/or [i]ball.getBounds2D()[/i] could probably prove helpful.

.getBounds() documentation: [url="http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/geom/RectangularShape.html#getBounds()"]http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/geom/RectangularShape.html#getBounds()[/url]
.getBounds2D() documentation: [url="http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/geom/Ellipse2D.Float.html#getBounds2D()"]http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/geom/Ellipse2D.Float.html#getBounds2D()[/url]
0

Share this post


Link to post
Share on other sites
i decided to take it one step at a time since i was having more trouble. im starting with switching the balls to an array list and getting that running first before trying the time thing
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0