# Monogame/XNA - Help on Working on Collisions Listed Rectangles

Page 1 of 1

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

### #1 French_Fries

Reputation: 0
• 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?

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()
{

}

{
currBG = "BG_1";
currTile = "Grass";
}

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);
}
else if (i > 8 && i < 11)
{
int j = i-9;
Rectangle TileR = new Rectangle(75 * j, 380, 75, 25);
}
else if (i > 10 && i < 16)
{
int j = i - 7;
Rectangle TileR = new Rectangle(75 * j, 380, 75, 25);
}

}
}

}

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 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();
}

{
spriteBatch = new SpriteBatch(GraphicsDevice);

}
{
}

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

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }