我正在尝试构建超级马里奥兄弟第一级的克隆,但在让我的班级互相“讲话”时遇到了一些麻烦。到目前为止,我有两个Controller类(键盘,游戏手柄)和两个动画Sprite类(RunMarioSprite,DeadMarioSprite)。我要做的是根据用户输入的键盘/游戏手柄在屏幕上显示的这两个精灵之间切换。如果按F键,则Mario应该显示为右运行。如果按下键D,则马里奥(Mario)应该上下移动。

通过我的控制器类更改在屏幕上绘制哪个Animated Sprite类的正确方法是什么?

键盘类

public class KeyboardController : IController
{
    KeyboardState keyboard = Keyboard.GetState();
    private IAnimatedSprite marioSprite;
    Texture2D texture;

    public void Update()
    {
        if (keyboard.IsKeyDown(Keys.Q))
        {
            // QUIT GAME
        }
        if (keyboard.IsKeyDown(Keys.F))
        {
            // MARIO RUN RIGHT
        }
        if (keyboard.IsKeyDown(Keys.D))
        {
            // DEAD MARIO UP DOWN

        }
    }
}


动画精灵类的示例

public class RunMarioSprite : IAnimatedSprite
{
    public Texture2D Texture { get; set; }
    private int currentFrame = 0;
    private int totalFrames = 4;
    public int frameShift = 30;

    public RunMarioSprite(Texture2D texture)
    {
        currentFrame = 0;
        this.Texture = texture;
    }

    public void Update()
    {
        currentFrame++;
        if (currentFrame == totalFrames)
            currentFrame = 0;
    }

    public void Draw(SpriteBatch spriteBatch)
    {

        Rectangle sourceRectangle2 = new Rectangle((240 +(currentFrame*frameShift)), 0, 30, 30);
        Rectangle destinationRectangle2 = new Rectangle(100, 100, 30, 30);

        spriteBatch.Begin();
        spriteBatch.Draw(Texture, destinationRectangle2, sourceRectangle2, Color.White);
        spriteBatch.End();
    }
}


    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();


        // TODO: Add your update logic here
      foreach (IController Controller in ControllerList)
        {
           Controller.Update();
        }

        marioSprite.Update();
        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here
        marioSprite.Draw(this.spriteBatch);

        base.Draw(gameTime);
    }


主(更新和绘制)

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();


        // TODO: Add your update logic here
      foreach (IController Controller in ControllerList)
        {
           Controller.Update();
        }

        marioSprite.Update();
        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here
        marioSprite.Draw(this.spriteBatch);

        base.Draw(gameTime);
    }


如果问题的格式不正确,我深表歉意。谢谢您的帮助!

最佳答案

您需要在Sprite Manager类中处理KeyboardController,其目的是绘制屏幕上的整个关卡。可能您不需要KeyboardController,而只需要KeyboardState

KeyboardController keyboardController = new KeyboardController();

public void Update()
{
   // you should implement a method like this
   KeyboardState keyboard = keyboardController.GetKeyboardState();

   if (keyboard.IsKeyDown(Keys.F))
   {
      //change marioSprite instance
   }
   if (keyboard.IsKeyDown(Keys.D))
   {
      //change marioSprite instance
   }
 }

关于c# - C#/XNA如何使我的类(class)互相“讲话”?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18617923/

10-11 23:09
查看更多