• 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

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.
0

Share this post


Link to post
Share on other sites
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]
0

Share this post


Link to post
Share on other sites
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 Edited by burnt_casadilla
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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 ([i]mainBall.xpos += mainBall.xspeed;)[/i] 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.
0

Share this post


Link to post
Share on other sites
here it is. im done coding for the night tho lol

[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()
{

checkCollision();

//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;
}



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

try
{
Thread.sleep(20);
}
catch(InterruptedException ex){}

checkCollision();

mainBall.ypos += mainBall.yspeed;
mainBall.xpos += mainBall.xspeed;
}

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 passThroughWalls()
{

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

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

BALLS = new ArrayList<Ball>();

System.out.println(getFixedBallBounds());

}

public void paint(Graphics g)
{
updateGame();

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

mainBall.paintMainBall(g);

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

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

}

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;
mainBall.xpos = 100;
mainBall.ypos = 100;
}
}

}[/source]
0

Share this post


Link to post
Share on other sites
You'll want to take a look at the code snippet that I posted most recently. In your updateGame method, you'll want to update the mainBall Rectangle and you might as well do the fixedBall one as well. Make sure you do that before you check for collisions in the method.
1

Share this post


Link to post
Share on other sites
Yeah, I'm done for the night as well. You made good progress for a weekend of coding. Hopefully you've learned some stuff and are feeling more comfortable with what's going on as well. That's what it's really all about. Building that understanding and comfort in knowing that you're growing as a developer. :)
0

Share this post


Link to post
Share on other sites
alright back to work lol
i have this in my ball class

[source lang="java"] public Rectangle getBallBounds()
{
for(Ball ball : BALLS)
return new Rectangle()
}[/source]

this should return a rectangle every time one is created i think? besides the return method not being finished. i dont know how to call the coordinates of a ball in the array

Changed it to this

[source lang="java"] public Rectangle getBallBounds()
{
for(int i = 0; i < BALLS.size(); i++)
{
return new Rectangle(BALLS(i).x, BALLS(i).y, 20, 20);
}
Rectangle ballRectangle = getBallBounds();
}[/source]

so i will increase when the arraylist BALLS increases. but for some reason its saying BALLS(int) isnt a method when i want to call the ArrayList BALLS

and now to this

[source lang="java"] public Rectangle getBallBounds()
{
for(int i = 0; i < BALLS.size(); i++)
{
return new Rectangle(BALLS.get(i).x, BALLS.get(i).y, 20, 20);
}
Rectangle ballRectangle = getBallBounds();
}[/source]

now its saying the type im returning isnt a rectangle?

alright try again...
[source lang="java"] public Rectangle getBallBounds()
{
for(int i = 0; i < BALLS.size(); i++)
{
new Rectangle(BALLS.get(i).x, BALLS.get(i).y, 20, 20);
}
return randomBallRectangle;
}[/source]

i made an object in the main game class Rectangle randomBallRectangle = getBallBounds();
the compiler shows no errors until i run it then i get this:

java.lang.NullPointerException
at game.getBallBounds(game.java:290)
at game.<init>(game.java:38) Edited by burnt_casadilla
0

Share this post


Link to post
Share on other sites
In the Ball class is definitely the right place for this method. Glad to see you moved it there rather than keeping it outside of the class. :)

You don't want a for loop here. The reason you don't goes back to a concept that we talked about earlier. This class is designed to represent 1 ball. That means inside the Ball class, you should pretend that the array of balls doesn't exist. Because this code is intended to represent 1 ball, it doesn't care if you stick it in an array or not to make your game work. Because you do want an array of balls for your game, that's why your game class is creating and managing an array of balls.

So this method inside the Ball class should be concerned with returning you the Rectangle that encloses "me".. "me" being the 1 and only ball that it knows or cares about. The fact that you have an array of Balls and need Rectangles for them all is something for the game class to deal with (again, because the game class does want this array of Balls so that it can accomplish what it wants for this game).

