2013-07-29 96 views
0

在我的乒乓球比賽後,我基本上有兩個撥片,它被設置爲反彈所有的牆/槳一球,它採用做工精細,但現在先打槳後,它開始檢測到衝突錯誤,能否請您對這個代碼來看看,看看什麼是錯的:XNA - 球碰撞檢測錯誤的第一次碰撞

Ball.cs:

public class Ball 
{ 
    GreenPaddle gPaddle; 
    BluePaddle bPaddle; 
    public Texture2D ballTexture; 
    public Vector2 ballPosition; 
    public Rectangle ballRect; 
    public float speed = 1f; 
    bool movingUp, movingLeft; 

    public Ball(GreenPaddle paddle, BluePaddle paddleb) 
    { 
     this.gPaddle = paddle; 
     this.bPaddle = paddleb; 
     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, 
      20, 20); 

    } 

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

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

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

     if (ballPosition.Y < 83) 
     { 
      movingUp = false; 
     } 
     if (ballPosition.Y >= 500) 
     { 
      movingUp = true; 
     } 

    } 


    public void CollideWithPaddles() 
     { 

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

     } 
    } 

在我Game1.cs我有這部分我可能認爲會引起一些問題:

public bool BallHitEffect() 
    { 
     //Plays an effect/animation when ball hits the paddle 
     if (gPaddle.gpRect.Intersects(ball.ballRect) || bPaddle.bpRect.Intersects(ball.ballRect)) 
     { 
      ball.speed += 0.5f;//Makes the ball go faster every paddle-hit. 
      return true; 
     } 
     else { return false; } 
    } 

在我Game1.cs更新方法,我有這樣的動畫:

//If it is not already playing and there is collision, start playing 
     if (!IsPlaying && BallHitEffect()) 
      IsPlaying = true; 
     //Increment the frameTimePlayed by the time (in milliseconds) since the last frame 
     if (IsPlaying) 
      frameTimePlayed += gameTime.ElapsedGameTime.TotalMilliseconds; 
     //If playing and we have not exceeded the time limit 
     if (IsPlaying && frameTimePlayed < animationTime) 
     { 
      animatedSprite.Update(gameTime); 
      // And increment your frames (Using division to figure out how many frames per second) 
     } 
     //If exceeded time, reset variables and stop playing 
     else if (IsPlaying && frameTimePlayed >= animationTime) 
     { 
      frameTimePlayed = 0; 
      IsPlaying = false; 
      // TODO: Possibly custom animation.Stop(), depending on your animation class 
     } 

動畫是800x400,每幅圖像/瓦爲200×200。

回答

0

的這裏的問題是,當球碰撞,您可以通過0.5提高速度。但是,當您移動hitbox時,您會繞過速度。這意味着擊中格移動1每一幀,而是由0.5

+0

哦球,我明白了,你知道如何解決它?我試過,但沒有贏得成功。X) –

+0

你可以只讓1,而不是0.5的速度增加。 – davidsbro