无法切换精灵

时间:2013-04-04 20:15:27

标签: c# xna

好的我有一个包含移动代码等的播放器类。 我的问题是当按下键“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和非武装变量设置为false,并且M4值设置为true。

我是以正确的方式来做这件事的吗?

我也尝试过IsKeyUp&amp;&amp; IsKeyDown方法,你将当前状态链接到旧状态,但这只会导致枪在消失之前闪烁到屏幕上,所以如果有办法解决这个问题,那就太棒了

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

2ND编辑:我想通了,我给了每个武器国家自己的武装();变量然后在底部的武装()中添加了真/假设置;变量在这里:

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

}

1 个答案:

答案 0 :(得分:0)

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

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

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

显然叫

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

将您的武器设置为false,将unarmed设置为true

因此,即使您认为可能在循环结束时切换状态,当您放开密钥时,它将始终重置下一个循环。

相关问题