Hallo,
nachdem ich nun seit etwa 2 Tagen mit einem Problem beschäftige und dieses nicht lösen kann würde ich mich sehr freuen wenn jemand den Code anschauen kann. Ich weiß selber einfach nicht mehr weiter 😕
Mein Problem ist folgendes: Ich habe einen Spieler und Texturen. Nun ist es so, dass der Spieler zwar gegen die Texturen läuft und dabei auch kollidiert und auch auf den Texturen läuft der Spieler wie er es sollte. Springe ich jedoch, so funktioniert gar nichts mehr.
Hier hab ich mal ein kurzes Video gemacht, in dem das Problem zu sehen ist.
Video
Der Code sieht folgendermaßen aus.
Die Kollisionsklasse
public class Collision
{
public static int MAPWIDTH = 32;
public static int MAPHEIGHT = 18;
public CellType[,] Cells { get; set; }
public Collision()
{
Cells = new CellType[MAPWIDTH, MAPHEIGHT];
}
public static Collision Load(string filename)
{
StreamReader sr = new StreamReader(filename);
Collision collisionMap = new Collision();
for (int y = 0; y < MAPHEIGHT; y++)
{
string line = sr.ReadLine();
for (int x = 0; x < MAPWIDTH; x++)
{
switch (line[x])
{
case '.':
collisionMap.Cells[x, y] = CellType.Nothing;
break;
case 'x':
collisionMap.Cells[x, y] = CellType.Collision;
break;
default:
throw new NotSupportedException();
}
}
}
return collisionMap;
}
//Typen, die in einem Level vorkommen
public enum CellType
{
Nothing,
Collision
}
}
Die aufgerufene Textdatei enthält eben die X für Stellen wo eine Kollision vorhanden sein soll.
Player Klasse.
public class Player
{
public Texture2D texture;
public Vector2 position;
public int speed;
public Vector2 pDimension;
public Vector2 oldPosition;
//Jumping
Vector2 velocity;
bool hasJumped;
//Konstruktor
public Player()
{
texture = null;
position = new Vector2(0, 582);
speed = 4;
hasJumped = false;
oldPosition = Vector2.Zero;
}
//Load Content
public void LoadContent(ContentManager Content)
{
texture = Content.Load<Texture2D>("playeridle");
}
//Update
public void Update(GameTime gameTime, Collision collisionMap)
{
KeyboardState keyState = Keyboard.GetState();
position += velocity;
pDimension = new Vector2(texture.Width, texture.Height);
//Steuerung
//Nach Rechts
if (keyState.IsKeyDown(Keys.Right) || keyState.IsKeyDown(Keys.D))
{
oldPosition = position;
position.X = position.X + speed;
}
//Nach Links
if (keyState.IsKeyDown(Keys.Left) || keyState.IsKeyDown(Keys.A))
{
oldPosition = position;
position.X = position.X - speed;
}
//nach Oben
if (keyState.IsKeyDown(Keys.Up) || keyState.IsKeyDown(Keys.W))
{
oldPosition = position;
position.Y = position.Y - speed;
}
//Runter laufen
if (keyState.IsKeyDown(Keys.Down) || keyState.IsKeyDown(Keys.S))
{
oldPosition = position;
position.Y = position.Y + speed;
}
//Springen
if (keyState.IsKeyDown(Keys.Space) && hasJumped == false)
{
position.Y -= 10f; //Standartwert 10
velocity.Y = -5f; //Standartwert -5
oldPosition = position;
hasJumped = true;
}
if (hasJumped == true)
{
oldPosition = position;
float i = 1;
velocity.Y += 0.20f * i; //Standartwert 0.15f
}
if (position.Y + texture.Height >= 720)
{
hasJumped = false;
}
if (hasJumped == false)
{
velocity.Y = 0f;
}
#region Spieler im Bild
//Spieler im Bild bleiben
//Linker Rand
if (position.X <= 0)
position.X = 0;
//Rechter Rand
if (position.X >= 1280 - texture.Width)
position.X = 1280 - texture.Width;
//oberer Rand
if (position.Y <= 0)
position.Y = 0;
//unterer Rand
if (position.Y >= 720 - texture.Height)
position.Y = 720 - texture.Height;
#endregion
//Collision
for (int y = 0; y < Collision.MAPHEIGHT; y++)
{
for (int x = 0; x < Collision.MAPWIDTH; x++)
{
if (collisionMap.Cells[x, y] == Collision.CellType.Collision)
{
if (position.X + pDimension.X < x * MapManager.PlattX || position.X > x * MapManager.PlattX + MapManager.PlattX ||
position.Y + pDimension.Y < y * MapManager.PlattY || position.Y > y * MapManager.PlattY + MapManager.PlattY)
{
//no collision
}
else
{
position = oldPosition;
}
}
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, Color.White);
}
}
Das Problem ist in der Player Klasse in der Update Methode, da wo auch die Steuerung ansetzt.
In der Main Klasse, wird ein Collision-Objekt erstellt, die Methode Load aufgerufen, und dem Player-Objekt diesen Parameter übergeben.
mfg
Ich habe mir den Code jetzt nicht angeschaut, es ist aber normalerweise ein bisschen komplizierter, weil du beachten musst, von welcher Seite du kommst. Außerdem willst du ja nicht an der vorherigen Position bleiben, sondern genau an der Kante hängen.
Es gibt aber einige ziemliche gute Tutorals dazu:
Simple platformer game in XNA tutorial – part six "jumping and stopping"
ImageTools for Silverlight: http://imagetools.codeplex.com | http://www.silverdiagram.net | http://www.cleancodedeveloper.de b:::