0 Replies - 696 Views - Last Post: 29 April 2017 - 08:37 AM

#1 French_Fries  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-April 17

Monogame/XNA - Help on Working on Collisions Listed Rectangles

Posted 29 April 2017 - 08:37 AM

Hello! I'm fairly new in Monogame and I can't get to make my collisions work. The Player endlessly falls down rather than detecting the tiles. I placed the collision detecting on my Game1.cs class.

Here's the code snippet, but the full code of the class Game1.cs is below.
            foreach (Rectangle r in Stage.TileRec)
            {
                if (r.Intersects(Player.LocRec))
                {
                    Player.plY = Player.plY;
                }
                else
                {
                    Player.plY++;
                }
                break;
            }


Any idea how?

Thank you so much for any help you may give!

My code in my Stage.cs

    public class Stage
    {
        Texture2D grassText, background;
        string currBG, currTile;
        public Rectangle BGRec;
        public List<Rectangle> TileRec = new List<Rectangle>();
        public List<Rectangle> MovingRec = new List<Rectangle>();
        public bool isCollide; 

        //constructor 
        public Stage()
        {
            
        }

        public void LoadContent(ContentManager Content)
        {
            currBG = "BG_1";
            currTile = "Grass";
            grassText = Content.Load<Texture2D>(currTile);
            background = Content.Load<Texture2D>(currBG);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            BGRec = new Rectangle(0,0, 1400, 800);
            spriteBatch.Draw(background,BGRec, Color.White);

            foreach (Rectangle i in TileRec)
            {
                spriteBatch.Draw(grassText, i, Color.White);
            }


        }

        public void Update(GameTime gameTime)
        {
                        
        }

        public void callStage1()
        {
            TileRec = new List<Rectangle>();
            // This variables provides the space between the targets
            for (int i = 0; i < 16; i++)
            {
                
                if (i < 9)
                {
                    Rectangle TileR = new Rectangle(75 * i, 580, 75, 25);
                    TileRec.Add(TileR);
                }
                else if (i > 8 && i < 11)
                {
                    int j = i-9;
                    Rectangle TileR = new Rectangle(75 * j, 380, 75, 25);
                    TileRec.Add(TileR);
                }
                else if (i > 10 && i < 16)
                {
                    int j = i - 7;
                    Rectangle TileR = new Rectangle(75 * j, 380, 75, 25);
                    TileRec.Add(TileR);
                }

            }
        }

    }





My code in my Player.cs
public class Player
    {
        Texture2D Pltexture;

        public Rectangle spriteRec, LocRec;
        public int speed;
        //Collision Variables 
        public Rectangle boundingBox;
        public bool isColliding;
        //Locations 

        public int plX, plY=0, size, Ax=0, Ay=0, Lx = 0, timer =1;
        public int walkChecker; 
        //constructor 
        public Player()
        {
            size = 80; 
            LocRec = new Rectangle(plX, plY, size, size);
            spriteRec = new Rectangle(1000/8*Ax, 500/4*Ay, 1000/8, 500/4);
            speed = 10;
            isColliding = false;
        }

        public void LoadContent(ContentManager Content)
        {
                        Pltexture = Content.Load<Texture2D>("Player_Sprite");
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Pltexture, LocRec, spriteRec, Color.White);
        }

        public void Update(GameTime gameTime)
        {
            spriteRec = new Rectangle(1000 / 8 * Ax, 500 / 4 * Ay, 1000 / 8, 500 / 4);
        }


    }




Here is my code for: Game1.cs
 public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Scripts.Player Player = new Scripts.Player();
        Scripts.Stage Stage = new Scripts.Stage();

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.IsFullScreen = false;
            graphics.PreferredBackBufferWidth = 1400;
            graphics.PreferredBackBufferHeight = 600;
            this.window.Title = "Start Screen";
            Content.RootDirectory = "Content";

        }

        protected override void Initialize()
        {

            base.Initialize();
            Stage.callStage1();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Player.LoadContent(Content);
            Stage.LoadContent(Content);
            
        }
        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();
            Player.Update(gameTime);
            Stage.Update(gameTime);

            foreach (Rectangle r in Stage.TileRec)
            {
                if (r.Intersects(Player.LocRec))
                {
                    Player.plY = Player.plY;
                }
                else
                {
                    Player.plY++;
                }
                break;
            }


            base.Update(gameTime);
        }

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

            spriteBatch.Begin();
            Stage.Draw(spriteBatch);
            Player.Draw(spriteBatch);
                      
            spriteBatch.End();
            base.Draw(gameTime);

        }
    }



Is This A Good Question/Topic? 0
  • +

Page 1 of 1