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 6882511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:15:44+00:00 2026-05-27T05:15:44+00:00

I was just messing around with XNA and trying to get a simple player

  • 0

I was just messing around with XNA and trying to get a simple player class that could move around through keyboard controls. I can’t seem to get it to draw the way I have it set up now though. I’m not seeing what error I’m making.

Here’s the Actor base class:

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace RunnerTEst
{
    class Actor
    {
        public static List<Actor> actors;

        public Texture2D texture;

        protected Vector2 position;
        protected float rotation;
        protected float scale;
        protected Color color;

    #region Constructors

        static Actor()
        {
            actors = new List<Actor>();
        }

        public Actor(Texture2D texture)
        {
            actors.Add(this);
            this.texture = texture;
            this.position = Vector2.Zero;
            this.rotation = 0f;
            this.scale = 1f;
        }

    #endregion

    #region Properties

        public Vector2 Position 
        {
            get { return this.position; }
            set { this.position = value; }
        }

        public Vector2 Origin
        {
            get { return new Vector2(this.position.X + this.texture.Width / 2,
                                     this.position.Y + this.texture.Height / 2); }
        }

        public float Rotation
        {
            get { return this.rotation; }
            set { this.rotation = value; }
        }

        public float Scale
        {
            get { return this.scale; }
            set { this.scale = value; }
        }

        public Color Color
        {
            get { return this.color; }
            set { this.color = value; }
        }

        public float Width
        {
            get { return this.texture.Width; }
        }

        public float Height
        {
            get { return this.texture.Height; }
        }

    #endregion

    #region Methods

        public virtual void Update(GameTime gameTime)
        {

        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(this.texture, this.position, this.color);
        }

    #endregion
    }
}

Player class:

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace RunnerTEst
{
    class Player : Actor
{
    private const float speed = 5f;

    protected Vector2 velocity;

    private static KeyboardState currState, prevState;

    static Player()
    {
        currState = prevState = Keyboard.GetState();
    }

    public Player(Texture2D texture, Vector2 position)
        : base(texture)
    {
        this.position = position;
        this.velocity = Vector2.Zero;
    }

    public override void Update(GameTime gameTime)
    {
        this.HandleInput();

        this.position += this.velocity;

        foreach (Actor actor in Actor.actors)
        {
            if (this == actor)
                continue;
            this.CheckCollision(actor);
        }

        base.Update(gameTime);
    }

    public override void Draw(SpriteBatch spriteBatch)
    {
        base.Draw(spriteBatch);
    }

    public void CheckCollision(Actor actor)
    {
        //--left/right sides
        if (this.position.X + this.Width > actor.Position.X) //right of this hitting left actor
        {
            this.position.X = actor.Position.X - this.Width;
        }
        else if (this.position.X < (actor.Position.X + actor.Width))//left this hit right actor
        {
            this.position.X = actor.Position.X + actor.Width;
        }

        //--top/bottom
        if (this.position.Y + this.Height > actor.Position.Y) //this bottom hit actor top
        {
            this.position.Y = actor.Position.Y - this.Width;
        }
        else if (this.position.Y < (actor.Position.Y + actor.Height))//this top hit actor bottom
        {
            this.position.Y = actor.Position.Y + actor.Height;
        }

        //TODO: check screen bounds
    }

    public void HandleInput()
    {
        currState = Keyboard.GetState();

        if (currState.IsKeyDown(Keys.W))
        {
            this.velocity.Y = -speed;
        }
        else if (currState.IsKeyDown(Keys.S))
        {
            this.velocity.Y = speed;
        }
        else
        {
            this.velocity.Y = 0f;
        }

        if (currState.IsKeyDown(Keys.A))
        {
            this.velocity.X = -speed;
        }
        else if (currState.IsKeyDown(Keys.D))
        {
            this.velocity.X = speed;
        }
        else
        {
            this.velocity.X = 0f;
        }

        prevState = currState;
    }
}
}

and lastly, the game:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

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

    Texture2D playerTexture;
    Player me;

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

    protected override void Initialize()
    {


        base.Initialize();
    }

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

        playerTexture = Content.Load<Texture2D>(@"Textures\player");
        me = new Player(playerTexture, new Vector2(200, 200));
    }

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

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        me.Update(gameTime);


        base.Update(gameTime);
    }

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

        spriteBatch.Begin();

        me.Draw(spriteBatch);

        spriteBatch.End();

        base.Draw(gameTime);
    }
}
}

Thanks in advance for your time!

  • 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-05-27T05:15:45+00:00Added an answer on May 27, 2026 at 5:15 am

    I looked through it, and it looks like you never define the Actor’s color property. This means that the color variable defaults to black, which means that the tint given to the Draw line (spriteBatch.Draw(this.texture, this.position, this.color);) is Black, which hides the sprite.

    Just set the color of the Actor to white somewhere (I just set it underneath me = new Player(playerTexture, new Vector2(200, 200));).

    So the new LoadContent would look like:

    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);
    
        playerTexture = Content.Load<Texture2D>(@"Textures\player");
        me = new Player(playerTexture, new Vector2(200, 200));
        me.Color = Color.White;
    }
    

    Hope that helps,
    max

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Cocoa Touch and I'm just messing around trying to get a
Was just messing around looking at the XNA documentation for the Game class and
I am just messing around trying to make a game right now, but I
Just by messing around a little it seems that the video stream is not
I'm messing about with some stuff in XNA and am trying to move an
I'm just messing around with some C++ at the moment trying to make a
I've just been messing around with threads in Java to get my head around
I want to get sql running on my mac. I'm just messing around with
I am messing around with a basic CAAnimation example where Im just trying to
just messing around, trying to expand my bag o' tricks: I was just experimenting

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.