So for the first step, focus on getting this method returning the Rectangle around itself (the "me" I mentioned before).
And then for the second step, get a loop (somewhere in the game class because the Ball class doesn't and shouldn't care about the array) that goes through each of the Balls in the array and stores you a Rectangle for each one.
Note: Determining where step 2 goes should be made easier by the fact that we did some simplifying of things last night. In your game class, you should be adding code in one of relatively few places. In the init method if you just need it to happen once when you initially launch your game. In the updateGame method if it's related to updating things that change during your game. And in the paint method if this involves drawing things for your game.
0

Share this post


Link to post
Share on other sites
[source lang="java"]
public Rectangle getBallBounds()
{
return new Rectangle(this.x, this.y, 20, 20);
}
[/source]

this is called everytime the ball class is used?

can you explain why i should keep this in the ball class? why would it be any different if it was outside of the class?

im thinking i should put it in the updateGame method and in the if method after the new ball is created every three seconds

[source lang="java"] 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));

getBallBounds();

timeSinceLastNewDot = 0;
}[/source]

so maybe something like this? Edited by burnt_casadilla
0

Share this post


Link to post
Share on other sites
[quote name='burnt_casadilla' timestamp='1344903432' post='4969256']
can you explain why i should keep this in the ball class? why would it be any different if it was outside of the class?
[/quote]

Definitely. Good question. Inside the class, it allows you to have a ball object [i]Ball myBall = new Ball(x, y, width, height);[/i] and be able to refer to that ball's rectangle without needing to know about or deal with any other balls, [i]Rectangle boundary = [/i][i]myBall.getBallBounds();[/i].

If I put it outside the class and I want to know the boundaries of any given ball, I'll need to provide this method with what ball I am talking about (something like [i]Rectangle boundary = getBallBounds(myBall);[/i]). It would certainly not be wrong to do it that way. But I've noticed that generally if you're making a method from scratch, you've tended to make it with no parameters. So I was favoring the way your were already writing methods.

And another perk (but not a 'metric' you should be using to make decisions at this point) is if your next game was going to do something different but still be using balls, you could very well have this Ball class in its own file and use it for both this game and the next one. If you have getBallBounds(Ball ball) in this game class, that means if your next game needed that same functionality, you'd have to implement it again. But since it does make good sense that the ball would be able to provide you with its own bounds, having inside the class is logical and makes one less piece of functionality that you'd have to re-implement.

[quote name='burnt_casadilla' timestamp='1344903432' post='4969256']
im thinking i should put it in the updateGame method and in the if method after the new ball is created every three seconds

...

so maybe something like this?
[/quote]
In this code, you call getBallBounds() so it's returning you a Rectangle object but you're not doing anything with it. So you're not using it right now nor are you saving it somewhere (in some variable) so you can pull it back up later to use.

Your getBallBounds method is looking alright. You did pull up the x and y values from this particular ball. Why did you hardcode the width and height rather than pulling those values from this particular ball as well? Edited by j-locke
0

Share this post


Link to post
Share on other sites
the width and height of the randomBalls will always be 20, 20 for now until i figure this out and can add more types of balls.

thanks for the explanation [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

well in the code i listed it would be calling the method, but in the main game class i would create the object Rectangle randomBallRectangle = getBallBounds();



also for my next game im planning on making a "Snake" clone so ill keep in mind what methods and classes i can reuse Edited by burnt_casadilla
0

Share this post


Link to post
Share on other sites
[quote name='burnt_casadilla' timestamp='1344908973' post='4969284']
well in the code i listed it would be calling the method, but in the main game class i would create the object Rectangle randomBallRectangle = getBallBounds();
[/quote]

Ah, ok. You may already know this (because I realize we're talking in kind of code short hand sometimes and it's just understood what code goes where) but in case not I'll bring it up now before it is an issue of confusion later.


When you call getBallBounds(), it will return you the Rectangle and store it in whatever variable you put on the left side of the equal sign. So if you call balls.get(i).getBallBounds(), it won't go find some matching piece of code to find out where to store the Rectangle that is returned. It will simply return it and then it will be forever lost. So the confusion i wanted to be sure to avoid is that calling getBallBounds() is useless unless you are using the returned Rectangle either by having it in a statement (such as [i]if (playerRectangle.intersects([/i][i]balls.get(i).getBallBounds()))[/i]) or storing it so you can use it later (such as [i]randomBallRectangle = balls.get(i).getBallBounds()[/i]).

I like that the next planned project isn't overly complex. That small steps approach will have you making some bigger projects in a couple of months time and hopefully not feeling TOO overwhelmed as you didn't jump too far beyond your knowledge all at once. :)
0

