5 Replies - 1418 Views - Last Post: 02 June 2016 - 03:13 AM

#1 adildo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-May 16

Textures disappearing

Posted 31 May 2016 - 01:02 PM

I decided to try to make a pong game to learn how to use monogame but I'm running into this weird bug. All of the objects on the screen disappear after 10 to 15 seconds. I'm not sure what's going on.

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Pong
{
    /// <summary>
    /// This is the main type for your game.
    /// </summary>
    public class pong : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D ballTexture;
        Rectangle ballrect;
        Texture2D paddleTexture;
        Rectangle paddleRect;
        public pong()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        
        protected override void Initialize()
        {
           
           
            base.Initialize();
        }

      
        protected override void LoadContent()
        {

            spriteBatch = new SpriteBatch(GraphicsDevice);
            ballTexture = this.Content.Load<Texture2D>("ball");
            ballrect = new Rectangle(graphics.GraphicsDevice.Viewport.Width / 2, graphics.GraphicsDevice.Viewport.Height / 2, 15, 15);
            paddleTexture = this.Content.Load<Texture2D>("paddle");
            paddleRect = new Rectangle(20, (graphics.GraphicsDevice.Viewport.Height / 2) - paddleTexture.Height / 2, 20, 80);


        }

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

 
        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.Down))
                paddleRect.Y += 3;
            if (Keyboard.GetState().IsKeyDown(Keys.Up))
                paddleRect.Y -= 3;
            

            base.Update(gameTime);
        }

      
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();
            spriteBatch.Draw(ballTexture,ballrect , Color.White);
            spriteBatch.Draw(paddleTexture,paddleRect, Color.White);
            spriteBatch.End();
            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Textures disappearing

#2 SixOfEleven  Icon User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Textures disappearing

Posted 31 May 2016 - 03:54 PM

You haven't added any code to keep the game objects on the screen. Check out this tutorial that I wrote for XNA that covers the basics of collision detection on what to do to keep game objects on the screen.

http://www.dreaminco...sion-detection/

This post has been edited by SixOfEleven: 31 May 2016 - 03:54 PM

Was This Post Helpful? 0
  • +
  • -

#3 adildo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-May 16

Re: Textures disappearing

Posted 31 May 2016 - 04:19 PM

Excuse me if I'm being stupid, but I can't see any difference in the code that you wrote in comparison to mine other than the fact that you put everything in classes.
Does Monogame automatically remove items from the screen if it thinks that the program doesn't need them anymore or am I loading the content wrong? I thought that as long as I called drew the shapes in the Draw function it would work. I feel like I'm missing something stupidly obvious here and I'm not sure if I'll be able to go to sleep without figuring this out :/
Was This Post Helpful? 0
  • +
  • -

#4 adildo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-May 16

Re: Textures disappearing

Posted 31 May 2016 - 04:36 PM

I decided to create a basic image drawing program to try to spot where I'm messing up and I'm starting to think that its a bug
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Game2
{

    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D myTexture;
        Rectangle myRectangle;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

  
        protected override void Initialize()
        {
         

            base.Initialize();
        }

   
        protected override void LoadContent()
        {
          
            spriteBatch = new SpriteBatch(GraphicsDevice);
            myTexture = Content.Load<Texture2D>("ball");
           
          
        }

       
        protected override void UnloadContent()
        {
           
        }

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


            base.Update(gameTime);
        }

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

            spriteBatch.Begin();
            spriteBatch.Draw(myTexture, new Rectangle(300, 400, 30, 30), Color.White);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}



Can't edit my post so I'll follow up here. This piece of code doesn't work either.
Was This Post Helpful? 0
  • +
  • -

#5 SixOfEleven  Icon User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Textures disappearing

Posted 31 May 2016 - 06:07 PM

In the other code you were changing positions so it looked like that is what the issue is. In this code there does not seem to be a reason why the objects would disappear off the screen. What I suggest is try uninstalling Monogame and reinstall it. Also check to make sure that you have all of the prerequisites for Monogame installed.
Was This Post Helpful? 0
  • +
  • -

#6 adildo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-May 16

Re: Textures disappearing

Posted 02 June 2016 - 03:13 AM

View PostSixOfEleven, on 31 May 2016 - 06:07 PM, said:

In the other code you were changing positions so it looked like that is what the issue is. In this code there does not seem to be a reason why the objects would disappear off the screen. What I suggest is try uninstalling Monogame and reinstall it. Also check to make sure that you have all of the prerequisites for Monogame installed.

I just ran the Monogame installer again and it fixed it. Glad to see that I'm not THAT bad at programming
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1