Jump to content

  • Log In with Google      Sign In   
  • Create Account

removing objects from an array list


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
5 replies to this topic

#1 burnt_casadilla   Members   -  Reputation: 442

Like
0Likes
Like

Posted 01 September 2012 - 06:58 PM

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 :)


Sponsor:

#2 Cornstalks   Crossbones+   -  Reputation: 6985

Like
0Likes
Like

Posted 01 September 2012 - 07:02 PM

What's your problem? BRICKBOUNDS.remove(i) will remove the ith item.

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.

[ 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 ]

#3 burnt_casadilla   Members   -  Reputation: 442

Like
0Likes
Like

Posted 01 September 2012 - 07:04 PM

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 :)


#4 burnt_casadilla   Members   -  Reputation: 442

Like
0Likes
Like

Posted 01 September 2012 - 07:14 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 :P

Edited by burnt_casadilla, 01 September 2012 - 08:21 PM.

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


#5 Cornstalks   Crossbones+   -  Reputation: 6985

Like
0Likes
Like

Posted 01 September 2012 - 10:29 PM

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, 01 September 2012 - 10:30 PM.

[ 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 ]

#6 Aldacron   GDNet+   -  Reputation: 3177

Like
0Likes
Like

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 Posted Image


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.





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