Share this post


Link to post
Share on other sites
i had a long day at work and my brain is scrambled so i might need an explanation for this.

would just putting Rectangle randomBallRectangle = balls.get(i).getBallBounds()); in the update game method work? and would i first have to create the rectangle randomBallRectangle in the main game class? or can i do that all at once in the update game method?

just tried and it doesnt work. so im guessing id need a for loop Edited by burnt_casadilla
0

Share this post


Link to post
Share on other sites
Let's go a little more indepth with what goes on for that method. For the time being we'll treat it like you only have 1 of them and it's named randomBall.

In your updateGame method you could have this code:

[source lang="java"]//This line has randomBall create and return a Rectangle that encloses it ('it' being the one ball
//that we have stored in the variable randomBall)
Rectangle randomBallRectangle = randomBall.getBallBounds();

Rectangle mainBallRectangle = mainBall.getBallBounds();

if (mainBallRectangle.intersects(randomBallRectangle))
{
//Do relevant stuff for a collision
}[/source]

It's in our updateGame method so that on every pass through our updateGame method, we get new rectangles from mainBall and randomBall. That means if they've moved, we're still getting up to date information because we're asking for it again every update before making the comparison.

Also note that I declared both of those Rectangle variables here inside the updateGame method. That means when the updateGame method ends, these variables are gone (they've gone [u]out of scope[/u]) and then they are created anew next time through the updateGame method. This works in this particular design, but if I wanted to do something like be able to draw those rectangles (maybe for debugging purposes, or whatever), this wouldn't work because they wouldn't exist anymore by the time I got to the paint method. To get that kind of functionality, I would declare the variables in the game class (but not in any method) and then just assign to them each time through the updateGame method.

A good design question for you to think about is when you get the rectangle of one of your multiple random balls, what do you want to do with it? Do you want to compare it immediately against the mainBallRectangle? Do you want to store it in an array so you can later go through that array of Rectangles to see if each collides with mainBallRectangle? Since the random balls don't move, do you want to get their Rectangle as soon as the ball is created and store it in an array (that would mean you wouldn't need to update each time through the updateGame method)? Do you have a different approach in mind? Edited by j-locke
0

Share this post


Link to post
Share on other sites
What i really wanted to do is store each rectangle in an array every time a new ball was created and then call the rectangle from the array each time the updateGame method was run to check for a collision. I think itd help me learn arrays better by learning how to call each object in the array and maybe itd be easier for me to understand for now

i understand what youre saying by putting the method in the updateGame method, but it makes more sense to me to store each rectangle created so i can call the location of each rectangle for debugging purposes just to make sure it works

maybe it just makes more sense to me that way because everything is stored in an array and everything can be called from that array when its needed. but honestly i really dont know why that makes more sense :P Edited by burnt_casadilla
0

Share this post


Link to post
Share on other sites
Hey, for now all that matters is that you've chosen a design so now you can progress towards making that design a reality. We all think differently and fortunately computer programming leaves lots of room for alternate approaches to solving a problem. :)

So, for that approach, you'll need to declare an array of Rectangles.
And then when it comes time to create a new Ball (the stuff that happens inside that particular if statement), you'll want to do something like (not saying this is all the code you'll need, but this code will handle the portion we're talking about now):

[source lang="java"]Ball newBall = new Ball(newX, newY, 20, 20);
randomBalls.add(newBall);
randomBallRectangles.add(newBall.getBallBounds());[/source]
You'll still need to update the mainBall's rectangle each time in the updateGame method because it moves so you need to get the most up to date location.

Then you'll want to put together a loop that compares the mainBallRectangle against each rectangle in the randomBallRectangles array. And you'll want to do something to mark when a collision has happened.
0

Share this post


Link to post
Share on other sites
not really understanding how this works

[source lang="java"]BallRectangle.add(new Ball.getBallBounds(newX, newY, 20, 20));[/source]
the compiler is saying it cant be resolved to a type. what im trying to do is add the rectangle of the ball by calling that method, so is there something im missing?
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