ok so the getBounds method will return an invisible rectangle around each ball created in the array but how would i pass each ball through the getBounds method? would i need another for loop and call the method when another ball is created in the array list? i think once i figure out how to create the rectangle around the balls the intersects method would be a piece of cake.
this might not make sense but what if i created another arraylist designated for the rectangles around the balls and then used that arraylist in the intersects method? its easier for me to understand but it might not be the easiet way
thread error
oh and none of my objects implements the Ellipse2d.float class
im going to try to create a Rectangle around the main ball first to see if i can get that to work first
another thought: im thinking about making a simple class for the main ball so i can just call that method inside the getBounds method. would that work?
im going to try to create a Rectangle around the main ball first to see if i can get that to work first
another thought: im thinking about making a simple class for the main ball so i can just call that method inside the getBounds method. would that work?
so this is my main ball class
[source lang="java"]class mainBall
{
int x;
int y;
int width;
int height;
int xpos = 100;
int ypos = 100;
int radius = 5;
int xspeed = 0;
int yspeed = 0;
public mainBall(int x, int y, int width, int height)
{
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 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;
}
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){}
return true;
}
public void paint(Graphics g)
{
g.setColor(Color.black);
g.fillOval(x, y, width, height);
g.drawString(xpos + ", " + ypos, 20, 40);
}
}//mainBall[/source]
but i cant figure out how to draw it in the main game class. ive tried putting mainBall.paint(g); in the main paint method but that gives me an error
sorry about all the questions lol. my brain is going way too fast and i cant slow down
[source lang="java"]class mainBall
{
int x;
int y;
int width;
int height;
int xpos = 100;
int ypos = 100;
int radius = 5;
int xspeed = 0;
int yspeed = 0;
public mainBall(int x, int y, int width, int height)
{
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 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;
}
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){}
return true;
}
public void paint(Graphics g)
{
g.setColor(Color.black);
g.fillOval(x, y, width, height);
g.drawString(xpos + ", " + ypos, 20, 40);
}
}//mainBall[/source]
but i cant figure out how to draw it in the main game class. ive tried putting mainBall.paint(g); in the main paint method but that gives me an error
sorry about all the questions lol. my brain is going way too fast and i cant slow down
oh and none of my objects implements the Ellipse2d.float class
Oh, sorry, I didn't catch that your Ball class changed so it doesn't anymore.
im going to try to create a Rectangle around the main ball first to see if i can get that to work first
That is a good idea. Write a method named whatever you think is appropriate that returns that invisible rectangle. If you make that an object of the Rectangle class then the Rectangle class will make it quite easy to handle the intersections.
another thought: im thinking about making a simple class for the main ball so i can just call that method inside the getBounds method. would that work?
Yep, that's a great reason that the main ball should be a class. So you can add functionality to it like that.
this might not make sense but what if i created another arraylist designated for the rectangles around the balls and then used that arraylist in the intersects method? its easier for me to understand but it might not be the easiet way
That approach would work. I think a more solid approach would be to add a method to your Ball class that returns a Rectangle. Then you could use your existing array to get Rectangles.
ok so the getBounds method will return an invisible rectangle around each ball created in the array but how would i pass each ball through the getBounds method? would i need another for loop and call the method when another ball is created in the array list?
As you've pointed out, you didn't inherit a getBounds method as you don't extend Ellipse2D but once you write a similar method, yes you would have a for loop iterate through the list of Balls and check if the player intersects with any of them (if (playerRectangle.intersects(enemyRectangle)).
but i cant figure out how to draw it in the main game class. ive tried putting mainBall.paint(g); in the main paint method but that gives me an error
sorry about all the questions lol. my brain is going way too fast and i cant slow down
I think it is a good time to start following the usual naming conventions for stuff (it helps a lot when all the people looking at the code can quickly know what's going on).
The basics of that would be to name:
classes start with a capital letter and have a capital letter at the start of each word (public class ThisIsAClass).
variables start with a lowercase letter and have a capital letter at the start of each word (int whatALongVariableName).
methods are named like variables (public void cantWeAllJustGetAlong()).
So with that tidbit out of the way, as always, you'll want to post what error you get and what line of code it happens on whenever you code is giving you an error.
As a guess, perhaps your mainBall object isn't named mainBall. I can confirm that that is your class name. So if you have a variable declared mainBall player, you'd want to call player.paint(g).
My main ball class is now MainBall with the constructor also called MainBall(x, y, width, height). the paint method is public void paintMainBall(Graphics g). How would i call the paintMainBall method in the main paint method? i feel like this should be really simple...
Cannot make a static reference to the non-static method paintMainBall(Graphics) from the type game.MainBall
it gives me this error when i type MainBall.paintMainBall(g); in the main paint method
Cannot make a static reference to the non-static method paintMainBall(Graphics) from the type game.MainBall
it gives me this error when i type MainBall.paintMainBall(g); in the main paint method
Nice. That makes it a little more straightforward to look at.
In your game class, you have to declare a MainBall object (MainBall mainBall;). It also needs initializing somewhere but I'll leave that to you. With that set up, in your game class' paint method, you'd call mainBall.paint(g). Or mainBall.paintMainBall(g) per your newest name.
In your game class, you have to declare a MainBall object (MainBall mainBall;). It also needs initializing somewhere but I'll leave that to you. With that set up, in your game class' paint method, you'd call mainBall.paint(g). Or mainBall.paintMainBall(g) per your newest name.
I made the mainBall object based on my MainBall class and put mainBall.paintMainBall(g) in my paint method and now it gives me another error:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at game.paint(game.java:251)
at game.update(game.java:227)
so its where i put the mainBall.paintMainBall(g) but the compiler doesnt see anything wrong with it
herpderp i forgot to create the object
MainBall mainBall = new MainBall(x, y, width, height);
that worked fine, but now it wont move. good lord this is frusterating
fixed it so that the coordinates of the mainBall change when i press the keys but it isnt being redrawn so the ball stays in one spot. almost!!!!
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at game.paint(game.java:251)
at game.update(game.java:227)
so its where i put the mainBall.paintMainBall(g) but the compiler doesnt see anything wrong with it
herpderp i forgot to create the object
MainBall mainBall = new MainBall(x, y, width, height);
that worked fine, but now it wont move. good lord this is frusterating
fixed it so that the coordinates of the mainBall change when i press the keys but it isnt being redrawn so the ball stays in one spot. almost!!!!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement