Question

In my pong game I'm making I have two paddles and a ball. I'm trying to make so when the ball collides with the paddle, an effect/animation is shown. I have a spritehseet and a working animation. I use this to detect when the animation plays (please notice that I havent fixed the position of the effect yet, so it plays on 400, 300 just to see if it works)

public bool BallHitEffect()
    {
        if (gPaddle.gpRect.Intersects(ball.ballRect))
        {
            return true;
        }
        else { return false; }

And in my Draw:

protected override void Draw(GameTime gameTime)
{
   spriteBatch.Begin();
   if (BallHitEffect())
   {
      animatedSprite.Draw(spriteBatch, new Vector2(400, 250));
   }
}

Now it appears when it collide, but only for a short millisecond, because when the ball leaves the paddle, it disappears. I'm aware that it's coded to only appear when it is colliding with the paddle, but in what way could I possibly make it only disappear only when it's animation ends?

Was it helpful?

Solution

You can easily used elapsed time and some makeshift timers to do this. I assume your spritesheet is a series of frames, and that you already have rendering set up.

You will need some variables to start out:

double frameTimePlayed; //Amount of time (out of animationTime) that the animation has been playing for
bool IsPlaying; //Self-Explanitory
int frameCount; //Frames in your sprite, I'm sure you already handle this in your animation logic, but I just threw it in here
int animationTime = 1000; //For 1 second of animation.

In your update method you need to

  1. Check if the ball is intersecting, and set IsPlaying to true
  2. If the animation IsPlaying, increment the frameTimePlayed by the elapsed time
  3. If the frameTimePlayed is equal to or greater than the animationTime, stop the animation by setting IsPlaying to false

In your draw method you need to

  1. Draw the animation, if IsPlaying

Here is some example code:

protected override void Update(GameTime gameTime)
{
    //If it is not already playing and there is collision, start playing
    if (!IsPlaying && BallHitEffect)
        IsPlaying = true;
    //Increment the frameTimePlayed by the time (in milliseconds) since the last frame
    if (IsPlaying)
        frameTimePlayed += gameTime.ElapsedGameTime.TotalMilliseconds;
    //If playing and we have not exceeded the time limit
    if (IsPlaying && frameTimePlayed < animationTime)
    {
         // TODO: Add update logic here, such as animation.Update()
         // And increment your frames (Using division to figure out how many frames per second)
    }
    //If exceeded time, reset variables and stop playing
    else if (IsPlaying && frameTimePlayed >= animationTime)
    {
        frameTimePlayed = 0;
        IsPlaying = false;
        // TODO: Possibly custom animation.Stop(), depending on your animation class
    }
}

And for drawing, pretty easy, just check is IsPlaying and draw if that is the case.

I made sure to comment the code good, so hopefully this will help you understand and work better.

You could also use a double and use TotalSeconds instead of milliseconds if needed, and calculate elapsed time with float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top