2013-07-29 8 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이며, 각 이미지/타일 200x200 크기입니다.

답변

0

공이 충돌 할 때 속도가 0.5 증가합니다. 그러나 히트 박스를 움직이면 속도가 반올림됩니다. 이것은 히트 박스가 각 프레임마다 1 개씩 움직이지만 0.5로 공을 이동한다는 것을 의미합니다.

+0

아, 어떻게 수정해야할까요? 나는 시도했지만 succed하지 않았다. x) –

+0

당신은 단지 0.5 대신에 1만큼 속도 증가를 만들 수 있었다. – davidsbro