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) 
        screenPosition.X -= velocity;


    public void Draw(SpriteBatch spriteBatch) 

    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.

  • \$\begingroup\$ I don't understand. You know exactly what you want to do, you've mentioned a way of doing it, and you don't want someone to write the code for you. Why aren't you trying to implement it the way you think it should be done? This doesn't seem like a gamedev question, it seems like a programming question. \$\endgroup\$
    – House
    Aug 15 '14 at 14:16
  • \$\begingroup\$ @Byte56 Because I am looking for the smartest and cleanest solution ;) \$\endgroup\$ Aug 15 '14 at 14:19
  • \$\begingroup\$ Please define "smartest and cleanest" ;) In this case there's practically no room for smart/clean optimizations - you just code in 30sec it and that's it. \$\endgroup\$
    – Kromster
    Aug 15 '14 at 14:21
  • \$\begingroup\$ Just as an example. Simply out of unknowing I defined all velocity variables in my project as int. Maybe it would have been smarter to make'em float or Vector2. I'm inexperienced ;) \$\endgroup\$ Aug 15 '14 at 14:24
  • \$\begingroup\$ Oh cmon dat downvote, really? \$\endgroup\$ Aug 15 '14 at 14:25

What you have now is essentially:

screenPosition.X -= velocity * 1.0;
screenPosition.Y -= velocity * 0.0;

You need to explicitly add a Direction variable which is a 2D vector (you are dealing with 2D, right?).

Direction = (1.0, 0.0);

You might be already seeing my lead. You need to scale Velocity by Direction and add to your Position like so:

screenPosition.X -= velocity * Direction.X;
screenPosition.Y -= velocity * Direction.Y;

Adding parameter to a constructor and a new field to the class is trivial.

P.S. I don't think there's anything smarter than simplicity in this case ;)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.