我目前正在开发类似于Tiny Wings的Windows Phone游戏,而且我一直遇到玩家角色和山丘之间的碰撞检测问题。我一直在研究几种不同的方法,包括Farseer Physics引擎和Silverlight tutorial
玩家的角色初始化
lander = new Lander(new Vector2(graphics.PreferredBackBufferWidth / 4, 100));
LoadContent
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
primitiveBatch = new PrimitiveBatch(GraphicsDevice);
// Load textures
landerTexture = this.Content.Load<Texture2D>(".\\Sprites\\boysmall");
// Initialize lander based on texture
lander.Width = landerTexture.Width;
lander.Height = landerTexture.Height;
// Load the parallaxing background
bgLayer1.Initialize(Content, "hillsbackground", GraphicsDevice.Viewport.Width, -2, 150);
bgLayer2.Initialize(Content, "hillsbackground2", GraphicsDevice.Viewport.Width, -3, 220);
// Load hills
string[] hillsArray = new string[2]{"textured","hillsmooth"};
mainHill.Initialize(Content, hillsArray, GraphicsDevice.Viewport.Width, -4);
// Load Sky
mainBackground.Initialize(Content, "mainbackground", GraphicsDevice.Viewport.Width, -1, 0);
}
据我所知,从根本上说,碰撞检测会查找两个矩形之间的交点,并且已经为“登陆器”做了这个,但是我不知道如何为山丘做这个,以及如何让它顺利滑下来?如果您需要任何其他代码,请告诉我们,谢谢您的帮助!
Lander的矩形尝试
public Rectangle GetScreenRectangle()
{
// calculate the rectangle from the current location
// and the current source rectangle
var rectangle = new Rectangle(
(int)graphics.PreferredBackBufferWidth / 4,
(int)100,
lander.Width,
lander.Height);
return rectangle;
}
Hills Code
class MainHill
{
// The image representing the parallaxing background
Texture2D texture;
// An array of positions of the parallaxing background
//Vector2[] positions;
// The speed which the background is moving
int speed;
// Object array holding position and texture of each live texture
object[,] positionsArray = new object[2, 2];
public void Initialize(ContentManager content, Array hillsArray, int screenWidth, int speed)
{
// Load the background texture we will be using
//texture = content.Load<Texture2D>("textured");
// Set the speed of the background
this.speed = speed;
// If we divide the screen with the texture width then we can determine the number of tiles need.
// We add 1 to it so that we won't have a gap in the tiling
//positions = new Vector2[screenWidth / texture.Width + 2];
texture = content.Load<Texture2D>("textured");
// Set the initial positions of the parallaxing background
for (int i = 0; i < screenWidth / texture.Width + 2; i++)
{
//Pull out random texture
Random random = new Random();
string[] randomHill = new string[2];
randomHill[0] = "textured";
randomHill[1] = "hillsmooth";
string currentString = randomHill[(int)(random.Next(0, 2))];
texture = content.Load<Texture2D>(currentString);
// We need the tiles to be side by side to create a tiling effect
positionsArray[i,0] = new Vector2(i * texture.Width, 290);
positionsArray[i, 1] = texture;
}
}
public void Update(ContentManager content)
{
Debug.WriteLine("=========================");
// Update the positions of the background
for (int i = 0; i < positionsArray.GetLength(0); i++)
{
Vector2 current_position = (Vector2)positionsArray[i, 0];
// Update the position of the screen by adding the speed
current_position.X += speed;
// Check the texture is out of view then put that texture at the end of the screen
if (current_position.X <= -texture.Width)
{
// Figure out a possibly new texture if it's out of view
Random random = new Random();
string[] newHill = new string[2];
newHill[0] = "textured";
newHill[1] = "hillsmooth";
string currentString = newHill[(int)(random.Next(0, 2))];
texture = content.Load<Texture2D>(currentString);
Color[] textureData = new Color[texture.Width * texture.Height];
texture.GetData(textureData);
current_position.X = texture.Width * (positionsArray.GetLength(0) - 1);
positionsArray[i, 1] = texture;
}
positionsArray[i, 0] = current_position;
}
}
public void Draw(SpriteBatch spriteBatch, ContentManager content)
{
for (int i = 0; i < positionsArray.GetLength(0); i++)
{
Vector2 current_position = (Vector2)positionsArray[i, 0];
//Random random = new Random();
//string[] hillsArray = new string[2];
//hillsArray[0] = "textured";
//hillsArray[1] = "hillsmooth";
//string currentString = hillsArray[(int)(random.Next(0,2))];
//texture = content.Load<Texture2D>(currentString);
texture = (Texture2D)positionsArray[i, 1];
spriteBatch.Draw(texture, current_position, Color.White);
}
}
}