【问题标题】:XNA - Rectangle ErrorXNA - 矩形错误
【发布时间】:2013-07-26 23:20:21
【问题描述】:

我正在做一个乒乓球比赛,我试图做到这一点,当球撞到球拍时,球会反弹......就这么简单。但是当我使用矩形时,我认为使用if(ballRect.Intersects(gPaddle.gRect)) 是进行碰撞的最佳方式。 但是当我开始我的游戏时,一切都出错了,球消失了,桨的纹理变得奇怪了,它的一部分没有跟随桨,是的.. 这是代码:

GreenPaddle.cs:

    public class GreenPaddle
{
    public Texture2D gPtexture;
    public Vector2 position;
    public Rectangle gpRect;
    public Vector2 origin;
    public int speed = 2;

    public GreenPaddle()
    {

    }

    public void LoadContent(ContentManager Content)
    {
        gPtexture = Content.Load<Texture2D>("greenpaddle");
        position = new Vector2(20, 200);
        gpRect = new Rectangle((int)position.X, (int)position.Y,
            gPtexture.Width, gPtexture.Height);
    }

    public void Update(GameTime gameTime)
    {
        KeyboardState keyState = Keyboard.GetState();

        //Movement
        PaddleMovement();

        //Border Collision
        isCollidingWithBorders();
      }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(gPtexture, position, gpRect, Color.White);
    }

    public Boolean isCollidingWithBorders()
    {
        if (position.Y < 83 && gpRect.Y < 83)
        {
            position.Y = 83;
            gpRect.Y = 83;
            return true;
        }

        if (position.Y > 400 && gpRect.Y > 400)
        {
            position.Y = 400;
            gpRect.Y = 400;
            return true;
        }

        else { return false; }

    }

    public void PaddleMovement()
    {
        KeyboardState keyState = Keyboard.GetState();
        if (keyState.IsKeyDown(Keys.W))
        {
            position.Y -= speed;
            gpRect.Y -= speed;
        }
        if (keyState.IsKeyDown(Keys.S))
        {
            position.Y += speed;
            gpRect.Y += speed;
        }
    }
}

球.cs:

 public class Ball
{
    GreenPaddle gPaddle = new GreenPaddle();

    Texture2D ballTexture;
    Vector2 ballPosition;
    Rectangle ballRect;
    int speed = 2;
    bool movingUp, movingLeft;

    public Ball()
    {
        movingLeft = true;
        movingUp = true;
    }

    public void LoadContent(ContentManager Content)
    {
        ballTexture = Content.Load<Texture2D>("ball");
        ballPosition = new Vector2(380, 225);
        ballRect = new Rectangle((int)ballPosition.X, (int)ballPosition.Y,
            ballTexture.Width, ballTexture.Height);

    }

    public void Update(GameTime gameTime)
    {
        BallMovement();

    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(ballTexture, ballPosition, ballRect, Color.White);
    }

    public void BallMovement()
    {
        if (movingUp) { ballPosition.Y -= speed; ballRect.Y -= speed; }
        if (!movingUp) { ballPosition.Y += speed; ballRect.Y += speed; }
        if (movingLeft) { ballPosition.X -= speed; ballRect.X -= speed; }
        if (!movingLeft) { ballPosition.X += speed; ballRect.X += speed; }

        if (ballRect.Intersects(gPaddle.gpRect))
            movingLeft = false;

        if (ballPosition.Y < 85)
        {
            movingUp = false;
        }
    }
}

Game1.cs:

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;


    Texture2D BackGround;
    GreenPaddle gPaddle = new GreenPaddle();
    Ball ball = new Ball();

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

    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        BackGround = Content.Load<Texture2D>("pongBG");
        gPaddle.LoadContent(Content);
        ball.LoadContent(Content);
    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        gPaddle.Update(gameTime);
        ball.Update(gameTime);

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

        spriteBatch.Begin();
        spriteBatch.Draw(BackGround, new Vector2(0f, 0f), Color.White);
        gPaddle.Draw(spriteBatch);
        ball.Draw(spriteBatch);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}

我该怎么办?我错过了什么? 在此先感谢:)

【问题讨论】:

    标签: c# xna collision pong


    【解决方案1】:

    碰撞测试在Ball 类中。在其中,您正在测试球是否与名为 gPaddle 的类的私有成员相撞,而不是您的游戏使用的并且您更新和绘制的那个。

    替换

    GreenPaddle gPaddle = new GreenPaddle();
    

    GreenPaddle gPaddle;
    ...
    public Ball(GreenPaddle paddle)
    {
         this.gPaddle = paddle;      
    }
    

    另外,不要使用 2 个布尔值和一个标量来表示速度。使用Vector2。您的 BallMovement 方法也是错误的。您应该在球拍的交叉点上反射球,这意味着在 x 轴和 y 轴上反转球的方向:

    有 2 个布尔值:

        if (ballRect.Intersects(gPaddle.gpRect))
         {
              movingLeft = !movingLeft
              movingTop = !movingTop;
         }
    

    Vector2 为速度:

         if (ballRect.Intersects(gPaddle.gpRect))
         {
              velocity = -velocity;
         }
    

    【讨论】:

    • 非常感谢 :D 关于球的运动,它还没有完成^^ 但我会试试你的方法 :D
    • 现在又出现了另一个错误..在我的 Game1.cs 我有: Greenpaddle gPaddle = new GreenPaddle();和球球 = 新球();但它说它不能接受 0 个参数,我试着这样说: Ball ball = new Ball(GreenPaddle gPaddle) 但它不起作用...:/
    • 球球 = 新球(gPaddle);
    • 现在这样说:“字段初始化程序不能引用非静态、方法或属性”。如果我很烦人,我很抱歉,但这对我来说很新:/
    • 已经是了,但是还是不行 x(GreenPaddle gPaddle = new GreenPaddle(); Ball ball = new Ball(gPaddle);
    猜你喜欢
    • 2011-05-03
    • 1970-01-01
    • 2011-08-03
    • 2011-02-17
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多