私がやろうとしているのは、背景の後ろに長方形を描き、本質的に衝突検出を行うことです。ただし、それをどのように正確に実装できるかはわかりません。スプライトがこれらの長方形に近づくにつれて、スプライトの速度が徐々に遅くなり、停止するようにすることを考えましたが、うまくいくでしょうか? 耳の後ろで少し濡れているように聞こえる場合は申し訳ありません。私はC#にかなり慣れていないため、独学で勉強しようとしています。どんな助けでも大歓迎です。
だから私は CharacterSprite クラス (walkingsprite) を持っています。フレームに関するすべてのものは、私が実装した歩行アニメーション シーケンスです。
namespace walkingsprite
{
class AnimatedSprite
{
//keyboard
KeyboardState currentKBState;
KeyboardState previousKBState;
Texture2D spriteTexture;
float timer = 0f;
float interval = 200f;
int currentFrame = 0;
int spriteWidth = 32;
int spriteHeight = 48;
int spriteSpeed = 2;
Rectangle sourceRect;
Texture2D obst;
Rectangle obst1;
Obstruction obstruction1;
Vector2 position;
Vector2 origin;
public Vector2 Position
{
get { return position; }
set { position = value; }
}
public Vector2 Origin
{
get { return origin; }
set { origin = value; }
}
public Texture2D Texture
{
get { return spriteTexture; }
set { spriteTexture = value; }
}
public Rectangle SourceRect
{
get { return sourceRect; }
set { sourceRect = value; }
}
public int SpriteSpeed
{
get { return spriteSpeed; }
set { spriteSpeed = value; }
}
public AnimatedSprite(Texture2D texture, int currentFrame, int spriteWidth, int spriteHeight)
{
this.spriteTexture = texture;
this.currentFrame = currentFrame;
this.spriteWidth = spriteWidth;
this.spriteHeight = spriteHeight;
}
public void HandleSpriteMovement(GameTime gameTime)
{
previousKBState = currentKBState;
currentKBState = Keyboard.GetState();
sourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);
////////////////////////////////////////////////////////////////////
if (currentKBState.GetPressedKeys().Length == 0)
{
if (currentFrame > 0 && currentFrame < 4)
{
currentFrame = 0;
}
if (currentFrame > 4 && currentFrame < 8)
{
currentFrame = 4;
}
if (currentFrame > 8 && currentFrame < 12)
{
currentFrame = 8;
}
if (currentFrame > 12 && currentFrame < 16)
{
currentFrame = 12;
}
}
////////////////////////////////////////////////////////////////////
//sprintin
if (currentKBState.IsKeyDown(Keys.Space))
{
spriteSpeed = 2;
interval = 100;
}
else
{
spriteSpeed = 1;
interval = 200;
}
///////////////////////////////////////////////
if (currentKBState.IsKeyDown(Keys.Down) == true)
{
AnimateDown(gameTime);
if (position.Y < 575)
{
position.Y += spriteSpeed;
}
}
////////////////////////////////////////////////////
if (currentKBState.IsKeyDown(Keys.Up) == true)
{
AnimateUp(gameTime);
if (position.Y > 25)
{
position.Y -= spriteSpeed;
}
}
//////////////////////////////////////////////////////////
if (currentKBState.IsKeyDown(Keys.Right) == true)
{
AnimateRight(gameTime);
if (position.X < 780)
{
position.X += spriteSpeed;
}
}
////////////////////////////////////////////////////////////////////
if (currentKBState.IsKeyDown(Keys.Left) == true)
{
AnimateLeft(gameTime);
if (position.X > 0)
{
position.X -= spriteSpeed;
}
}
origin = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);
}
////////////////////////////////////////////////////////////////////
public void AnimateRight(GameTime gameTime)
{
if (currentKBState != previousKBState)
{
currentFrame = 9;
}
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if(timer > interval)
{
currentFrame++;
if(currentFrame > 11)
{
currentFrame = 8;
}
timer = 0f;
}
}
////////////////////////////////////////////////////////////////////
public void AnimateUp(GameTime gameTime)
{
if (currentKBState != previousKBState)
{
currentFrame = 13;
}
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (timer > interval)
{
currentFrame++;
if (currentFrame > 15)
{
currentFrame = 12;
}
timer = 0f;
}
}
//////////////////////////////////////////
public void AnimateDown(GameTime gameTime)
{
if (currentKBState != previousKBState)
{
currentFrame = 1;
}
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (timer > interval)
{
currentFrame++;
if (currentFrame > 3)
{
currentFrame = 0;
}
timer = 0f;
}
}
////////////////////////////////////////////////////
public void AnimateLeft(GameTime gameTime)
{
if (currentKBState != previousKBState)
{
currentFrame = 5;
}
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (timer > interval)
{
currentFrame++;
if (currentFrame > 7)
{
currentFrame = 4;
}
timer = 0f;
}
}
}
}
そして、私は妨害クラスを持っています。
namespace walkingsprite
{
class Obstruction
{
Texture2D obst;
Rectangle obstRec1;
public Rectangle ObstRec1
{
get { return obstRec1; }
set { obstRec1 = value; }
}
public Texture2D Obst
{
get { return obst; }
set { obst = value; }
}
public Obstruction(Texture2D texture, Rectangle rec)
{
this.obstRec1 = rec;
this.obst = texture;
}
}
}