簡體   English   中英

XNA drawRectangle.X + = 1不會移動我的Rectangle / Sprite

[英]XNA drawRectangle.X += 1 won't move my Rectangle/Sprite

我正在制作一個非常基本的Mario風格的游戲,並且我希望我的平台之一可以在屏幕上來回移動。

在我的Platforms類中,我創建了一個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類:

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

在平台更新中執行此操作。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM