和这些例子一样的块:
与地面相撞,当地面停止跳跃时,它可以完美地发挥玩家的作用
与屋顶相撞的是,在坚硬的街区之后他无法继续跳跃 所以他继续堕落(工作完美)
我现在的问题是左碰撞和右边
像那样 如果有人可以帮助我,我会很高兴 让他无法继续在艰难的阻挡之后继续前进这是我的课程 我的左右不工作
UpdateCollision.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
class UpdateCollision
{
public Player myPlayer;
public Map myMap;
public Block toughtBlock;
public void Initialize(Player myPlayer, Map myMap)
{
this.myPlayer = myPlayer;
this.myMap = myMap;
}
public bool IsPlayerToughGround()
{
Rectangle Player;
Rectangle BlockDown;
// Only create the rectangle once for the player
Player = new Rectangle((int)myPlayer.Position.X,
(int)myPlayer.Position.Y,
myPlayer.Width/2,
myPlayer.Height/2);
// Do the collision between the player and the Block
int i, j;
bool isTough = false;
// For each block in the game
for (i = 0; i < (myMap.Height / myMap.pictureSize) && !isTough; i++)
{
for (j = 0; j < (myMap.Width / myMap.pictureSize) && !isTough; j++)
{
// If the block is Draw
if (myMap.Blocks[i, j].isAlive)
{
BlockDown = new Rectangle((int)myMap.Blocks[i, j].X,
(int)myMap.Blocks[i, j].Y,
myMap.pictureSize,
myMap.pictureSize);
// Determine if the two objects collided with each
// other
if (Player.Intersects(BlockDown))
{
isTough = true;
toughtBlock = myMap.Blocks[i, j];
}
}
}
}
return isTough;
}
public bool IsPlayerToughRoof()
{
Rectangle Player;
Rectangle Blockup;
// Only create the rectangle once for the player
Player = new Rectangle((int)myPlayer.Position.X,
(int)myPlayer.Position.Y,
myPlayer.Width / 2,
myPlayer.Height / 2);
// Do the collision between the player and the Block
int i, j;
bool isTough = false;
// For each block in the game
for (i = 0; i < (myMap.Height / myMap.pictureSize) && !isTough; i++)
{
for (j = 0; j < (myMap.Width / myMap.pictureSize) && !isTough; j++)
{
// If the block is Draw
if (myMap.Blocks[i, j].isAlive)
{
Blockup = new Rectangle((int)myMap.Blocks[i, j].X,
(int)myMap.Blocks[i, j].Y + 20,
myMap.pictureSize,
myMap.pictureSize);
// Determine if the two objects collided with each
// other
if (Player.Intersects(Blockup))
{
isTough = true;
toughtBlock = myMap.Blocks[i, j];
}
}
}
}
return isTough;
}
public bool IsPlayerToughLeft()
{
Rectangle Player;
Rectangle BlockLeft;
// Only create the rectangle once for the player
Player = new Rectangle((int)myPlayer.Position.X,
(int)myPlayer.Position.Y,
myPlayer.Width ,
myPlayer.Height );
// Do the collision between the player and the Block
int i, j;
bool isTough = false;
// For each block in the game
for (i = 0; i < myMap.Height / myMap.pictureSize; i++)
{
for (j = 0; j < myMap.Width / myMap.pictureSize; j++)
{
// If the block is Draw
if (myMap.Blocks[i, j].isAlive)
{
BlockLeft = new Rectangle((int)myMap.Blocks[i, j].X +20,
(int)myMap.Blocks[i, j].Y,
myMap.pictureSize,
myMap.pictureSize);
// Determine if the two objects collided with each
// other
if (Player.Intersects(BlockLeft))
{
isTough = true;
toughtBlock = myMap.Blocks[i, j];
}
}
}
}
return isTough;
}
public bool IsPlayerToughRight()
{
Rectangle Player;
Rectangle Blockup;
Rectangle BlockDown;
Rectangle BlockLeft;
Rectangle BlockRight;
// Only create the rectangle once for the player
Player = new Rectangle((int)myPlayer.Position.X,
(int)myPlayer.Position.Y,
myPlayer.Width / 2,
myPlayer.Height / 2);
// Do the collision between the player and the Block
int i, j;
bool isTough = false;
// For each block in the game
for (i = 0; i < (myMap.Height / myMap.pictureSize) && !isTough; i++)
{
for (j = 0; j < (myMap.Width / myMap.pictureSize) && !isTough; j++)
{
// If the block is Draw
if (myMap.Blocks[i, j].isAlive)
{
Blockup = new Rectangle((int)myMap.Blocks[i, j].X,
(int)myMap.Blocks[i, j].Y + 20,
myMap.pictureSize,
myMap.pictureSize);
BlockDown = new Rectangle((int)myMap.Blocks[i, j].X,
(int)myMap.Blocks[i, j].Y,
myMap.pictureSize,
myMap.pictureSize);
BlockLeft = new Rectangle((int)myMap.Blocks[i, j].X + 20,
(int)myMap.Blocks[i, j].Y,
myMap.pictureSize,
myMap.pictureSize);
// Determine if the two objects collided with each
// other
if (Player.Intersects(Blockup) ||
Player.Intersects(BlockDown) ||
Player.Intersects(BlockLeft))// ||
//Player.Intersects(BlockRight))
{
isTough = true;
toughtBlock = myMap.Blocks[i, j];
}
}
}
}
return isTough;
}
}
在KeyBoardHeadler中使用它们
KeyBoardHeadler.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
class KeyBoardHeadler
{
KeyboardState myKeyBoard;
// A movement speed for the player
float playerMoveSpeed;
float Gravity;
float JumpSpeed;
// The graphic device
GraphicsDevice gamedevice;
// The Contect that load texture
ContentManager LoadTexture;
// The vector that move the gravity
Vector2 velocity;
UpdateCollision Coll;
Player player;
Map map;
bool RightIsOk=true;
bool LeftIsOk=true;
public void Initialize(GraphicsDevice gv, ContentManager mainTexture, Player player, Map map)
{
// Get keyboard statement
myKeyBoard = new KeyboardState();
// Set a constant player move speed
playerMoveSpeed = 3.0f;
// Set Gravity vector
velocity = Vector2.Zero;
// Gravity amount
Gravity = 10f;
// Jump Speed
JumpSpeed = 400f;
// Get the graphic device
gamedevice = gv;
// Get Contect Manger to change player texture
LoadTexture = mainTexture;
// Initlize collision
Coll = new UpdateCollision();
Coll.Initialize(player, map);
// Initalize map and player
this.player = player;
this.map = map;
}
public void Update(GameTime gameTime)
{
// Read the current state of the keyboard and gamepad and store it
myKeyBoard = Keyboard.GetState();
// Use the Keyboard / Dpad
if (myKeyBoard.IsKeyDown(Keys.Left) && LeftIsOk)
{
player.PlayerAnimation.elapsedTime +=(int)gameTime.ElapsedGameTime.TotalMilliseconds;
player.PlayerAnimation.spriteStrip = LoadTexture.Load<Texture2D>("Player\\Left");
player.PlayerAnimation.frameCount = 2;
player.Position.X -= playerMoveSpeed;
}
if (myKeyBoard.IsKeyDown(Keys.Right) && RightIsOk)
{
player.PlayerAnimation.elapsedTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;
player.PlayerAnimation.spriteStrip = LoadTexture.Load<Texture2D>("Player\\Right");
player.PlayerAnimation.frameCount = 2;
player.Position.X += playerMoveSpeed;
}
if (myKeyBoard.IsKeyDown(Keys.Up) && !player.isJump)
{
// add the gravity to the Player
velocity.Y = -JumpSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
player.isJump = true;
}
if (player.isJump)
velocity.Y += Gravity * (float)gameTime.ElapsedGameTime.TotalSeconds;
else
velocity.Y = 0;
// Add the position of the jump
player.Position.Y += velocity.Y;
// If the Player tough anything
if (Coll.IsPlayerToughGround())
{
// if the Player is not jump
if (Coll.myPlayer.Position.Y <= Coll.toughtBlock.Y)
{
player.isJump = false;
velocity.Y = 0;
}
}
else
{
player.isJump = true;
}
if (Coll.IsPlayerToughRoof())
{
velocity.Y = 2;
}
if (Coll.IsPlayerToughLeft())
{
int x=6;
}
// Make sure that the player does not go out of bounds (get the area of the game (between 0 - max of map)
player.Position.X = MathHelper.Clamp(player.Position.X, 0, gamedevice.Viewport.Width - player.Width + 25);
player.Position.Y = MathHelper.Clamp(player.Position.Y, 0, gamedevice.Viewport.Height - player.Height + 50);
if (myKeyBoard.IsKeyDown(Keys.NumPad0))
{
player.Position = new Vector2(100, 100);
velocity.Y = 0;
}
if (myKeyBoard.IsKeyDown(Keys.NumPad1))
{
player.isJump = !player.isJump;
}
}
public void Draw(SpriteBatch spriteBatch)
{
}
}
是否有任何选项只能在X轴上进行检查