XNA drawRectangle.X + = 1不会移动我的Rectangle / Sprite

时间:2013-11-03 16:51:20

标签: c# xna

我正在制作一款非常基本的马里奥风格游戏,我想让我的一个平台在屏幕上来回移动。

在我的平台类中,我创建了一个Update()方法,然后在我的Game1类中调用它。但是,平台保持静止。我知道代码是不完整的,关于让它前后移动,但现在我只想让平台移动。

代码:

class Platforms
{
    int xCoord;
    int yCoord;
    int leftLimit;
    int rightLimit;
    int length;
    Rectangle drawPlatformRectangle;
    const int width = 20; // until i figure out how to Game1.twoWidthUnits
    const int height = 20; // until i figure out how to Game1.twoHeightUnits
    Texture2D blockSprite;
    Texture2D leftBlockEdgeSprite;
    Texture2D rightBlockEdgeSprite;

    public Rectangle CollisionRectangle
    {
        get { return drawPlatformRectangle; }
    }

    /// <summary>
    /// Constructor
    /// </summary>
    /// <param name="xCoord">X-Coord of upper left-hand corner of platform</param>
    /// <param name="yCoord">Y-Coord of upper left-hand corner of platform</param>
    /// <param name="length">Length of platform (oneWidthUnits)</param>
    /// <param name="blockSprite">sprite</param>
    /// <param name="leftBlockEdgeSprite">sprite</param>
    /// <param name="rightBlockEdgeSprite">sprite</param>
    public Platforms(int xCoord, int yCoord, int length, 
        Texture2D blockSprite, Texture2D leftBlockEdgeSprite, Texture2D rightBlockEdgeSprite)
    {
        this.blockSprite = blockSprite;
        this.leftBlockEdgeSprite = leftBlockEdgeSprite;
        this.rightBlockEdgeSprite = rightBlockEdgeSprite;
        this.length = length;
        this.xCoord = xCoord;
        this.yCoord = yCoord;

        //build draw rectangles
        this.drawPlatformRectangle = new Rectangle(
            xCoord, yCoord, 2* width, 2* height);
    }

    /// <summary>
    /// Constructor
    /// </summary>
    /// <param name="xCoord">X-Coord of upper left-hand corner of platform</param>
    /// <param name="yCoord">Y-Coord of upper left-hand corner of platform</param>
    /// <param name="length">Length of platform (oneWidthUnits)</param>
    /// <param name="blockSprite">sprite</param>
    /// <param name="leftBlockEdgeSprite">sprite</param>
    /// <param name="rightBlockEdgeSprite">sprite</param>
    /// <param name="leftLimit">X-Coord of how far left platform should travel</param>
    /// <param name="rightLimit">X-Coord of how far right platform should travel</param>
    public Platforms(int xCoord, int yCoord, int length,
        Texture2D blockSprite, Texture2D leftBlockEdgeSprite, Texture2D rightBlockEdgeSprite,
        int leftLimit, int rightLimit)
    {
        this.blockSprite = blockSprite;
        this.leftBlockEdgeSprite = leftBlockEdgeSprite;
        this.rightBlockEdgeSprite = rightBlockEdgeSprite;
        this.length = length;
        this.xCoord = xCoord;
        this.yCoord = yCoord;
        this.leftLimit = leftLimit;
        this.rightLimit = rightLimit;

        //build draw rectangles
        this.drawPlatformRectangle = new Rectangle(
            xCoord, yCoord, 2 * width, 2 * height);
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        // draw the platform
        for (int i = xCoord; i < xCoord + (width * length); i += width * 2)
        {
            drawPlatformRectangle.X = i; drawPlatformRectangle.Y = yCoord;
            spriteBatch.Draw(blockSprite, drawPlatformRectangle, Color.White);
        }

        // draw the grass edges
        drawPlatformRectangle.X = xCoord - width*2; drawPlatformRectangle.Y = yCoord;
        spriteBatch.Draw(leftBlockEdgeSprite, drawPlatformRectangle, Color.White);

        drawPlatformRectangle.X = xCoord + (width * length)+width; drawPlatformRectangle.Y = yCoord;
        spriteBatch.Draw(rightBlockEdgeSprite, drawPlatformRectangle, Color.White);
    }

    public void Update(GameTime gameTime)
    {
        drawPlatformRectangle.X += 5;
        // to do: reverse direction when it reaches limits
    }
}

