#81 Members - Reputation: 265
Posted 12 August 2012 - 03:27 PM
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
#82 Members - Reputation: 265
Posted 12 August 2012 - 03:31 PM
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?
Edited by burnt_casadilla, 12 August 2012 - 03:34 PM.
#83 Members - Reputation: 265
Posted 12 August 2012 - 03:49 PM
[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
#84 Members - Reputation: 555
Posted 12 August 2012 - 04:09 PM
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)).
#85 Members - Reputation: 555
Posted 12 August 2012 - 04:21 PM
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).
Edited by j-locke, 12 August 2012 - 04:23 PM.
#86 Members - Reputation: 265
Posted 12 August 2012 - 04:30 PM
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
Edited by burnt_casadilla, 12 August 2012 - 04:37 PM.
#87 Members - Reputation: 555
Posted 12 August 2012 - 04:36 PM
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.
#88 Members - Reputation: 265
Posted 12 August 2012 - 04:44 PM
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!!!!
Edited by burnt_casadilla, 12 August 2012 - 05:22 PM.
#91 Members - Reputation: 555
Posted 12 August 2012 - 05:43 PM
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.
#92 Members - Reputation: 265
Posted 12 August 2012 - 05:50 PM
[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]
Edited by burnt_casadilla, 12 August 2012 - 06:06 PM.
#93 Members - Reputation: 555
Posted 12 August 2012 - 06:32 PM
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.
#94 Members - Reputation: 265
Posted 12 August 2012 - 06:52 PM
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
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?
}
Edited by burnt_casadilla, 12 August 2012 - 07:15 PM.
#95 Members - Reputation: 555
Posted 12 August 2012 - 07:23 PM
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.i changed the variables in the paintMainBall method to xpos and ypos and it worked
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.
#96 Members - Reputation: 265
Posted 12 August 2012 - 07:39 PM
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]
#97 Members - Reputation: 265
Posted 12 August 2012 - 07:48 PM
[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?
Edited by burnt_casadilla, 12 August 2012 - 08:04 PM.
#98 Members - Reputation: 555
Posted 12 August 2012 - 08:09 PM
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.
#99 Members - Reputation: 265
Posted 12 August 2012 - 08:15 PM
[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]






