2017-03-11 2 views
0

Ich mache ein Ziegelsteinbruch Spiel in C# Windows-Form, in der ich ein Bildfeld entfernen muss, wenn der Ball (Bild-Box) damit kollidiert ........ Ich habe versucht, zu verstecken, beseitigen Kontrolle entfernen und sogar gleich null, aber alle von ihnen nur verstecken und macht es nicht verschwunden bedeutet, dass es immer noch Kollision ist ..... hier ist mein Code:C# Entfernen eines Objekts vollständig (keine Kollision)

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 WindowsFormsApplication20 
    { 
    public partial class Form1 : Form 
    { 
    public int spx =10; 
    public int spy = 10; 
    public Form1() 
    { 
     InitializeComponent(); 
     timer1.Enabled = true; 
     Cursor.Hide(); 
     this.FormBorderStyle = FormBorderStyle.None; 
     this.TopMost = true; 
     this.Bounds = Screen.PrimaryScreen.Bounds; 
     paddle.Top = background.Bottom - (background.Bottom/10); 
     if(paddle.Left > background.Left) 
     { 
      paddle.Left += 0; 
     } 

    } 
    private void Form1_ControlRemoved(object sender, ControlEventArgs e) 
    { 

    } 
    private void timer1_Tick(object sender, EventArgs e) 
    { 
     ball.Left += spx; 
     ball.Top += spy; 
     paddle.Left = Cursor.Position.X - (paddle.Left/Width); 
     if(ball.Left <= background.Left) 
     { 
      spx = -spx; 
     } 
     if (ball.Right >= background.Right) 
     { 
      spx = -spx; 
     } 
     if (ball.Top <= background.Top) 
     { 
      spy = -spy; 
     } 
     if (ball.Bottom >= background.Bottom) 
     { 
      spy = -spy; 
     } 


     if (paddle.Top <= ball.Bottom && paddle.Top >= ball.Top && ball.Left >= paddle.Left && ball.Right <= paddle.Right) 
     { 
      spy = -spy; 
     } 
     if (pictureBox.Top <= ball.Bottom && pictureBox.Bottom >= ball.Top && ball.Left <= pictureBox.Right && ball.Right >= pictureBox.Left) 
     { 
      spy = -spy; 
      background.Controls.Remove(pictureBox); 
      pictureBox = null; 
     } 
    } 

    private void Form1_KeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.Escape) 
     { 
      this.Close(); 
     } 
    } 


} 
} 
+0

Haben Sie versucht auch, damit es seine Position bewegt ist nicht mehr in Form? –

Antwort

0

Ihr Code, der auf Kollisionen prüft, ist fest codiert, um pictureBox zu betrachten. Es unsichtbar zu machen, ändert das nicht.

Wenn Sie die Visible Eigenschaft pictureBox verwenden möchten, es zu verbergen, dann sollten Sie diese if Bedingungen ändern, es zu ignorieren, wenn sie nicht sichtbar ist:

if (pictureBox.Visible && (pictureBox.Top <= ball.Bottom && pictureBox.Bottom >= ball.Top && ball.Left <= pictureBox.Right && ball.Right >= pictureBox.Left)) 
    { 
     spy = -spy; 
     pictureBox.Visible = false; 
    } 
Verwandte Themen