• Advertisement
Sign in to follow this  

removing objects from an array list

This topic is 1968 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

in this brickbreaker type game, i need to figure out how to remove and object from an array list when the ball hits a brick

[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

Share this post


Link to post
Share on other sites
Advertisement
What's your problem? [font=courier new,courier,monospace]BRICKBOUNDS.remove(i)[/font] will remove the [font=courier new,courier,monospace]i[/font]th item.

Or perhaps you're wondering how to do it while still iterating over each item? In which case, don't increment [font=courier new,courier,monospace]i[/font] 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

Share this post


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

Share this post


Link to post
Share on other sites
If you post the actual error message, that would help a lot, as it has some extra details you've left out. Is it perhaps complaining about you accessing xbrick, ybrick, widthbrick, and heightbrick? Because if Brick is a class, those members have to be static for you to access them *without* an object. If you want them to be normal members, you have to use an *existing Brick object*. Maybe read up on instance and class members would help.

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

Share this post


Link to post
Share on other sites

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 tongue.png


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement