在XNA中让精灵自己移动

本文关键字:自己 移动 精灵 XNA | 更新日期: 2023-09-27 18:14:20

在我的代码中,我试图在X轴上以一个方向和速度移动三个精灵。然而,当我在自己的课程中编写代码时,它可以正常编译,没有错误,但当游戏开始时,我想移动的精灵根本不会移动。他们只是坐在那里。下列代码:

                   class Enemy : EnemySprite
{
    const string ENEMY_ASSETNAME = "BadguyLeft";
    const int START_POSITION_X1 = 350;
    const int START_POSITION_X2 = 600;
    const int START_POSITION_X3 = 750;
    const int START_POSITION_Y = 415;
    const int MOVE_LEFT = -1;
    int WizardSpeed = 160;
    enum State
    {
        Walking
    }

真正的问题是:

               public void LoadContent(ContentManager theContentManager)
    {
        base.LoadContent(theContentManager, ENEMY_ASSETNAME);
    }
    public void Update(GameTime theGameTime)
    {
        //KeyboardState aCurrentKeyboardState = Keyboard.GetState();
        //UpdateMovement(aCurrentKeyboardState);
        //mPreviousKeyboardState = aCurrentKeyboardState;
        Position[0] = new Vector2(START_POSITION_X1, START_POSITION_Y);
        Position[1] = new Vector2(START_POSITION_X2, START_POSITION_Y);
        Position[2] = new Vector2(START_POSITION_X3, START_POSITION_Y);
        base.Update(theGameTime, mSpeed, mDirection);
    }
    private void UpdateMovement(KeyboardState aCurrentKeyboardState)
    {
        //int positionTracker = START_POSITION_X3;
        if (mCurrentState == State.Walking)
        {
            mSpeed = Vector2.Zero;
            mDirection = Vector2.Zero;

          for (int i = 0; i < Position.Count(); i++) 
            if (mCurrentState == State.Walking)
            {
                mSpeed.X = WizardSpeed;
                mDirection.X = MOVE_LEFT;
            }


        }

在XNA中让精灵自己移动

你根本没有真正改变精灵的位置。

你的更新方法是移动应该(通常)发生的地方。

它看起来像这样:

//this will move an object to the left
Vector2 speed = new Vector2(-10, 0);
public void Update(GameTime theGameTime)
{
    //this will add the speed of the sprite to its position
    //making it move
    Position[0] += speed;
    Position[1] += speed;
    Position[2] += speed;
    base.Update(theGameTime, mSpeed, mDirection);
}