Search code examples
c#monogame

Can't get the "power-up" in my MonoGame to load, no errors from compiling


Having a slight problem making my power up appear/load on the screen and I have zero errors in VS19. The Worm class inherit from the same parent as the Player and Enemy Class, and they both load. The only difference between them is that Worm loads in Update() instead of LoadContent() so I can only assume something is wrong with the code in there.

Let me know if you see anything weird, thanks in advance!

Global declared a list and a texture-type:

List<Worm> worms;
Texture2D wormSprite;

In load LoadContent() I assign the texture to a variable:

wormSprite = Content.Load<Texture2D>("worm");

Then the code I assume is iffy in Update():

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

            // TODO: Add your update logic here

            /* player */ 
            player.Update(Window);

            /* power up */
            // worms that spawn in a chance of 200 each loop(60/s)
            Random random = new Random();
            int newWorm = random.Next(1, 200);
            if (newWorm == 1)
            {
                // cordinates for worms to appear
                int rndX = random.Next(0, Window.ClientBounds.Width - wormSprite.Width);
                int rndY = random.Next(0, Window.ClientBounds.Height - wormSprite.Height);
                // add the worm to the list
                worms.Add(new Worm(wormSprite, rndX, rndY, gameTime));
            }
            // loop thru the list of worms
            foreach (Worm w in worms.ToList())
            {
                if (w.IsAlive) // check if worm is alive
                {
                    // w.Update() check if the worm is to old to stay active
                    w.Update(gameTime);

                    // check if the worms collides with the player
                    if (w.CheckCollision(player))
                    {
                        // remove worm if collides
                        worms.Remove(w);
                        player.Points++; // and give the player a point
                    }
                }
                else // remove the worm if it's too inactive
                    worms.Remove(w);
            }
            base.Update(gameTime);
        }

Print to screen method

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

            // TODO: Add your drawing code here
            _spriteBatch.Begin();

            player.Draw(_spriteBatch);

            foreach (Enemy e in enemies)
                e.Draw(_spriteBatch);

            foreach (Worm w in worms)
                w.Draw(_spriteBatch);

            printText.Print("Points:" + player.Points, _spriteBatch, 0, 0);

            _spriteBatch.End();

            base.Draw(gameTime);
        }

The PowerUp() class that inherits from PhysicalObject()fd

    class Worm : PhysicalObject
    {
        double timeToDie; // active time of the worm
        // Worm(), construct to create a worm
        public Worm(Texture2D texture, float X, float Y, GameTime gameTime)
            : base(texture, X, Y, 0, 2f)
        {
            timeToDie = gameTime.TotalGameTime.TotalMilliseconds + 5000;
        }
        /* Update(), check if the worm should stay alive */
        public void Update(GameTime gameTime)
        {
            // kill worm if it stayed alive too long
            if (timeToDie < gameTime.TotalGameTime.TotalMilliseconds)
                isAlive = false;
        }

    }

Solution

  • Forgot to set a bool variable to true

    protected bool IsAlive; // changed to: protected bool IsAlive = true;