2013-07-26 163 views
2

我正在研究一个pong游戏,因为我是编程新手,我不知道如何获取另一个类变量。我有单独的课程,绿色和蓝色的桨,一个球,和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); 
    } 

} 

一个桨类(他们看起来exept的名称和移动键相同):

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

回答

2

声明你想作为公共访问的变量,或者创建get方法。

对于公共变量,你会怎么做:

public Vector2 Position; 

并访问它,你会叫:

Ball ball; 
ball.Position 

对于get方法实现:

public Vector2 getPosition() 
{ 
    return Position; 
} 

你会打电话该方法来获得该位置。

+1

谢谢你,我很高兴你没有发布固定代码,现在我知道如何自己做这个,非常感谢你:D –

+0

你应该投票并将问题标记为已回答 – Rwiti