2012-05-17 25 views
0

所以我在一個突破遊戲和迄今爲止一切都進展順利,但是當我讓球的圖片框離開屏幕我似乎得到無限量的消息框。爲什麼我得到不間斷的消息框?

here is the code snippet

using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
public partial class Form1 : Form 
{ 
    List<Brick> bricks = new List<Brick>(); 
    Ball _ball; 
    Paddle _paddle; 
    DateTime startTime = DateTime.Now; 
    DateTime startTime2 = DateTime.Now; 

    float ballVelocityX = 0; 
    float ballVelocityY = -1; 
    float ballSpeed = 20; 

    public Form1() 
    { 
     InitializeComponent(); 
     timer1.Enabled = true; 
     timer2.Enabled = true; 
     startTime = DateTime.Now; 
     System.Media.SoundPlayer sndPlayer; 
     sndPlayer = new System.Media.SoundPlayer(Properties.Resources._01_Calm_1); 
     foreach (PictureBox brick in panel1.Controls) 
     { 
      if (brick.Name != "ball" && brick.Name != "paddle") 
       bricks.Add(new Brick(brick)); 
      else if (brick.Name == "ball") 
       _ball = new Ball(brick); 
      else if (brick.Name == "paddle") 
       _paddle = new Paddle(brick); 
      //sndPlayer.Play(); 

     } 
    } 

    private void gameTimer_Tick(object sender, EventArgs e) 
    { 
     checkBrickCollision(); 
     checkPaddleCollision(); 
     checkBallScreenBounds(); 

     ball.SetBounds(ball.Location.X + (int)(ballVelocityX * ballSpeed), 
      ball.Location.Y + (int)(ballVelocityY * ballSpeed), 
      ball.Size.Width, ball.Size.Height); 
     paddle.SetBounds(Cursor.Position.X - panel1.Location.X, paddle.Location.Y, 
      paddle.Size.Width, paddle.Size.Height); 
    } 

    private void checkBallScreenBounds() 
    { 
     if (_ball.BallRectangle.Location.X + 25 > panel1.Size.Width) 
      ballVelocityX *= -1; 
     else if (_ball.BallRectangle.Location.X < 0) 
      ballVelocityX *= -1; 
     else if (_ball.BallRectangle.Location.Y < 0) 
      ballVelocityY *= -1; 
     else if (_ball.BallRectangle.Location.Y > panel1.Size.Height) 
      ballOffScreen(); 
    } 

    private void ballOffScreen() 
    { 
     new_start(); 
     Startnew startnew = new Startnew(); 
     startnew.Show(); 
     this.Close(); 
    } 

    private void new_start() 
    {    
     TimeSpan elapsedTime = DateTime.Now - startTime2; 
     DialogResult result; 
     result = MessageBox.Show("Your total time is " + ":" + elapsedTime.Minutes + elapsedTime.Seconds 
      + " would you like to play aigan?", "Application1", MessageBoxButtons.YesNo); 
     if (result == DialogResult.No) 
     { 
      Form1 form1 = new Form1(); 
      form1.Show(); 
     } 
     else if (result == DialogResult.Yes) 
     { 
      this.Close(); 
     } 

     //MessageBox.Show("Total time is " + elapsedTime.Minutes + ":" + elapsedTime.Seconds); 

    } 

    private void checkPaddleCollision() 
    { 
     int tmpBallVelocityX = _paddle.CheckPaddleMovement(); 

     if (_ball.BallRectangle.IntersectsWith(_paddle.PaddleRectangle)) 
     { 
      ballVelocityX = tmpBallVelocityX; 
      ballVelocityY *= -1; 
     } 
    } 

    private void checkBrickCollision() 
    { 
     Rectangle ballRectangle = _ball.BallRectangle; 

     foreach (Brick brick in bricks) 
      if (brick.IntersectWith(ballRectangle)) 
      { 
       bricks.Remove(brick); 
       ballVelocityX *= -1; 
       ballVelocityY *= -1; 
       break; 
      } 
    } 

    private void panel1_MouseEnter(object sender, EventArgs e) 
    { 
     Cursor.Hide(); 
    } 

