ほぼ完全なプレーヤー クラスのゲームがありますが、これが私の問題です。Ctrl + K を押すと、プレイヤーは自殺します (意図的)。今、彼が死ぬと、プレイヤーの位置に血しぶきが生成されます (これも意図されています)。ここで問題が発生します。プレイヤーが再スポーンすると、血しぶきは表示されたままになりますが、プレイヤーの周りを追跡します。これは少し奇妙なことです。もしあなたが私に尋ねるなら
したがって、メインコードは次のとおりです。
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace Teir_Tactical_2A
{
public class Player
{
Random random = new Random();
public Texture2D playerunarmed;
public Texture2D playerM1911;
public Texture2D bloodspatter2;
public Texture2D playerM4;
public KeyboardState keyState;
public SpriteFont Font1;
public KeyboardState oldKeyboardState;
public Vector2 playerPosition;
public SpriteBatch spriteBatch;
public float Angle { get; set; }
public float AngularVelocity { get; set; }
public Vector2 playerVelocity = Vector2.One;
public bool M1911;
public bool M4;
String[] strs = new String[] { "You suicided. Way to go Genius.", "I saw your limbs blow off with that one.", "Click to Respawn", "HAHAHAHAHAHA-Sorry Click to Respawn" };
String suicideStr;
public bool player;
public bool suicide;
public bool LMBpressed;
public bool RMBpressed;
public bool isplayeralive;
public bool isplayerdead;
public bool respawnscreen;
public bool bloodspatter;
float angle;
public Player(ContentManager content, Vector2 location)
{
this.playerunarmed = content.Load<Texture2D>("PLAYER");
this.playerM1911 = content.Load<Texture2D>("PLAYERM1911");
this.playerM4 = content.Load<Texture2D>("PLAYERM4");
this.bloodspatter2 = content.Load<Texture2D>("blood");
playerPosition = location;
suicideStr = strs[random.Next(strs.Length)];
M1911 = true;
M4 = true;
player = true;
LMBpressed = false;
suicide = false;
RMBpressed = false;
isplayeralive = true;
isplayerdead = false;
respawnscreen = false;
bloodspatter = false;
Font1 = content.Load<SpriteFont>("Font1");
}
public void Update()
{
MouseState curMouse = Mouse.GetState();
if (bloodspatter == true)
{
blood();
}
if (suicide == true)
{
suicided();
}
if (isplayeralive == true)
{
alive();
}
if (isplayerdead == true)
{
dead();
}
if (M1911 == true)
{
armedM1911();
}
if (M4 == true)
{
armedM4();
}
if (player == true)
{
unarmed();
}
if (LMBpressed == true)
{
LMBpressedA();
}
if (RMBpressed == true)
{
RMBpressedA();
}
Vector2 mouseLoc = new Vector2(curMouse.X, curMouse.Y);
Vector2 direction = mouseLoc - playerPosition;
angle = (float)(Math.Atan2(direction.Y, direction.X));
if (curMouse.LeftButton == ButtonState.Pressed)
{
LMBpressed = true;
}
if (curMouse.RightButton == ButtonState.Pressed)
{
RMBpressed = true;
}
keyState = Keyboard.GetState();
if (isplayeralive == false && LMBpressed == true)
{
isplayeralive = true;
isplayerdead = false;
suicide = false;
suicideStr = strs[random.Next(strs.Length)];
}
if (keyState.IsKeyDown(Keys.LeftControl) && keyState.IsKeyDown(Keys.K))
{
isplayeralive = false;
isplayerdead = true;
bloodspatter = true;
suicide = true;
}
if (keyState.IsKeyDown(Keys.D1))
{
M1911 = false;
M4 = false;
player = true;
}
if (keyState.IsKeyDown(Keys.D2))
{
M1911 = true;
player = false;
}
if (keyState.IsKeyDown(Keys.D3))
{
M4 = true;
M1911 = false;
player = false;
}
if (keyState.IsKeyDown(Keys.D))
playerPosition.X += playerVelocity.X + 1;
if (keyState.IsKeyDown(Keys.A))
playerPosition.X -= playerVelocity.X + 1;
if (keyState.IsKeyDown(Keys.W))
playerPosition.Y -= playerVelocity.Y + 1;
if (keyState.IsKeyDown(Keys.S))
playerPosition.Y += playerVelocity.Y + 1;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Begin();
{
Rectangle sourceRectangle = new Rectangle(0, 0, playerunarmed.Width, playerunarmed.Height);
Vector2 origin = new Vector2(playerunarmed.Width / 2, playerunarmed.Height / 2);
if (M1911 == true)
{
spriteBatch.Draw(playerM1911, new Rectangle((int)playerPosition.X, (int)playerPosition.Y, playerunarmed.Width, playerunarmed.Height), null, Color.White, angle + 90, new Vector2(86, 88), SpriteEffects.None, 0f);
}
if (suicide == true)
{
spriteBatch.DrawString(Font1, suicideStr, new Vector2(10, 670), Color.Red);
}
}
if (bloodspatter == true)
{
spriteBatch.Draw(bloodspatter2, new Rectangle((int)playerPosition.X, (int)playerPosition.Y, bloodspatter2.Width, bloodspatter2.Height), Color.White);
}
if (player == true)
{
spriteBatch.Draw(playerunarmed, new Rectangle((int)playerPosition.X, (int)playerPosition.Y, playerunarmed.Width, playerunarmed.Height), null, Color.White, angle + 90, new Vector2(86, 88), SpriteEffects.None, 0f);
}
if (M4 == true)
{
spriteBatch.Draw(playerM4, new Rectangle((int)playerPosition.X, (int)playerPosition.Y, playerunarmed.Width, playerunarmed.Height), null, Color.White, angle + 90, new Vector2(86, 88), SpriteEffects.None, 0f);
}
if (LMBpressed == true)
{
spriteBatch.DrawString(Font1, "LMB PRESSED (shot taken prototype)", new Vector2(1000, 22), Color.GreenYellow);
}
if (RMBpressed == true)
{
spriteBatch.DrawString(Font1, "RMB PRESSED (grenade thrown prototype)", new Vector2(968, 34), Color.Red);
}
if (RMBpressed == true && LMBpressed == true)
{
spriteBatch.DrawString(Font1, "If you are seeing this, the mouse is functioning correctly", new Vector2(810, 45), Color.Black);
}
spriteBatch.End();
}
public void armedM1911()
{
M1911 = true;
M4 = false;
player = false;
}
public void armedM4()
{
M4 = true;
M1911 = false;
player = false;
}
public void unarmed()
{
M1911 = false;
M4 = false;
}
public void LMBpressedA()
{
LMBpressed = false;
}
public void RMBpressedA()
{
RMBpressed = false;
}
public void alive()
{
M1911 = false;
player = true;
M4 = false;
respawnscreen = false;
}
public void dead()
{
M1911 = false;
player = false;
M4 = false;
respawnscreen = true;
}
public void blood()
{
bloodspatter = true;
}
public void suicided()
{
suicide = true;
}
}
}
ここに問題があると思います (95% 確実) プレイヤーの座標を取得し、血しぶきの場所として設定する必要があります (必要に応じてさらにスポーンします)。
if (bloodspatter == true)
{
spriteBatch.Draw(bloodspatter2, new Rectangle((int)playerPosition.X, (int)playerPosition.Y, bloodspatter2.Width, bloodspatter2.Height), Color.White);
}
おそらく座標として を使用した方法が原因ですが、playerPosition
最初にそれらを取得してから、プレイヤーが自殺した場所に血飛沫の場所を設定するX/Y
ように指示する方法がわかりません。
編集: 誤解があるようです, 血のテクスチャが消えてほしくない,プレイヤーが最後に死んだ場所にとどまることを望んでいます(この場合, 自殺, それを呼び出す弾丸を追加できるようになるまで)非常に低解像度のテクスチャなので問題ありません。さらに、ゲームが終了するか、レベルが変化する場合にのみそれらを非表示にしたいです (ここでは問題ではありません)。