Question

I'm creating a 2D shooter and I would like to increase the size of certain enemies when collision occurs between that and a projectile object. I can scale the sprite in the code by just changing the float value below

enemyAnimation.Initialize(enemyTexture, Vector2.Zero, 47, 61, 8, 30, Color.White, **2f**, true);

I can then change that float to a variable, and update the variable when a collision occurs. The only problem is, that the enemy scale does change but only for newly spawned enemies and all of them. This was obvious but what I want to be able to do (if at all possible) is change the size of the enemy that the collision occurred with and only that enemy...

Any ideas on how I might go about doing this?

EDIT:

Okay, I am a little confused. I have created a float value called scaleSize in my Enemy class. Then in collision detection in my Game1.cs I am running a for loop to iterate through my list of enemy objects

// Projectile vs Enemy Collision
            for (int i = 0; i < projectiles.Count; i++)
            {
                for (int j = 0; j < enemies.Count; j++)
                {
                    // Create the rectangles we need to determine if we collided with each other


                    // Determine if the two objects collided with each other
                    if (rectangle1.Intersects(rectangle2))
                    {

                        enemies[j].Health -= projectiles[i].Damage;
                        projectiles[i].Active = false;

                            *enemies[j].scaleSize += 1f;*
                    }
                }
            }

Now I realise scaleSize is not being initialized properly. But I am unsure of where I should change this. As above in the earlier code sample, the float value is changing the scale of the enemy but through the Animation class and if I change this, it changes for all enemies (afaik). I am sorry as I know this is very vague but I just can't wrap my head around where I should set the enemies scale size. creating it enemy class is fine but it is not referencing the actual scale of the enemy.

Was it helpful?

Solution

Store the scale value in your Enemy class. That way, each instance of your Enemy objects will have their own scale value.

If you do not have an Enemy class, you should create one. You can also store the enemyTexture, and have each Enemy have their own appearance.

Edit

You most likely are not using the proper scale value in your Draw method. Take an example Enemy class:

class Enemy
{       
   protected float Scale { get; set; }
   protected Texture2D Texture { get; set; }
   protected Vector2 Position { get; set; }

   public Enemy()
   { 
       Scale = 1f;
   }

   public void Hit()   
   {
       Scale = 2f;
   }

   public void Draw(SpriteBatch spriteBatch)
   {
        spriteBatch.Draw(Texture, Position, null, Color.White, 0, Vector2.Zero, Scale, SpriteEffects.None, 1);   
   }
}

Usage examples:

Enemy A = new Enemy();      
A.Hit();    
A.Draw(spriteBatch);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top