Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


thread error


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
139 replies to this topic

#121 burnt_casadilla   Members   -  Reputation: 443

Like
0Likes
Like

Posted 14 August 2012 - 05:19 PM

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, 14 August 2012 - 05:22 PM.

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


Sponsor:

#122 j-locke   Members   -  Reputation: 825

Like
0Likes
Like

Posted 14 August 2012 - 08:21 PM

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 out of scope) 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, 14 August 2012 - 08:24 PM.


#123 burnt_casadilla   Members   -  Reputation: 443

Like
0Likes
Like

Posted 14 August 2012 - 08:27 PM

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, 14 August 2012 - 08:32 PM.

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


#124 j-locke   Members   -  Reputation: 825

Like
0Likes
Like

Posted 14 August 2012 - 08:53 PM

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.

#125 burnt_casadilla   Members   -  Reputation: 443

Like
0Likes
Like

Posted 15 August 2012 - 03:50 PM

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?

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


#126 burnt_casadilla   Members   -  Reputation: 443

Like
0Likes
Like

Posted 15 August 2012 - 03:54 PM

aha! created a new arraylist called BallREctangle

[source lang="java"]public ArrayList<Rectangle> BallRectangle;[/source]

then in the updategame method if loop...

[source lang="java"]BallRectangle.add(new Rectangle(newX, newY, 20, 20));[/source]

does this work for what i want?

scratch that

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

right below the checkCollision(); in the update game method

now i just need the for statement in my checkCollision method

after running my program with the new for loop it freezes when the first ball is created

this is in my checkcollision method

[source lang="java"] if(mainBallRectangle.intersects(BallRectangle.get(i))) { mainBall.xspeed = 0; mainBall.yspeed = 0; mainBall.xpos = 100; mainBall.ypos = 100; }[/source]

now the game doesnt even run lol

Edited by burnt_casadilla, 15 August 2012 - 04:25 PM.

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


#127 j-locke   Members   -  Reputation: 825

Like
0Likes
Like

Posted 15 August 2012 - 04:31 PM

does this work for what i want?

That does work for what you want. It would probably be a little better design to use your getBallBounds method, but they get you the same results in this case.

right below the checkCollision(); in the update game method

By doing this, you're making BallRectangle (variables should start with a lowercase letter ala ballRectangle) hold more and more rectangles. You're adding a new rectangle to it every time through the updateGame method (to get an idea of how often and fast that happens, add a debug statement at the beginning of the method that says something simple like "Running updateGame").

I think for the design approach you chose last night, a better approach is to go at it like you were earlier in that post.. add one rectangle every time you add one new ball.

Given what new code you've shown, I'm not sure why the program freezes without giving you any kind of error or anything. You'll have to show more code to get some thoughts on why that happens.

#128 burnt_casadilla   Members   -  Reputation: 443

Like
0Likes
Like

Posted 15 August 2012 - 04:33 PM

[source lang="java"] public void updateGame() { long currentTime = System.currentTimeMillis(); long elapsedTime = currentTime - lastUpdateTime; lastUpdateTime = currentTime; 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)); BallRectangle.add(new Rectangle(newX, newY, 20, 20)); timeSinceLastNewDot = 0; }[/source]

ok restart lol. this adds a new rectangle every time a new ball is created

woops forgot to make a new arraylist in the init method

[source lang="java"]BallRectangle = new ArrayList<Rectangle>();[/source]

[source lang="java"] public ArrayList<Rectangle> BallRectangle;[/source]

what is the reason for needing both of the lines of code? how come just the first line wont work?

Edited by burnt_casadilla, 15 August 2012 - 04:40 PM.

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


#129 j-locke   Members   -  Reputation: 825

Like
0Likes
Like

Posted 15 August 2012 - 05:20 PM

what is the reason for needing both of the lines of code? how come just the first line wont work?


Those 2 lines do 2 different things and you need both of them.

One of them (public ArrayList BallRectangle;) declares your variable. It gives it an access modifier (public), a data type (ArrayList) and a name (BallRectangle).
The other line (BallRectangle = new ArrayList();) assigns a value to your variable. It constructs an object (new ArrayList()) and then assigns the result of that statement (in this case, a newly reserved block of memory for an ArrayList) to the variable on the left side of the equal sign (BallRectangle).

