Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9191623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:43:45+00:00 2026-06-17T20:43:45+00:00

I have 3 classes: – Game1 (main class) – Entity (base entity class) –

  • 0

I have 3 classes:
– Game1 (main class)
– Entity (base entity class)
– Player (player class, extends Entity class)

I draw the player class, but after that I can’t seem to change the position of the player object.

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    //Create a list with all the entities
    List<Entity> entityList = new List<Entity>();

    //Player
    Player player;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        base.Initialize();
    }

    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        //Create player
        Texture2D playertexture = Content.Load<Texture2D>("player/player");
        Rectangle playerrectangle = new Rectangle(10, 10, playertexture.Width, playertexture.Height);

        player = new Player(playertexture, playerrectangle);

        entityList.Add(player);

    }

    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            this.Exit();

        //Update players and entities
        player.Update(graphics.GraphicsDevice);

        base.Update(gameTime);
    }


    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();

        //Draw player
        player.Draw(spriteBatch);

        spriteBatch.End();

        base.Draw(gameTime);
    }
}

class Entity
{

    //Standard variables
    int health;
    int armor;
    float speed;
    float friction;

    //Get graphic and bounding box
    Texture2D texture;
    Rectangle rectangle;

    public Entity(Texture2D newTexture, Rectangle newRectangle){

        texture = newTexture;
        rectangle = newRectangle;

    }

    public void Update(GraphicsDevice graphics) {



    }

    public void Draw(SpriteBatch spriteBatch) {


    }

    /*
     * Modifiers for the variables
     */
    public void modifyHealth(int amount) { health = health + amount; }
    public void modifyArmor(int amount){ armor = armor + amount; }
    public void modifySpeed(float amount) { speed = speed + amount; }

    /*
     * Getters for variables
     */
    public int getHealth() { return health;  }
    public int getArmor() { return armor; }
    public float getSpeed() { return speed; }
    public float getFriction() { return friction; }

    /*
     * Setters for variables
     */
    public void setHealth(int amount) { health = amount; }
    public void setArmor(int amount) { armor = amount; }
    public void setSpeed(float amount) { speed = amount; }
    public void setFriction(float amount) { friction = amount; }

    /*
     * Functions
     */
    public void damage(int damage) {

        /*
         * Calculate health
         * 
         * Armor takes half the damage, if possible
         */
        if (damage / 2 >= armor) {
            damage = damage / 2;
            armor -= damage;
        } else if (armor > 0) {
            damage -= armor;
            armor = 0;
        }

        health -= damage;

        if(health <= 0){
            health = 0;
            //TODO Death
        }
    }
}

class Player : Entity
{

    //Create player
    Entity player;

    //Position and velocity
    Vector2 position;
    Vector2 velocity;

    //Texture and rectangle
    Texture2D texture;
    Rectangle rectangle;

    public Player(Texture2D newtexture, Rectangle newrectangle) : base(newtexture, newrectangle) {

        texture = newtexture;
        rectangle = newrectangle;

        //Set basic variables
        this.setHealth(100);
        this.setArmor(0);
        this.setSpeed(10);
        this.setFriction(1);

    }

    public void Update() {

        //Movement
        if(Keyboard.GetState().IsKeyDown(Keys.Right)){
            rectangle.X += 1;
        }

        rectangle.Y += 4;


    }

    public void Draw(SpriteBatch spriteBatch) {

        spriteBatch.Draw(texture, rectangle, Color.White);

    }
}

If there are also general mistakes I make, do point them out, I want to make everything as good as I can now that I’m still learning. Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:43:46+00:00Added an answer on June 17, 2026 at 8:43 pm

    Your calling public void Update(GraphicsDevice graphics)

    but the movement code is in public void Update()

    What I suggest you do is this, use the virtual and override keywords.

    In your entity class it should look like this:

     public virtual void Update(GraphicsDevice graphics) {
    
        }
    

    And in your player class

    public override void Update(GraphicsDevice graphics) {
    //ADD MOVEMENT CODE HERE
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have classes that can mix only specified traits: class Peter extends
I have classes that can take a file as an argument, for example: ParserClass(file('/some/file',
I have classes A1, A2, A3, A4 derived from a class A. How can
I have classes that are structured like the following: public class Forecast { [Key]
I have classes abstract class A { //.... } class B extends A {
If I have classes like this, class A < ActiveRecord::Base include ExampleModule end class
Hi I have classes that extend some given interfaces which I can't change. And
I have classes that are named exactly the same across different plug-ins that I
I have classes that are needed in both my web service and my server.
I have classes structured like this: Public MustInherit Class A ' several properties End

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.