C#/ XNA如何让我的课程互相“说话”?

时间:2013-09-04 15:28:47

标签: c# class xna

我正在尝试建立第一级超级马里奥兄弟的克隆,但我在让我的课程互相“说话”时遇到了一些麻烦。截至目前,我有两个Controller类(键盘,游戏手柄)和两个动画精灵类(RunMarioSprite,DeadMarioSprite)。我想要做的是根据用户输入的键盘/游戏手柄在屏幕上显示这两个精灵。如果按下F键,Mario应显示正确。如果按下按键D,马里奥应该上下移动。

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

键盘类

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

如果问题格式不正确,我道歉。谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

您需要在精灵管理器类中处理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
   }
 }

答案 1 :(得分:0)

如果您要求的是根据另一个类中的键盘输入知道要在Main.Draw()方法中绘制哪个精灵的方法,您可以尝试这样的事情:

public class Main
{
    public static bool DrawAliveMario;
    RunMarioSprite runmariosprite;
    DeadMarioSprite deadmariosprite;

    protected override void Draw(GameTime gameTime)
    {
        //...

        // TODO: Add your drawing code here
        if (DrawAliveMario) runmariosprite.Draw(this.spriteBatch);
        else deadmariosprite.Draw(this.spriteBatch);

        base.Draw(gameTime);
    }

}

然后你可以拥有你的控制器类:

public class KeyboardController : IController
{
    KeyboardState keyboard = Keyboard.GetState();

    public void Update()
    {
        if (keyboard.IsKeyDown(Keys.Q))
        {
            // QUIT GAME    
        }
        if (keyboard.IsKeyDown(Keys.F))
        {
            Main.DrawAliveMario = true;           
        }
        if (keyboard.IsKeyDown(Keys.D))
        {
            Main.DrawAliveMario = false;

        }
    }
}

希望这有帮助。

相关问题