这是Game1 Class:

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    const int WINDOW_WIDTH = 800;

    const int WINDOW_HEIGHT = 600;

    // game state
    GameState gameState = GameState.OpeningMenu;

    // menu fields
    Texture2D test;

    // Level 1 textures list
    Texture2D skyBackground;
    Texture2D grassTexture;
    Texture2D leftGrassTexture;
    Texture2D rightGrassTexture;
    SpriteFont score;

    // Level 1 platforms
    Platforms platform1;
    Platforms platform2;
    Platforms platform3;
    Platforms platform4;
    Platforms platform5;
    Platforms platform6;
    Platforms platform7;
    Platforms platform8;
    Platforms platform9;

    // sounds
    SoundEffect backgroundSong;
    SoundEffectInstance backgroundSongInstance;

    // drawing variables
    int oneWidthUnit = WINDOW_WIDTH / 40;
    int oneHeightUnit = WINDOW_HEIGHT / 30;
    //int twoWidthUnits = WINDOW_WIDTH / 20;
    //int twoHeightUnits = WINDOW_HEIGHT / 15;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";

        graphics.PreferredBackBufferWidth = WINDOW_WIDTH;
        graphics.PreferredBackBufferHeight = WINDOW_HEIGHT;
        IsMouseVisible = true;
    }

    protected override void Initialize()
    {
        Window.Title = "Rory's Super Mega Awesome Game of Awesomeness";
        base.Initialize();
    }

    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        // TODO: use this.Content to load your game content here

        // Opening Screen Textures
        test = Content.Load<Texture2D>("test");


        // Load sounds here
        backgroundSong = Content.Load<SoundEffect>("Call to Adventure");
        backgroundSongInstance = backgroundSong.CreateInstance();
        backgroundSongInstance.IsLooped = true;


        // Load Level 1 sprite textures here
        skyBackground = Content.Load<Texture2D>("skybackground");
        //dirtTexture = Content.Load<Texture2D>("dirt");
        grassTexture = Content.Load<Texture2D>("grass_top");
        leftGrassTexture = Content.Load<Texture2D>("edge_left");
        rightGrassTexture = Content.Load<Texture2D>("edge_right");

        //create platforms
        platform1 = new Platforms(0, 28 * oneHeightUnit, 15, grassTexture, leftGrassTexture, rightGrassTexture);
        platform2 = new Platforms(26 * oneWidthUnit, 28 * oneHeightUnit, 14, grassTexture, leftGrassTexture, rightGrassTexture);
        platform3 = new Platforms(10 * oneWidthUnit, 23 * oneHeightUnit, 7, grassTexture, leftGrassTexture, rightGrassTexture);
        platform4 = new Platforms(18 * oneWidthUnit, 19 * oneHeightUnit, 5, grassTexture, leftGrassTexture, rightGrassTexture);
        platform5 = new Platforms(5 * oneWidthUnit, 15 * oneHeightUnit, 9, grassTexture, leftGrassTexture, rightGrassTexture);
        platform6 = new Platforms(19 * oneWidthUnit, 11 * oneHeightUnit, 3, grassTexture, leftGrassTexture, rightGrassTexture);
        platform7 = new Platforms(23 * oneWidthUnit, 7 * oneHeightUnit, 3, grassTexture, leftGrassTexture, rightGrassTexture);
        platform8 = new Platforms(30 * oneWidthUnit, 7 * oneHeightUnit, 7, grassTexture, leftGrassTexture, rightGrassTexture);
        platform9 = new Platforms(34 * oneWidthUnit, 14 * oneHeightUnit, 6, grassTexture, leftGrassTexture, rightGrassTexture);

    }

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

        // goes from menu to level 1 when player presses enter
        if (gameState == GameState.OpeningMenu && Keyboard.GetState().IsKeyDown(Keys.Enter))
        {
            gameState = GameState.Play;
            backgroundSongInstance.Play();

        }

        if (gameState == GameState.Play)
        {
            platform4.Update(gameTime); // ******THIS IS WHERE I'M HAVING THE PROBLEM!********
        }

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here
        spriteBatch.Begin();

        // draw opening menu
        if (gameState == GameState.OpeningMenu)
        {
            Rectangle rec = new Rectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
            spriteBatch.Draw(test, rec, Color.White);
        }

        // draw level 1
        else if (gameState == GameState.Play)
        {
            DrawScenery();

            platform1.Draw(spriteBatch);
            platform2.Draw(spriteBatch);
            platform3.Draw(spriteBatch);
            platform4.Draw(spriteBatch);
            platform5.Draw(spriteBatch);
            platform6.Draw(spriteBatch);
            platform7.Draw(spriteBatch);
            platform8.Draw(spriteBatch);
            platform9.Draw(spriteBatch);
        }

        spriteBatch.End();

        base.Draw(gameTime);
    }

    private void DrawScenery()
    {
        Rectangle backgroundRectangle = new Rectangle(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
        spriteBatch.Draw(skyBackground, backgroundRectangle, Color.White);
    }
}

1 个答案:

答案 0 :(得分:1)

在您的平台更新中执行此操作。

public void Update(GameTime gameTime)
{
    xCoord += 5;
    // to do: reverse direction when it reaches limits
}