Quantcast
Channel: Community | MonoGame - Latest topics
Viewing all articles
Browse latest Browse all 6821

Updating position and texture of an object in Update(GameTime gameTime)

$
0
0

@Alex_Grinshteyn wrote:

I am trying to learn MonoGame by building a simple 2D wolf catch eggs game here is my Game class

    using System;
    using System.Collections.Generic;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Storage;
    using Microsoft.Xna.Framework.GamerServices;
    using GameName1.GameObjects;

    namespace GameName1
    {
        /// <summary>
        /// This is the main type for your game
        /// </summary>
        public class Game1 : Game
        {
            GraphicsDeviceManager graphics;
            SpriteBatch spriteBatch;


            private Texture2D background;
          
            private Texture2D wolfLeftUp;
            private Texture2D wolfLeftDown;
            private Texture2D wolfRightUp;
            private Texture2D wolfRightDown;

            private Wolf wolf;

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

            /// <summary>
            /// Allows the game to perform any initialization it needs to before starting to run.
            /// This is where it can query for any required services and load any non-graphic
            /// related content.  Calling base.Initialize will enumerate through any components
            /// and initialize them as well.
            /// </summary>
            protected override void Initialize()
            {
                // TODO: Add your initialization logic here
                IsMouseVisible = true;
                base.Initialize();
            }

            /// <summary>
            /// LoadContent will be called once per game and is the place to load
            /// all of your content.
            /// </summary>
            protected override void LoadContent()
            {
                // Create a new SpriteBatch, which can be used to draw textures.
                spriteBatch = new SpriteBatch(GraphicsDevice);
                background = Content.Load<Texture2D>("background");

                wolfLeftUp = Content.Load<Texture2D>("wolflleftup");

                wolfLeftDown = Content.Load<Texture2D>("wolflleftdown");

                wolfRightUp = Content.Load<Texture2D>("wolflrightup");

                wolfRightDown = Content.Load<Texture2D>("wolflleftup");

               

                // TODO: use this.Content to load your game content here
            }

            /// <summary>
            /// UnloadContent will be called once per game and is the place to unload
            /// all content.
            /// </summary>
            protected override void UnloadContent()
            {
                // TODO: Unload any non ContentManager content here
            }

            /// <summary>
            /// Allows the game to run logic such as updating the world,
            /// checking for collisions, gathering input, and playing audio.
            /// </summary>
            /// <param name="gameTime">Provides a snapshot of timing values.</param>
            protected override void Update(GameTime gameTime)
            {
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                    Exit();

                 if (Keyboard.GetState().IsKeyDown(Keys.Right))
                 {
                    if (wolf.GetPosition() == WolfPositions.LeftUp)
                    {
                        
                       wolf.Update(gameTime, wolfLeftUp, new Rectangle(20, 3, 600, 500), WolfPositions.RightUp);
                       var pos = wolf.GetPosition();
                       
                    }
                    if (wolf.GetPosition() == WolfPositions.LeftDown)
                    {
                       
                    }
                }
                // TODO: Add your update logic here

                base.Update(gameTime);
            }

            /// <summary>
            /// This is called when the game should draw itself.
            /// </summary>
            /// <param name="gameTime">Provides a snapshot of timing values.</param>
            protected override void Draw(GameTime gameTime)
            {
                GraphicsDevice.Clear(Color.Gold);
                
                // TODO: Add your drawing code here

                spriteBatch.Begin();
                Rectangle destinationRectangle = new Rectangle(0, 0, 800, 500);
                spriteBatch.Draw(background, destinationRectangle, Color.White);

                
                wolf = new Wolf(wolfLeftDown, new Rectangle(20, 3, 600, 500), Color.White, WolfPositions.LeftUp);
                wolf.Draw(spriteBatch);

                spriteBatch.End();

                base.Draw(gameTime);
            }
        }
    }

and my wolf class

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;

namespace GameName1.GameObjects
{
    public class Wolf  
    {
        private WolfPositions wolfPosition;
        private  Texture2D content;
        private  Rectangle position;
        private  Color color;


        public Wolf(Texture2D content, Rectangle position, Color color, WolfPositions wolfPosition)
        {
            this.color = color;
            this.content = content;
            this.position = position;
            this.wolfPosition = wolfPosition;
        }

        public WolfPositions GetPosition()
        {
            return wolfPosition;
        }

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


        public virtual void Update(GameTime gameTime, Texture2D content, Rectangle position, WolfPositions wolfPosition)
        {
            this.content=content;
            this.position=position;
            this.wolfPosition = wolfPosition;
           

        }
    }
}

I am not sure what am I doing wrong but every time i am calling the

wolf.Update(gameTime, wolfLeftUp, new Rectangle(20, 3, 600, 500), WolfPositions.RightUp);

I do not see the position or Texture being updated on the screen

Any help is strongly appreciated.

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 6821

Trending Articles