0
\$\begingroup\$

I was curious if anyone could explain how I would be able to create simple movement handling in monogame. So far I have a red block which I tried to move by using a loop but that didn't take any effect.

protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            position = new Vector2(0, 0);
            texture = new Texture2D(this.GraphicsDevice, 100, 100);
            Color[] colorData = new Color[150 * 150];
            for(int i = 0; i < 10000; i++)
                colorData[i] = Color.Red;
            texture.SetData<Color>(colorData);
            base.Initialize();
        }

I also have created this handler but I don't exactly know what I have to do next.

/*protected override void HandleInput(KeyboardState keyState)
        {
            if (keyState.IsKeyDown(Keys.W))
            {
                // Look Up
            }
            if (keyState.IsKeyDown(Keys.S))
            {
                // Look Down
            }
            if (keyState.IsKeyDown(Keys.A))
            {
                // Move Left
            }
            if (keyState.IsKeyDown(Keys.D))
            {
                // Move Right
            }
        }*/
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

From your code it is not clear if you use GameComponents or not, so I answer for both. I assume you do, since I think you are able to draw the Red Block and not update it. Which leads me to assume you haven't seen the Update and Draw methods in the main class.

Behavior and player input is handled in the gameloop. The gameloop is basically the "update the gameworld state"- "draw the gameworld state" cycle.

The update (like the draw) is handled in your main game class. Since you seem to be able to show the Red Block, you found the draw method. In the main game class (Game1, if you follow the project template) you'll also find a pregenerated Update method:

 protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();
        }

From that method call your RedBlock object to do things. For example:

 protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

           KeyboardState kbstate = Keyboard.GetState();
           _myRedBlock.HandleInput(kbstate);
        }

This tells your Red Block code to execute the HandleInput every update frame. From this you can update the position.X or position.Y based on the input.

You might have used a GameComponent. When registered via Game.Components.Add(_myRedBlock);, the component automatically is updated and drawn in the gameloop. Use the Update in the GameComponent to have it updated each frame (see GameComponent.Update). From this Update, check the state of the keyboard and move the block accordingly.

Personally I don't use the GameComponent/DrawableGameComponent classes, but write my own game objects. There have been debates on what architecture works best. Both ways work and each have their advantages.

\$\endgroup\$
2
  • \$\begingroup\$ Great! You can mark the answer as correct to close the question. \$\endgroup\$
    – Felsir
    May 23, 2016 at 5:08
  • \$\begingroup\$ There! :) thx for helping out a newb \$\endgroup\$ May 24, 2016 at 12:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .