1

我有一个几乎完整的玩家类的游戏,但这是我的问题。当我按下 control + 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坐标的方式,但我不知道如何告诉它先获取它们,然后将血溅位置设置为玩家自杀的位置。

编辑:似乎有一个误解,我希望血液纹理消失,我希望它在玩家最后死亡的地方(在这种情况下,自杀,直到我可以添加调用它的子弹)基本上,性能是不是问题,因为它的分辨率非常低,此外,我只希望它们在游戏结束或关卡发生变化时消失(这里不是问题)

4

2 回答 2

0

不要将玩家与血溅连接。让它独立。因此,当玩家死亡时,只需调用将在当前玩家位置产生血液的函数。

创建一个列表,您将在其中存储所有血溅,然后将新的血溅添加到该列表中。然后在 gamplay 类中调用该列表的绘制和更新。

伪:

splater (position as vector)
splaterlist inherit list of splater
- sub add new splater(position)
- sub update each splater, add duration, fade out
- sub draw each splater

player class
if player die add new splater into splaterlist

gameplay class
update player, draw player
update splaterlist, drawsplaterlist
于 2013-04-11T15:36:13.500 回答
0

如果您想在自杀后留下一个血点,您可以使用将一个 Vector2 添加到您的 Player 类并将其命名为 PlaceOfDeath。默认为新的 Vector2.Zero(); 当你死后,将你的英雄的位置设置为新的 PlaceOfDeath,然后使用该 Vector2 作为你的精灵的绘图点。

如果您想绘制玩家死亡的每个地方,您可以创建一个 Vector2 列表(列表)并将其命名为 PlacesOfDeath。默认为空列表。当你死后,用 PlacesOfDeath.Add({Vector2 Location of Hero}) 设置你的英雄的位置。

然后在您的绘制代码中,您可以遍历 PlacesOfDeath 列表中的每个 Vector2 并在每个位置绘制血迹。

所以现在到代码

对于单次血液放置 -

添加到您的类属性/成员声明中:

私人 Vector2 placeOfDeath = new Vector2.Zero();

添加到你的自杀方法,

placeOfDeath = 玩家位置;

在 Draw 方法中更改 if(bloodsplatter == true) 条件代码块:

spriteBatch.Draw(bloodspatter2, new Rectangle((int)placeOfDeath.X, (int)placeOfDeath.Y, bloodspatter2.Width, bloodspatter2.Height), Color.White);

对于多处血斑

添加到您的类属性/成员声明中:

私有列表 placesOfDeath = new List();

添加到你的自杀方法,

placesOfDeath.Add(playerPosition);

在 Draw 方法中更改 if(bloodsplatter == true) 条件代码块:

foreach(placeOfDeath 中的 Vector2 placeOfDeath){

spriteBatch.Draw(bloodspatter2, new Rectangle((int)placeOfDeath.X, (int)placeOfDeath.Y, bloodspatter2.Width, bloodspatter2.Height), Color.White);

}

于 2013-06-29T07:26:42.470 回答