If you declared the variable and then tried to use it without assigning a value to it, the variable would have its default value. For data types that are not primitive data types (primitive data types: http://docs.oracle.c.../datatypes.html), that default value is null. This is where something should come full circle for you and register as something you've definitely seen before... If you try to call a method or access a field on a null object, you get a NullPointerException. I know you've seen that exception once or twice. And that process is the reason.
Declare: public ArrayList ballList;
Initialize/Assign: ballList = new ArrayList();
Use: ballList.add(new Ball());

If you go straight from declaring to using, the Use line would throw a NullPointerException.

Edited by j-locke, 15 August 2012 - 05:25 PM.


#130 burnt_casadilla   Members   -  Reputation: 443

Like
0Likes
Like

Posted 15 August 2012 - 05:37 PM

well what do you know my program runs fine after adding that line. thanks for the explanation. i understand variable types, but i guess i didnt think about the arraylist as a variable.

[source lang="java"]public void checkCollision() { if(mainBallRectangle.intersects(fixedBallRectangle)) { mainBall.xspeed = 0; mainBall.yspeed = 0; mainBall.xpos = 100; mainBall.ypos = 100; } if(mainBallRectangle.intersects(BallRectangle.get(i))) { mainBall.xspeed = 0; mainBall.yspeed = 0; mainBall.xpos = 100; mainBall.ypos = 100; }[/source]

when i run the program with this it gives me an error. am i not calling the arraylist BallRectangle the right way?

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


#131 j-locke   Members   -  Reputation: 825

Like
0Likes
Like

Posted 15 August 2012 - 05:55 PM

What's the error and what line?

Also, what do you intend for the 'i' in .get(i) to be/mean?

#132 burnt_casadilla   Members   -  Reputation: 443

Like
0Likes
Like

Posted 15 August 2012 - 05:58 PM

the error is at if(mainBallRectangle.intersects(BallRectangle.get(i)))

i is supposed to be the number of rectangles stored in the array BallRectangle. but now that i type it out it makes more sense to put that in a for loop

[source lang="java"]for(int i = 0; i < BallRectangle.size(); i++) if(mainBallRectangle.intersects(BallRectangle.get(i))) { mainBall.xspeed = 0; mainBall.yspeed = 0; mainBall.xpos = 100; mainBall.ypos = 100; }[/source]

HOLY SHIT I WIN. Game complete :'D

Edited by burnt_casadilla, 15 August 2012 - 06:01 PM.

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


#133 j-locke   Members   -  Reputation: 825

Like
0Likes
Like

Posted 15 August 2012 - 06:39 PM

Congrats! :-D

Woot woot

#134 burnt_casadilla   Members   -  Reputation: 443

Like
0Likes
Like

Posted 15 August 2012 - 06:43 PM

yay!!! now for snake lol

1. add a ball that deletes itself everytime mainBall intersects it. im just going to use fixedBallRectangle and mainBall for this.
2. somehow make the snake bigger... add another ball that follows the mainBall?

ok so i have one arraylist just to store the length of the snake. the rest should be relatively easy, drawing a new apple for the snake to eat and check collision and if the snake comes in contact with the apple i delete the apple and draw a new one at a random point

how do you remove an object drawn to the screen?

Edited by burnt_casadilla, 15 August 2012 - 07:18 PM.

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


#135 j-locke   Members   -  Reputation: 825

Like
0Likes
Like

Posted 15 August 2012 - 07:25 PM

Sounds like a strategy that can work. Good job setting up an approach of getting a little bit working then getting a little more working and so on.

#136 burnt_casadilla   Members   -  Reputation: 443

Like
0Likes
Like

Posted 15 August 2012 - 07:32 PM

thank you for all your help :) i think i can do this one by myself!!

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


#137 j-locke   Members   -  Reputation: 825

Like
0Likes
Like

Posted 15 August 2012 - 07:42 PM

Awesome. Knock it out of the park! And feel free to make a thread to show off your work. Your fellow GameDevers enjoy trying out completed projects and throwing out (usually challenging) suggestions of other things to do/add.

#138 burnt_casadilla   Members   -  Reputation: 443

Like
0Likes
Like

Posted 15 August 2012 - 07:45 PM

aw man one last question. i have a mainBall and a randomApple and im testing collision for them. every time i hit the randomApple with the mainBall it adds another main ball to my arrayList MainBall. The only problem is that it adds 4 balls to my mainball arraylist instead of just one. how do i fix that?

[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 snake 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 ArrayList<Ball> SnakeSize; static final int NUM_OF_BALLS = 4; int i; int t; Rectangle randomAppleRectangle; Rectangle BallRectangle; Ball Ball = new Ball(100, 100, 10, 10, 0, 0); Apple randomApple = new Apple(250, 250, 15, 15); public void start() { Thread th = new Thread(this); th.start();//start main game } public void updateGame() { randomAppleRectangle = getAppleBounds(); BallRectangle = getBallBounds(); checkCollision(); Ball.ypos += Ball.yspeed; Ball.xpos += Ball.xspeed; if (Ball.xpos < 1) { Ball.xpos = 449; } if (Ball.xpos > 449) { Ball.xpos = 1; } if (Ball.ypos < 1) { Ball.ypos = 449; } if (Ball.ypos > 449) { Ball.ypos = 1; } try { Thread.sleep(20); } catch(InterruptedException ex){} } 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 class Ball { int width; int height; int xpos; int ypos; int xspeed; int yspeed; public Ball( 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 paintBall(Graphics g) { g.setColor(Color.black); g.fillOval(Ball.xpos, Ball.ypos, width, height); g.drawString(xpos + ", " + ypos, 20, 40); } }//mainBall public class Apple { int x; int y; int width; int height; public Apple(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.red); g.fillOval(x, y, width, height); g.drawString("Apple printed at: " + randomApple.x + ", " + randomApple.y, 20, 60); g.drawString("Size of snake: " + SnakeSize.size(), 20, 80); } //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) { Ball.xspeed = -5; Ball.yspeed = 0; } if(key == Event.RIGHT) { Ball.xspeed = 5; Ball.yspeed = 0; } if(key == Event.UP) { Ball.yspeed = -5; Ball.xspeed = 0; } if(key == Event.DOWN) { Ball.yspeed = 5; Ball.xspeed = 0; } return true; } public void run() { while(true) { repaint(); } } public void init() { this.setSize(WIDTH, HEIGHT); SnakeSize = new ArrayList<Ball>(); } public void paint(Graphics g) { updateGame(); g.drawString("time: " + t, 20, 20); Ball.paintBall(g); randomApple.paint(g); } public Rectangle getBallBounds() { return new Rectangle(Ball.xpos, Ball.ypos, 10, 10); } public Rectangle getAppleBounds() { return new Rectangle(250, 250, 15, 15); } public void checkCollision() { if(BallRectangle.intersects(randomAppleRectangle)) { SnakeSize.add(new Ball(Ball.xpos - 10, Ball.ypos - 10, 10, 10, 0, 0)); } }}[/source]

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


#139 j-locke   Members   -  Reputation: 825

Like
0Likes
Like

Posted 15 August 2012 - 07:55 PM

I don't see where you're getting rid of the random apple after they collide. With that being the case, it seems very likely that the ball and apple are colliding so one gets added. Then next time through updateGame, they are still colliding so another gets added. And so on. If you get rid of that random apple after the collision(likely by making your one random apple be somewhere else), that should solve that.

#140 burnt_casadilla   Members   -  Reputation: 443

Like
0Likes
Like

Posted 15 August 2012 - 07:59 PM

[source lang="java"]public void checkCollision() { if(BallRectangle.intersects(randomAppleRectangle)) { randomApple = new Apple(randomNumber(), randomNumber(), 15, 15); SnakeSize.add(new Ball(Ball.xpos - 10, Ball.ypos - 12, 12, 10, 0, 0)); } }[/source]

perfect. once again, thank you so much :D

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS