c# - 访问其他类变量

标签 c# class variables xna get

我正在开发乒乓球游戏,因为我是编程新手,所以我不知道如何访问另一个类变量。我有单独的类、绿色和蓝色 Racket 、一个球和 game1.cs。

我用 bool movingUp, movingLeft 控制球的移动;

它会从屏幕边框反弹,但我不知道如何使其与桨配合使用。基本上,我如何检查桨的位置,并使得当球接触桨时,它会弹开?我的意思是,如何检测碰撞?

public class Ball
{
    ParticleEngine particleEngine;
    GraphicsDeviceManager graphics;
    Texture2D texture;
    Vector2 position;
    Boolean movingUp, movingLeft;
    Vector2 origin;

    public Ball()
    {
        position = new Vector2(800 / 2, 330);
    }

    public void LoadContent(ContentManager Content)
    {
        texture = Content.Load<Texture2D>("ball");
        movingLeft = true;
        //Particle Engine
        List<Texture2D> textures = new List<Texture2D>();
        textures.Add(Content.Load<Texture2D>("pixel"));
        particleEngine = new ParticleEngine(textures, new Vector2(400, 240));
    }

    public void Update(GameTime gameTime)
    {
        float speed = 2.5f;

        //Physics
        if (movingUp)
        {
            position.Y -= 3;
        }

        if (movingLeft)
        {
            position.X -= 3;
        }

        if (!movingUp)
        {
            position.Y += 3;
        }

        if (!movingLeft)
        {
            position.X += 3;
        }

        if (position.X <= 0 && movingLeft) movingLeft = false;
        if (position.Y <= 85 && movingUp) movingUp = false;

        if (position.X >= 800 - texture.Width && !movingLeft) movingLeft = true;
        if (position.Y >= 500 - texture.Height && !movingUp) movingUp = true;

        origin = new Vector2(position.X + texture.Width / 2, position.Y + texture.Height / 2);

        //Particles
        particleEngine.EmitterLocation = new Vector2(origin.X, origin.Y);
        particleEngine.Update();
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        particleEngine.Draw(spriteBatch);
        spriteBatch.Draw(texture, position, Color.White);
    }

}

桨类之一(除了名称和移动键之外,它们看起来相同):

public class GreenPaddle
{
    Texture2D texture;
    Vector2 position;
    float speed = 2f;
    public GreenPaddle()
    {
       position = new Vector2(10, 230);
    }
    public void LoadContent(ContentManager Content)
    {
        texture = Content.Load<Texture2D>("greenpaddle");
    }
    public void Update(GameTime gameTime)
    {
        KeyboardState keyState = Keyboard.GetState();
        //Check If Keys Are Pressed // Movement
        if (keyState.IsKeyDown(Keys.W))
            position.Y -= speed;
        if (keyState.IsKeyDown(Keys.S))
            position.Y += speed;
        //Check Border
        if (position.Y < 87)
        {
            position.Y = 87;
        }
        if (position.Y > 396)
        {
            position.Y = 396;
        }
     }
     public void Draw(SpriteBatch spriteBatch)
     {
        spriteBatch.Draw(texture, position, Color.White);
     }
}

预先感谢,我真的很想学习这样的东西:D

最佳答案

将您想要访问的变量声明为公共(public)变量,或创建 get 方法。

对于公共(public)变量,你会这样做:

public Vector2 Position;

要访问它,您可以调用:

Ball ball;
ball.Position

对于 get 方法实现:

public Vector2 getPosition()
{
    return Position;
}

您将调用该方法来获取位置。

关于c# - 访问其他类变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17883303/

相关文章:

c# - 使一个类只拥有预定义的状态

c# - 在 C# 中切换应用程序,如任务管理器

javascript - 在 Javascript 中从子方法调用父方法。

javascript - javascript中继承类的最佳实践

css - 动态引用 sass/scss 变量

c# - 检查文件是否为 .NET 程序集

c# - ASP.NET MVC3 : DisplayTemplates does not show model value (MVC3 Partial Page)

c++ - Const 对 Copy Constructor 重要吗?

codeigniter - 如何将多个变量放入 Codeigniter 的语言文件中

bash - 在 Bash 中,这里文档如何包含变量,然后存储在变量中?