Вот мой код Game1.cs.Моя цель состояла в том, чтобы спрайт "игрок" приземлился на платформах, но он не работает.Когда я использую изображение, которое не является spritesheet, оно работает.Что я здесь не так делаю?T_T Я проверил много ссылок, но все еще не могу понять это.Спасибо!Наша последняя презентация в понедельник, поэтому я в отчаянии.Я не могу передать нашу тему GameDev.
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Audio;
namespace Basa_Game
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SoundEffect effect;
enum GameState
{
Menu,
Playing,
Options,
}
GameState CurrentGameState = GameState.Menu;
cButton btnPlay,btnControls,btnCredits,btnQuit;
Animation player;
Player2 player2;
Character sample;
List <Platform> platforms = new List<Platform>();
KeyboardState ks;
float elapsed;
float delay = 200f;
int frames = 0;
Texture2D menuTexture;
Rectangle destRect, sourceRect;
Texture2D mapTexture;
Rectangle mapRectangle;
Texture2D tile1Texture;
Rectangle tile1Rectangle;
int screenWidth = 1366;
int screenHeight =768;
Vector2 position = new Vector2();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.GraphicsProfile = GraphicsProfile.HiDef;
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
destRect = new Rectangle(0, 0, 1366, 768);
graphics.PreferredBackBufferWidth = 1366;
graphics.PreferredBackBufferHeight = 768;
graphics.ApplyChanges();
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
menuTexture = Content.Load<Texture2D>("menu2");
sample = new Character(Content.Load<Texture2D>("caren whole"), new Vector2(50, 50));
player = new Animation(Content.Load<Texture2D>("basawalk"), new Vector2(200, 200), 180, 110);
player2 = new Player2(Content.Load<Texture2D>("right2"), new Vector2(1200, 640), 180, 110);
position = new Vector2(400, 400);
mapTexture = Content.Load<Texture2D>("level1");
mapRectangle = new Rectangle(0, 0, 1366,768);
tile1Texture = Content.Load<Texture2D>("1");
tile1Rectangle = new Rectangle(150, 540, 177, 44);
platforms.Add(new Platform(Content.Load<Texture2D>("1"), new Vector2(20, 640)));
platforms.Add(new Platform(Content.Load<Texture2D>("1"), new Vector2(200, 550)));
platforms.Add(new Platform(Content.Load<Texture2D>("1"), new Vector2(650, 450)));
platforms.Add(new Platform(Content.Load<Texture2D>("1"), new Vector2(120, 450)));
platforms.Add(new Platform(Content.Load<Texture2D>("1"), new Vector2(900, 535)));
platforms.Add(new Platform(Content.Load<Texture2D>("1"), new Vector2(100, 200)));
platforms.Add(new Platform(Content.Load<Texture2D>("1"), new Vector2(400, 450)));
platforms.Add(new Platform(Content.Load<Texture2D>("1"), new Vector2(1000, 200)));
platforms.Add(new Platform(Content.Load<Texture2D>("1"), new Vector2(1090, 450)));
IsMouseVisible = true;
btnPlay = new cButton(Content.Load<Texture2D>("play"), graphics.GraphicsDevice);
btnPlay.setPosition(new Vector2(760, 320));
btnControls = new cButton(Content.Load<Texture2D>("btnControl"), graphics.GraphicsDevice);
btnControls.setPosition(new Vector2(760, 450));
screenWidth = GraphicsDevice.Viewport.Width;
screenHeight = GraphicsDevice.Viewport.Height;
}
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
protected override void Update(GameTime gameTime)
{
MouseState mouse = Mouse.GetState();
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
this.Exit();
switch (CurrentGameState)
{
case GameState.Menu:
Animate(gameTime);
sourceRect = new Rectangle(1366 * frames, 0, 1366, 768);
if (btnPlay.isClicked == true) CurrentGameState = GameState.Playing;
btnPlay.Update(mouse);
btnControls.Update(mouse);
break;
case GameState.Playing:
foreach (Platform platform in platforms)
if (player.rectangle.isOnTopOf(platform.rectangle))
{
player.velocity.Y = 0f;
player.hasJump = false;
}
player.Update(gameTime);
break;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
switch (CurrentGameState)
{
case GameState.Menu:
spriteBatch.Draw(menuTexture,destRect,sourceRect, Color.White);
btnPlay.Draw(spriteBatch);
btnControls.Draw(spriteBatch);
break;
case GameState.Playing:
spriteBatch.Draw(mapTexture, mapRectangle, Color.White);
foreach (Platform platform in platforms)
platform.Draw(spriteBatch);
sample.Draw(spriteBatch);
player.Draw(spriteBatch);
player2.Draw(spriteBatch);
break;
}
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
public void Animate(GameTime gameTime)
{
elapsed += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (elapsed >= delay)
{
if (frames >= 2)
{
frames = 0;
}
else
{
frames++;
}
elapsed = 0;
}
}
}
}
static class RectangleHelper
{
const int penetrationMargin = 5;
public static bool isOnTopOf(this Rectangle r1, Rectangle r2)
{
return (r1.Bottom >= r2.Top - penetrationMargin &&
r1.Bottom <= r2.Top + 1 &&
r1.Right >= r2.Left + 5 &&
r1.Left <= r2.Right - 5);
}
}
А вот мой Animation.cs, куда я помещаю движения игрока и прочее.
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Basa_Game
{
class Animation
{
public Texture2D texture;
public Rectangle rectangle;
public Vector2 position;
public Vector2 origin;
public Vector2 velocity;
SpriteEffects effect = SpriteEffects.FlipHorizontally;
int currentFrame;
int frameHeight = 110;
int frameWidth = 180;
float time;
float interval=80f;
public bool hasJump;
public Animation(Texture2D newTexture, Vector2 newPostion, int newFrameHeigth, int newFrameWidth)
{
texture = newTexture;
position = newPostion;
frameHeight = newFrameHeigth;
frameWidth = newFrameWidth;
hasJump = true;
}
public void Update(GameTime gameTime)
{
position += velocity;
rectangle = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);
origin = new Vector2(rectangle.Width / 2, rectangle.Height / 2);
position = position + velocity;
if (Keyboard.GetState().IsKeyDown(Keys.D))
{
AnimateRight(gameTime);
velocity.X = 1;
}
else if (Keyboard.GetState().IsKeyDown(Keys.A))
{
AnimateLeft(gameTime);
velocity.X = -1;
}
else velocity.X = 0f;
if (rectangle.X <= 0)
rectangle.X = 0;
if (Keyboard.GetState().IsKeyDown(Keys.W) && hasJump == false)
{
position.Y -= 10f;
velocity.Y = -5f;
hasJump = true;
}
if (hasJump == true)
{
float i = 1;
velocity.Y += 0.15f * i;
}
if (position.Y + texture.Height >= 768)
hasJump = false;
if (hasJump == false)
velocity.Y = 0f;
}
public void AnimateRight(GameTime gameTime)
{
time += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
effect = SpriteEffects.None;
if (time > interval)
{
currentFrame++;
time = 0;
if (currentFrame > 3)
currentFrame = 0;
}
}
public void AnimateLeft(GameTime gameTime)
{
time += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
effect = SpriteEffects.FlipHorizontally;
if (time > interval)
{
currentFrame++;
time = 0;
if (currentFrame > 3)
currentFrame = 0;
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, rectangle, Color.White, 0f, origin, 1.0f, effect, 0);
}
}
}