如何使弹孔出现在背景中?(XNA C#)
我试着让子弹孔在我开枪后出现在背景中 到目前为止,游戏的工作原理如下: 1.装枪精灵 2.单击鼠标左键加载火焰精灵并播放声音 3.释放鼠标左键加载枪精灵(无火焰) 我想实现的下一步是在背景上与枪的相对距离处有一些弹孔如何使弹孔出现在背景中?(XNA C#),c#,xna,C#,Xna,我试着让子弹孔在我开枪后出现在背景中 到目前为止,游戏的工作原理如下: 1.装枪精灵 2.单击鼠标左键加载火焰精灵并播放声音 3.释放鼠标左键加载枪精灵(无火焰) 我想实现的下一步是在背景上与枪的相对距离处有一些弹孔 class Gun { Texture2D gun, gunFlame, gunObject, bulletHole, hole; Vector2 position, bulletHolePosition; SoundEffe
class Gun
{
Texture2D gun, gunFlame, gunObject, bulletHole, hole;
Vector2 position, bulletHolePosition;
SoundEffect shotSound, fallSound;
MouseState currentMouseState, previousMouseState;
public Gun(ContentManager Content)
{
bulletHole = Content.Load<Texture2D>("bullethole");
bulletHolePosition = new Vector2(position.X + 3, position.Y + 3);
gun = Content.Load<Texture2D>("Gun");
position = new Vector2(10, 10);
gunFlame = Content.Load<Texture2D>("gunFlame");
shotSound = Content.Load<SoundEffect>("gunshot");
fallSound = Content.Load<SoundEffect>("bullet-fall");
gunObject = gun;
}
public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
spriteBatch.Draw(gunObject, position, Color.White);
}
public void Update(GameTime gameTime)
{
// Keep track of the previous state to only play the effect on new clicks and not when the button is held down
previousMouseState = currentMouseState;
currentMouseState = Mouse.GetState();
if (currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton != ButtonState.Pressed)
{
gunObject = gunFlame;
hole = bulletHole;
shotSound.Play();
fallSound.Play();
}
else if(currentMouseState.LeftButton == ButtonState.Released)
{
gunObject = gun;
}
}
class枪
{
纹理2D枪、枪火、枪体、弹孔、孔;
矢量2位置,子弹位置;
SoundEffect shotSound、fallSound;
MouseState currentMouseState,previousMouseState;
公共枪支(ContentManager内容)
{
弹孔=内容物荷载(“弹孔”);
bulletHolePosition=新矢量2(位置X+3,位置Y+3);
枪=内容物。装载(“枪”);
位置=新矢量2(10,10);
炮火=内容。装载(“炮火”);
shotSound=内容加载(“枪声”);
fallSound=Content.Load(“子弹坠落”);
枪体=枪;
}
公开无效抽牌(游戏时间,SpriteBatch SpriteBatch)
{
spriteBatch.Draw(枪体、位置、颜色、白色);
}
公开作废更新(游戏时间游戏时间)
{
//跟踪以前的状态,仅在新单击时播放效果,而不在按下按钮时播放效果
previousMouseState=currentMouseState;
currentMouseState=Mouse.GetState();
如果(currentMouseState.LeftButton==ButtonState.Pressed&&previousMouseState.LeftButton!=ButtonState.Pressed)
{
炮弹=炮火;
孔=弹孔;
shotSound.Play();
fallSound.Play();
}
else if(currentMouseState.LeftButton==ButtonState.Released)
{
枪体=枪;
}
}
我发现您的代码中存在一些问题,这让我怀疑您是否能够执行它,例如,在您的构造函数中,您有:
bulletHolePosition = new Vector2(position.X + 3, position.Y + 3);
gun = Content.Load<Texture2D>("Gun");
position = new Vector2(10, 10);
在构造函数中:
bulletHolePositions = new List<Vector2>;
最后,在Draw
方法中:
if (currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton != ButtonState.Pressed)
{
bulletHolePositions.Add(new Vector2(currentMouseState.X, currentMouseState.Y));
//...
}
//...
spriteBatch.Draw(gunObject, position, Color.White);
foreach(var holePosition in bulletHolePositions)
spriteBatch.Draw(bulletHole , position, Color.White);
请记住,如果屏幕上有许多这样的内容,您的游戏性能可能会下降
如果您需要更多帮助,请告诉我们您遇到问题的详细情况。谢谢,这对我帮助很大,我的游戏看起来已经好多了。@Light\u Warrior太好了!如果问题解决了,请不要忘记投票!
spriteBatch.Draw(gunObject, position, Color.White);
foreach(var holePosition in bulletHolePositions)
spriteBatch.Draw(bulletHole , position, Color.White);