ich entschuldige mich vielmals für mein rumgenerve, aber die kollisionserkennung ist echt hart
, sitze seit einer woche dran. es klappt nix!
was ist falsch daran:
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D player1;
Rectangle player1_rect;
Texture2D bombableblock;
Texture2D constblock;
Rectangle[] bombableblock_rect = new Rectangle[100];
Rectangle[] constblock_rect = new Rectangle[217];
Rectangle staticblock;
//List<Rectangle> bombableblock_rect = new List<Rectangle>();
//List<Rectangle> constblock_rect = new List<Rectangle>();
int const_x = 0;
int const_y = 0;
int currWidth = 810;
int currHeight = 630;
bool block = false;
bool block2 = false;
bool block3 = false;
bool block4 = false;
bool block5 = false;
public Game1()
{
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
graphics.PreferredBackBufferWidth = currWidth;
graphics.PreferredBackBufferHeight = currHeight;
graphics.ApplyChanges();
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()
{
player1 = Content.Load<Texture2D>("player");
player1_rect = new Rectangle(30, 30, 30, 30);
bombableblock = Content.Load<Texture2D>("bombable_block");
constblock = Content.Load<Texture2D>("staticblock");
spriteBatch = new SpriteBatch(GraphicsDevice);
for (int i = 0; i < 216; i++)
{
constblock_rect
.Location = new Point(const_x, const_y);
constblock_rect[i].Height = 30;
constblock_rect[i].Width = 30;
if (i < 27)
{
const_x += 30;
}
else if (i >= 27 && i <= 47)
{
const_x = 0;
const_y += 30;
}
else if (i >= 47 && i <= 75)
{
const_y = 600;
const_x += 30;
}
else if (i >= 75 && i < 95)
{
const_x = currWidth - 30;
const_y -= 30;
}
else if (i == 95)
{
const_x = 60;
const_y = 60;
}
else if (i > 96)
{
const_y += 60;
if (const_y == 600)
{
const_x += 60;
const_y = 0;
}
}
}
const_x = 60;
const_y = 60;
// 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)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
if (Keyboard.GetState().IsKeyUp(Keys.W))
{
block = false;
}
if (Keyboard.GetState().IsKeyUp(Keys.A))
{
block2 = false;
}
if (Keyboard.GetState().IsKeyUp(Keys.S))
{
block3 = false;
}
if (Keyboard.GetState().IsKeyUp(Keys.D))
{
block4 = false;
}
if (Keyboard.GetState().IsKeyDown(Keys.W))
{
if (block == false)
{
block = true;
if (player1_rect.X >= 20)
{
for (int i = 0; i < 10; i++)
{
const_y += 60;
if (const_y > 540)
{
const_x += 60;
const_y = 0;
}
if (player1_rect.Y > const_y && player1_rect.X < const_x)
{
player1_rect.Y -= 30;
}
}
}
}
}
else if (Keyboard.GetState().IsKeyDown(Keys.A))
{
if (!block2)
{
block2 = true;
if (player1_rect.X >= 45)
{
player1_rect.X -= 30;
}
}
}
else if (Keyboard.GetState().IsKeyDown(Keys.S))
{
if (!block3)
{
block3 = true;
if (player1_rect.Y <= currHeight - 70)
{
player1_rect.Y += 30;
}
}
}
else if (Keyboard.GetState().IsKeyDown(Keys.D))
{
if (!block4)
{
block4 = true;
if (player1_rect.X <= currWidth - 70)
{
player1_rect.X += 30;
}
}
}
if (constblock_rect[216].Intersects(player1_rect))
{
// player1.X = X - Koordinaten der figur egal wo sie sich bewegt
// player1.X = Y - Koordinaten der figur egal wo sie sich bewegt
player1_rect.Y -= constblock_rect[216].Height;
player1_rect.X -= constblock_rect[216].Width;[/i][/i]
[i][i](DAs ist der wichtige Part) es klappt alles, nur nicht die kollision für die restlichen blöcke.[/i][/i]
[i][i]
}
// 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.White);
spriteBatch.Begin();
spriteBatch.Draw(player1, player1_rect, Color.White);
for (int i = 0; i < constblock_rect.Count(); i++)
{
spriteBatch.Draw(constblock, constblock_rect[i], Color.White);
}
// TODO: Add your drawing code here
spriteBatch.End();
base.Draw(gameTime);
}
}
}[/i][/i][/i]