Sign in to follow this  

How to modify this Projectile class?

This topic is 1250 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I have the following class which Updates and Draws a projectile:

public class TierOneProjectile : IProjectile
{
    private AnimatedTexture animation;
    private Vector2 screenPosition, framesPerSecond, origin;
    private float rotation, scale, depth;
    private int velocity;

    public TierOneProjectile(Vector2 _screenPosition,int _velocity,AnimatedTexture _animation)
    {
        screenPosition = _screenPosition;
        animation = _animation;
        velocity = _velocity;
    }

    public void Update(float elapsedGameTime) 
    {
        animation.UpdateFrame(elapsedGameTime);
        screenPosition.X -= velocity;

    }

    public void Draw(SpriteBatch spriteBatch) 
    {
        animation.DrawFrame(spriteBatch,screenPosition);
    }

    public Rectangle getBoundingBox() 
    {
        return new Rectangle((int)screenPosition.X, (int)screenPosition.Y, (int)animation.GetTextureWH().X, (int)animation.GetTextureWH().Y);
    }
}

Currently the projectile alway flies to the left. Little did I know when I created this class. Now I want to pass a direction to the constructor so every projectile could fly into a different direction. What would be the smartest solution for this? I do not want you to write the code for me. But maybe you could give me a hint how this could be done.

Share this post


Link to post
Share on other sites

Change _velocity to a Vector2 and change the update function to consider velocity.x for the x component and velocity.y for the y component of the position. Also, always add the value for x, and set the direction with the sign of velocity (for the Y value you'll have to check with your current direction of Y)

 

And one more thing, it's a bit confusing adding a velocity to a position. I'd do something like getting a distance value (velocity.x * elapsedGameTime) and adding that value to the current position. This way you're actually using it as a speed, if one update takes longer than the previous one the projectile will move a greater distance to compensate. This means the movement doesn't depend on the frame rate of your game and you should aim for that, the projectile will move the same distance in the same amount of time in any PC or in any state of the development.

Edited by DiegoSLTS

Share this post


Link to post
Share on other sites

This topic is 1250 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this