VG:Power Ups and Enemy Movement

From MyProjectFun
Jump to: navigation, search

Adding power-ups is similar to firing bullets - we just drop the power-up before an enemy is defeated! Under the function that is getting the enemy killed (i.e. CollisionWithBulletFN), we just copy a power-up sprite before the enemy is deleted. Then, the player just needs to check if he collides with that power-up, and if he does, we activate it (I suggest adding setting a timer to some value that will get decremented each frame).

As for stopping all of the enemies, they would all have to have if-checks in their behaviors to see if the player still had time on his power-up. If he does, then we want to just return from the function without doing anything.

For enemy movement, it varies greatly on the effects.

To be a bit more clear, let me use an example. I need to first of all make my power-up art and animation. Then I make the actor for my power-up in MyGame.cs under the CreateResources function. Next, I make my master power-up sprite in my level initialization (which I make sure is unused so I can copy it).

Now I'm ready to spawn a power-up in CollisionWithBulletFN. The algorithm looks kind of like this.

// If I collide with a bullet
if (This.Sprite.CollisionWithSprite("Bullet"))
  #region Spawning a power-up
  // Find the power-up master
  // Copy the power-up master
  // Place it on this sprite's position
  // Make it used
  // Explode and die

A shield power-up could do something like have a timer for how long it sticks around, and, while it's around, delete bullets that are trying to hurt the player.

You need some public static int outside of the function to act as the shield's timer (which could be set in other functions, like when the player picks up the power-up).

This is all assuming that your shield sprite is larger than your player, so that enemy bullets will collide with it before they hit the player.

So here's about how that would work - this function would be attached to the player by the way.

// Find our shield sprite
Sprite Shield = This.Game.FindSprite("Shield");
// If the shield timer is greater than zero
if ( ShieldTimer > 0 )
  // Set the shield's center to our center
  float x = This.Sprite.Position.X + (This.Sprite.Frame.Width / 2.0f) - (Shield.Frame.Width / 2.0f);
  float y = This.Sprite.Position.Y + (This.Sprite.Frame.Height / 2.0f) - (Shield.Frame.Height / 2.0f);
  Shield.Position = new Point2D( x , y );
  // Make sure the shield is used
  Shield.Unused = false;
  // Decrement our shield timer
// Otherwise
  // Make the shield sprite unused
  Shield.Unused = true;

That would get the shield to follow you. It also turns the shield on when you have some time left on your shield and turns it off when you run out of time.

However, you also need to write a behavior for your shield that will kill incoming bullets. That, I'll leave to you.

For your monster, we need something that's a bit complicated. I'm going to focus on movement in this post.

We need two public static variables.

public static Point2D Center = new Point2D( This.Game.ViewportSize.Width / 2.0f , 100 );
public static int Theta = 0;

What we're going to do each frame is set the our monster's position to the Center, adding sin(Theta) to the x: This will make your monster move back and forth in a smooth manner. So, for your monster's movement function, you want to do something like this:

// Calculate our x and y for our position
float x = Center.X + Math.sin( Theta );
float y = Center.Y;
// Set our position
This.Sprite.Position = new Point2D( x , y );
// If Theta is 360 after incrementing it
if ( ++Theta == 360 )
  // Reset it to 0
  Theta = 0;

That's it for the movement stuff.

For shooting at the player, let's fire a bullet every half second (30 frames). We need some sort of timer for this, but we have that Theta from the movement function, and it's already being incremented each frame. Good news!

However, to check for each 30 frames, we need to see if Theta is divisible by 30. For that, we have the modulo operator (looks like %, which can be typed with Shift+5). The modulo operator gives us the remainder after division, so Theta % 30 means that Theta is divisible by 30 exactly.

So for firing...

// If half a second has passed
if ( Theta % 30 == 0 )
  // Find the bullet we want to copy
  // Copy it
  // Give our copy a new name
  // Put our copy at our position
  // Make the copy used
  // Find the player
  // Tell the copy to follow the player
  // ex. BulletCopy.Follow( Player , 5.0f , 0.0f);

Finally, we need to respawn our monster. That should be very similar to how we spawn the eye or snake. Just make sure that the timer is the length you want it!

Personal tools
Game Design
Multimedia Production
VG Programming