removing objects from an array list

Started by
4 comments, last by Aldacron 11 years, 7 months ago
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

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

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;
}
}
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Perfect! i had it the other way around

remove.BRICKBOUNDS.get(i);

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

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

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

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).
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

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;
}

This topic is closed to new replies.

Advertisement