#1 Members - Reputation: 235
Posted 01 September 2012 - 06:58 PM
[source lang="java"] public void checkCollision() { if(ballRectangle.intersects(paddleRectangle)) { ball.yspeed = -ball.yspeed; } for(int i = 0; i < BRICKBOUNDS.size(); i++) { if(ballRectangle.intersects(BRICKBOUNDS.get(i))) { ball.yspeed = -ball.yspeed; } } }[/source]
i have no idea how to go about this
#2 Moderator* - Reputation: 5362
Posted 01 September 2012 - 07:02 PM
Or perhaps you're wondering how to do it while still iterating over each item? In which case, don't increment i when you remove (something like this):
for(int i = 0; i < BRICKBOUNDS.size();)
{
if(ballRectangle.intersects(BRICKBOUNDS.get(i)))
{
ball.yspeed = -ball.yspeed;
BRICKBOUNDS.remove(i);
}
else
{
++i;
}
}
Edited by Cornstalks, 01 September 2012 - 07:04 PM.
#4 Members - Reputation: 235
Posted 01 September 2012 - 07:14 PM
[source lang="java"] public Rectangle getBrickBounds() { for(int i = 0; i < BRICKS.size(); i++) { BRICKBOUNDS.add(new Rectangle(Brick.xbrick, Brick.ybrick, Brick.widthbrick, Brick.heightbrick)); } }[/source]
im trying to return a rectangle for each Brick in my BRICKS array for collision, but for each variable it says that it cannot make a static reference to a non static variable. i have no idea what that means
Edited by burnt_casadilla, 01 September 2012 - 08:21 PM.
#5 Moderator* - Reputation: 5362
Posted 01 September 2012 - 10:29 PM
Also, that function never returns anything, so it should be complaining about that. You'll need to actually return a Rectangle from it at some point (or make it void if you're not going to return anything).
Edited by Cornstalks, 01 September 2012 - 10:30 PM.
#6 Members - Reputation: 1804
Posted 02 September 2012 - 07:36 PM
another issue...
[source lang="java"] public Rectangle getBrickBounds() { for(int i = 0; i < BRICKS.size(); i++) { BRICKBOUNDS.add(new Rectangle(Brick.xbrick, Brick.ybrick, Brick.widthbrick, Brick.heightbrick)); } }[/source]
im trying to return a rectangle for each Brick in my BRICKS array for collision, but for each variable it says that it cannot make a static reference to a non static variable. i have no idea what that means
You aren't accessing variables. You're trying to access members using the Brick class itself, rather than any instances of it.
for(int i = 0; i < BRICKS.size(); i++)
{
// Get a reference to the Brick instance at index i in the array list.
Brick b = BRICKS.get(i);
// Use the Brick reference to get the values for the rectangle.
BRICKBOUNDS.add(new Rectangle(b.xbrick, b.ybrick, b.widthbrick, b.heightbrick));
}
The other issue is your return value. You declare the method to return a single Rectangle, but you say you want to return a Rectangle for each brick. And then you aren't returning anything at all. If this method is being called externally, then you probably want to be returning BRICKBOUNDS. Meaning, you need to declare the function to return ArrayList<Rectangle> (or List<Rectangle>) rather than just Rectangle. Then the function becomes:
public ArrayList<Rectangle> getBrickBounds()
{
for(int i = 0; i < BRICKS.size(); i++)
{
// Get a reference to the Brick instance at index i in the array list.
Brick b = BRICKS.get(i);
// Use the Brick reference to get the values for the rectangle.
BRICKBOUNDS.add(new Rectangle(b.xbrick, b.ybrick, b.widthbrick, b.heightbrick));
}
return BRICKBOUNDS;
}
And a piece of advice: if you call this method a lot, it can be really inefficient to create a new Rectangle for each brick every time you call it. It might be a better strategy to replace the xbrick, ybrick, widthbrick and heightbrick members of the Brick class with a Rectangle called bounds. Then you can add methods, if you need them, like getX, getY, getWidth, getHeight, and getBounds that return the appropriate values. You can add each Rectangle to BRICKBOUNDS when a new Brick is created. Then, the only time you need to modify the list is when a brick is destroyed -- just remove its rectangle from the list. At that point, getBrickBounds becomes a single line:
ArrayList<Rectangle> getBrickBounds()
{
return BRICKBOUNDS;
}
Edited by Aldacron, 02 September 2012 - 07:42 PM.






