1

好的,我有一个包含移动代码等的玩家类。我的问题是当按下“2”键时,我希望玩家精灵(默认情况下是非武装的)切换到一个拿着枪的人。

直到我尝试在游戏中添加第三把枪,我才开始工作,此时我只能通过一直按住“2”和“3”键才能看到这两把枪。

我希望它切换,我认为我对布尔值的使用可能是原因,但这是代码

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
    {
        public Texture2D playerunarmed;
        public Texture2D playerM1911;
        public Texture2D playerM4;
        public KeyboardState keyState;
        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 unarmed;
        public bool M1911;
        public bool M4;
        public bool IsToggled = false;
        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");
            playerPosition = location;
            unarmed = true;
            M4 = true;
            M1911 = true;           
        }

        public void Update()
        {
            MouseState curMouse = Mouse.GetState();

            if (M1911 == true)
            {
                armed();
            }

            if (unarmed == true)
            {
                armed();
            }

            if (M4 == true)
            {
                armed();
            }

            Vector2 mouseLoc = new Vector2(curMouse.X, curMouse.Y);
            Vector2 direction = mouseLoc - playerPosition;
            angle = (float)(Math.Atan2(direction.Y, direction.X));
            keyState = Keyboard.GetState();         

            if (keyState.IsKeyDown(Keys.D1))
            {
                unarmed = true;
                M4 = false;
                M1911 = false;              
            }

            if (keyState.IsKeyDown(Keys.D2))
            {
                M1911 = true;
                M4 = false;
                unarmed = false;         
            }

            if (keyState.IsKeyDown(Keys.D3))
            {
                M4 = true;
                unarmed = false;
                M1911 = 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 (unarmed == true)
                {
                    spriteBatch.Draw(playerunarmed, new Rectangle((int)playerPosition.X, (int)playerPosition.Y, playerunarmed.Width, playerunarmed.Height), null, Color.White, angle + 90, new Vector2(62, 70), SpriteEffects.None, 0f);
                }

                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(62, 70), 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(62, 70), SpriteEffects.None, 0f);
                }
            }
            spriteBatch.End();
        }

        public void armed()
        {
            M1911 = false;
            M4 = false;
            unarmed = true;
        }
    }
}

现在我认为问题出在这,就在这里

if (keyState.IsKeyDown(Keys.D1))
{
    unarmed = true;
    M4 = false;
    M1911 = false;

}

if (keyState.IsKeyDown(Keys.D2))
{
    M1911 = true;
    M4 = false;
    unarmed = false;        
}

if (keyState.IsKeyDown(Keys.D3))
{
    M4 = true;
    unarmed = false;
    M1911 = false;
}

不知何故,我必须让按键切换,这样当我按下“3”时,它确保 M1911 和非武装变量设置为假,而 M4 值设置为真。

我会以正确的方式解决这个问题吗?

我也尝试过 IsKeyUp && IsKeyDown 方法,您可以在其中将当前状态链接到旧状态,但这只会导致枪在消失之前短暂地在屏幕上闪烁,所以如果有办法解决这个问题,那就是伟大的

编辑:那么有没有办法编辑武装方法,以便在按下键时改变武器状态?

第二次编辑:我想通了,我给每个武器状态都赋予了它自己的 armed(); 变量,然后在底部武装()中添加真/假设置;这里的变量:

    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;
    }
}

}

4

1 回答 1

0

armed()无论您的武器状态如何,您都在每个更新循环开始时调用该方法

if (M1911 == true)
{
    armed();
}

if (unarmed == true)
{
    armed();
}
if (M4 == true)
{
    armed();
}

这显然是调用

public void armed()
{
    M1911 = false;
    M4 = false;
    unarmed = true;
}

这会将你的武器设置为 false 和unarmedtrue

所以即使你认为你可能会在循环结束时切换你的状态,当你放开按键时,它总是会再次重置下一个循环。

于 2013-04-04T20:29:00.680 回答