    private void panel1_MouseLeave(object sender, EventArgs e) 
    { 
     Cursor.Show(); 
    } 
    public void Form_closse(object sender, FormClosedEventArgs e) 
    { 
     //ProgramInfo.ProgramState = State.Splash_Screen; 
    } 

    private void panel1_Paint(object sender, PaintEventArgs e) 
    { 

    } 
    private void timer1_tick(object sender, EventArgs e) 
    { 
     LabelTime.Text = "It is " + DateTime.Now; 
    } 
    private void timer2_tick(object sender, EventArgs e) 
    { 
     TimeSpan elapsedTime = DateTime.Now - startTime; 
     labelElapsedTime.Text = "Time Elapsed " + elapsedTime.Minutes + ":" + elapsedTime.Seconds; 
     } 
    } 
    } 
    ` #**EDIT**#` 
    `**because things were getting a bit confusing i posted all my code.**` 

香港專業教育學院有一個按鈕,這樣做,它工作正常,所有的,但我需要彈出一次。有沒有辦法只讓它調用一次?

+0

我的猜測是,你的球繼續它已經離屏,並且每次移動它時會導致ballOffScreen()被調用後移動。你有沒有任何代碼在球離開屏幕後停止移動球? – Peter

+0

是的,先生,這正是最新發生的事情,可悲的部分是我似乎不知道如何編寫代碼,以防止它認爲它在無限循環上。 –

回答

1

如果NEW_START()爲Form1的一部分,

以下行類似的罪魁禍首,但需要仔細一看更多的代碼。

if (result == DialogResult.No) 
    { 
     Form1 form1 = new Form1(); 
     form1.Show(); //This line is creating endless messagebox on selecting NO 
    } 

UPDATE基於對評論信息,

你可以用標誌

private bool checkNewGame = false; 
private void new_start() 
{   
    if(checkNewGame) return; 
    checkNewGame = true;  
    TimeSpan elapsedTime = DateTime.Now - startTime2; 
    DialogResult result; 
    result = MessageBox.Show("Your total time is " + ":" + elapsedTime.Minutes + elapsedTime.Seconds 
     + " would you like to play aigan?", "Application1", MessageBoxButtons.YesNo); 
    if (result == DialogResult.No) 
    { 
     Form1 form1 = new Form1(); 
     form1.Show(); 
    } 
    else if (result == DialogResult.Yes) 
    { 
     checkNewGame = true; 
     this.Close(); 

    } 

更新2

而不是控制NEW_START功能

if (result == DialogResult.No) 
{ 
    Form1 form1 = new Form1(); 
    form1.Show(); //This line is creating endless messagebox on selecting NO 
} 

這樣做

if (result == DialogResult.No) 
{ 
    this.Clear(); // Create new function Clear, and clear all the Form state. 
} 

void clear() 
{ 
    ... //RESET all form state 
} 
+0

我可以給你更多的代碼,但即使與form1.show它也會做同樣的事情,即時通訊只是使用它,所以當球離開屏幕,他們打到對話結果是肯定會重置自己。 @Tilak –

+0

你使用任何計時器。還有,多少次new_start()或ballOffScreen被調用?你的預期行爲是什麼 – Tilak

+0

是的,我使用3個計時器,一個用於遊戲,另外2個用於這個時間。我只把它叫做一次,但似乎沒有按計劃進行。我的期望行爲是,一旦球離開控制箱,該消息對話框結果框將被調用,並且根據答案,它會關閉或回調以重置遊戲並開始新一輪。 –

0

最有可能的代碼應該重新啓動遊戲並沒有明確的「球閉屏」的條件和您的遊戲立即去終結比賽的。

注:調試應立即顯示出行爲的原因...

+0

不,我跑這是脫鉤模式它什麼也沒有顯示。我想我可能會知道爲什麼要這樣做,我肯定也許它與邊界有關。球可能會一直想着它會走出界限和無限的時間,無論如何,我可以創造一些方法來阻止它? –

+0

沒有代碼可以調整新遊戲的位置。請確保新遊戲開始時球是在界限內。 –

相關問題