Adding Objects More Than To ArrayList
I'm developing a simple shooter game in XNA. I'm adding the copy of the object to an array list whenever it hits an hostile. The problem is, the object is added 2 times.
Here is the code:
--> Player fires spheres.
--> s is fired sphere
--> sp is copy of s
--> a sphere is drawn iff it's active
if(s.isCollided)
{
s.isCollided = false;
Sphere sp = new Sphere(s.texture, new Vector2(s.dest_rectangle.X,s.dest_rectangle.Y), s.speed);
s.dest_rectangle = new Rectangle((int)s.position.X, (int)s.position.Y, s.texture.Width, s.texture.Height);
sp.isActive = tru开发者_Python百科e;
sp.isCollided = false;
collidedSpheres.Add(sp);
}
It's possible that s
is colliding with your hostile during two "frames".
You could try adding this line to the end of your function: spheres.Remove(s);
.
Do the duplicate spheres have the same position?
精彩评论