2014-10-08 3 views
0

충돌 감지와 관련하여 다른 문제가 있습니다. 나는 현재 pictureboxes를 사용하여 낙하물을 만들었는데,이 그림 상자의 이미지는 내가 만든 imagelist로부터 무작위로 가져옵니다. 문제는 두 종류의 이미지가 있다는 것입니다. 물고기와 동전. 내가하고 싶은 것은 프로그램이 특정 충돌이있는 이미지 (동전인지 아닌지)를 인식하고 동전 절차가 묶인 레이블을 업데이트하는 것입니다. 나는 여러 가지 시도를했지만 문제의 원인을 파악할 수없는 것 같습니다. 누구든지 나를 도울 수 있습니까?충돌 감지 및 이미지 식별 문제

나는이 지금까지 코드가 있습니다 각각

'Collision 
    If PictureBox1.Bounds.IntersectsWith(play_avatar.Bounds) Then 
     If PictureBox1.ImageLocation Is My.Resources.coin Then 
      cns += 1 
      lbl_coins.Text += cns 
     End If 
     PictureBox1.Top -= Panel1.Height 
     PictureBox1.Image.Dispose() 
     PictureBox1.Image = ImageList1.Images(rno1) 
     scr += 1 
     lbl_score.Text += scr 
    End If 
    If PictureBox2.Bounds.IntersectsWith(play_avatar.Bounds) Then 
     If PictureBox1.ImageLocation Is My.Resources.coin Then 
      cns += 1 
      lbl_coins.Text += cns 
     End If 
     PictureBox2.Top -= Panel1.Height 
     PictureBox2.Image.Dispose() 
     PictureBox2.Image = ImageList1.Images(rno2) 
     scr += 1 
     lbl_score.Text += scr 
    End If 
    If PictureBox3.Bounds.IntersectsWith(play_avatar.Bounds) Then 
     If PictureBox3.ImageLocation Is My.Resources.coin Then 
      cns += 1 
      lbl_coins.Text += cns 
     End If 
     PictureBox3.Top -= Panel1.Height 
     PictureBox3.Image.Dispose() 
     PictureBox3.Image = ImageList1.Images(rno3) 
     scr += 1 
     lbl_score.Text += scr 
    End If 

이 이미지의 목록입니다 그 랜덤 기능 :

ImageList1.Images.Add(My.Resources.coin) 
    ImageList1.Images.Add(My.Resources.f1) 
    ImageList1.Images.Add(My.Resources.f2) 
    ImageList1.Images.Add(My.Resources.f3) 
    ImageList1.Images.Add(My.Resources.f4) 
    ImageList1.Images.Add(My.Resources.f5) 
    ImageList1.Images.Add(My.Resources.f6) 
    ImageList1.Images.Add(My.Resources.f7) 
    ImageList1.Images.Add(My.Resources.f8) 
    ImageList1.Images.Add(My.Resources.f9) 


Dim cns As Integer = 0 
Dim rnd As New Random 
Dim rno1 As Integer 
Dim rno2 As Integer 
Dim rno3 As Integer 

rno1 = rnd.Next(0, 10) 
rno2 = rnd.Next(0, 10) 
rno3 = rnd.Next(0, 10) 

편집 :

여기에 전체 코드입니다. 당신은 각각의 PictureBox에 대한 변수를 사용한다

1. comparing string with bitmap. Turn on option strict
2. even if you do PictureBox1.Image Is My.Resources.coin it will always be false because it is a different object

:

Imports System.IO 

Public Class EClassic 
Dim images() As String 
Dim rnd As New Random 
Dim rno1 As Integer 
Dim rno2 As Integer 
Dim rno3 As Integer 
Dim flist As New List(Of PictureBox) 
Dim dlist As New List(Of Image) 
Dim scr As Integer = 0 
Dim life As Integer = 5 
Dim cns As Integer = 0 
Dim picturebox1ImageIndex, picturebox2ImageIndex, picturebox3ImageIndex As Integer 
Private Sub EClassic_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    With flist 
     .Add(PictureBox1) 
     .Add(PictureBox2) 
     .Add(PictureBox3) 
    End With 
    lbl_score.Text = scr 
    lbl_life.Text = life 
    lbl_coins.Text = cns 

End Sub 
Private Sub EClassic_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown 
    Dim c As Integer = Panel1.ClientSize.Width 
    Dim res As Integer 
    Dim res2 As Integer 
    res2 = c - c + 100 
    res = c/2 

    Select Case e.KeyCode 
     Case Keys.Left 
      If play_avatar.Left > res2 Then 
       play_avatar.Left -= 100 
      ElseIf play_avatar.Left < res2 Then 
       play_avatar.Left -= 0 
      End If 

     Case Keys.Right 
      If play_avatar.Left < res Then 
       play_avatar.Left += 100 
      ElseIf play_avatar.Left > res Then 
       play_avatar.Left -= 0 

      End If 

    End Select 
End Sub 

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 
    Dim mov As Integer = rnd.Next(0, flist.Count - 0) 
    Me.flist(mov).Top += 20 
    'Fish - Dragon Collision 
    If PictureBox1.Bounds.IntersectsWith(play_avatar.Bounds) Then 
     PictureBox1.Image.Dispose() 
     PictureBox1.Top -= Panel1.Height 
     rno1 = rnd.Next(0, 9) 
     PictureBox1.Image = ImageList1.Images(rno1) 
     picturebox1ImageIndex = rno1 
     scr += 1 
     lbl_score.Text += scr 
     If picturebox1ImageIndex = 0 Then '0 is the index of coin in ImageList1 
      cns += 1 
      lbl_coins.Text += cns 
     End If 
    End If 
    If PictureBox2.Bounds.IntersectsWith(play_avatar.Bounds) Then 
     PictureBox2.Image.Dispose() 
     PictureBox2.Top -= Panel1.Height 
     rno2 = rnd.Next(0, 9) 
     PictureBox2.Image = ImageList1.Images(rno2) 
     picturebox2ImageIndex = rno2 
     scr += 1 
     lbl_score.Text += scr 
     If picturebox2ImageIndex = 0 Then 
      lbl_coins.Text += cns 
     End If 
    End If 

    If PictureBox3.Bounds.IntersectsWith(play_avatar.Bounds) Then 
     PictureBox3.Image.Dispose() 
     PictureBox3.Top -= Panel1.Height 
     rno3 = rnd.Next(0, 9) 
     PictureBox3.Image = ImageList1.Images(rno3) 
     picturebox3ImageIndex = rno3 
     scr += 1 
     lbl_score.Text += scr 
     If picturebox3ImageIndex = 0 Then 
      cns += 1 
      lbl_coins.Text += cns 
     End If 
    End If 

    'Fish - Line Collision 
    If PictureBox1.Bounds.IntersectsWith(line.Bounds) Then 
     life -= 1 
     PictureBox1.Top -= Panel1.Height 
     lbl_life.Text = life 
    End If 
    If PictureBox2.Bounds.IntersectsWith(line.Bounds) Then 
     life -= 1 
     PictureBox2.Top -= Panel1.Height 
     lbl_life.Text = life 
    End If 
    If PictureBox3.Bounds.IntersectsWith(line.Bounds) Then 
     life -= 1 
     PictureBox3.Top -= Panel1.Height 
     lbl_life.Text = life 
    End If 
    If life = 0 Then 
     Timer1.Stop() 
     GameOver.Show() 
     Exit Sub 
    End If 
End Sub 

최종 클래스

+2

실제 문제는 무엇입니까? 코드가 현재하고 싶은 것이 아니라 코드가 원하는 것을 명시했습니다. – Psychemaster

+0

나는 그것이 동전인지 아닌지를 프로그램이 인식하지 못한다고 생각한다. 랜덤 화 직후에 동전인지 어떻게 인식 할 수 있습니까? 아니면 무작위 화가 끝난 후에도 내가 할 것이라고 생각합니다. 나는 완전히 잃어버린 것입니다. 아마 여기서 문제가있을거야, 안 그래? 'PictureBox1.ImageLocation이 My.Resources.coin 인 경우 cns + = 1 lbl_coins.Text + = cns End If ' – Chris

+0

그 조건 이후에 cns (실제로 동전 카운터)가 업데이트되지 않았기 때문입니다. – Chris

답변

0

PictureBox1.ImageLocation는 My.Resources.coin 두 가지 방법으로 잘못인가 예 : picturebox1ImageIndex, picturebox2ImageIndex 및 비교

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 
Dim mov As Integer = rnd.Next(0, flist.Count - 0) 
Me.flist(mov).Top += 20 
'Fish - Dragon Collision 
If PictureBox1.Bounds.IntersectsWith(play_avatar.Bounds) Then 
    'PictureBox1.Image.Dispose() ' you dont need that 
    PictureBox1.Top -= Panel1.Height '<- place break point. See if this code is triggered 
    rno1 = rnd.Next(0, 9) 
    PictureBox1.Image = ImageList1.Images(rno1) '<- place break point check rno1 
    picturebox1ImageIndex = rno1 
    scr += 1 '<- place break point check picturebox1ImageIndex 
    lbl_score.Text += scr 
    If picturebox1ImageIndex = 0 Then '<- place break point 
     cns += 1 '<- place break point 
     lbl_coins.Text += cns 
    End If 
End If 
If PictureBox2.Bounds.IntersectsWith(play_avatar.Bounds) Then 
    'PictureBox2.Image.Dispose() ' you dont need that 
    PictureBox2.Top -= Panel1.Height '<- place break point. See if this code is triggered 
    rno2 = rnd.Next(0, 9) 
    PictureBox2.Image = ImageList1.Images(rno2) '<- place break point check rno2 
    picturebox2ImageIndex = rno2 
    scr += 1 '<- place break point check picturebox2ImageIndex 
    lbl_score.Text += scr 
    If picturebox2ImageIndex = 0 Then '<- place break point 
     lbl_coins.Text += cns '<- place break point 
    End If 
End If 

If PictureBox3.Bounds.IntersectsWith(play_avatar.Bounds) Then 
    'PictureBox3.Image.Dispose() ' you dont need that 
    PictureBox3.Top -= Panel1.Height '<- place break point. See if this code is triggered 
    rno3 = rnd.Next(0, 9) 
    PictureBox3.Image = ImageList1.Images(rno3) '<- place break point check rno3 
    picturebox3ImageIndex = rno3 
    scr += 1 '<- place break point check picturebox3ImageIndex 
    lbl_score.Text += scr 
    If picturebox3ImageIndex = 0 Then '<- place break point 
     cns += 1 '<- place break point 
     lbl_coins.Text += cns 
    End If 
End If 

'Fish - Line Collision 
If PictureBox1.Bounds.IntersectsWith(line.Bounds) Then 
    life -= 1 
    PictureBox1.Top -= Panel1.Height 
    lbl_life.Text = life 
End If 
If PictureBox2.Bounds.IntersectsWith(line.Bounds) Then 
    life -= 1 
    PictureBox2.Top -= Panel1.Height 
    lbl_life.Text = life 
End If 
If PictureBox3.Bounds.IntersectsWith(line.Bounds) Then 
    life -= 1 
    PictureBox3.Top -= Panel1.Height 
    lbl_life.Text = life 
End If 
If life = 0 Then 
    Timer1.Stop() 
    GameOver.Show() 
    Exit Sub 
End If 
End Sub 
+0

해결할 아이디어가 있습니까? 나는 길을 찾을 수없는 것 같아. 미안. – Chris

+0

@Chris 편집 된 참고 자료 확인 –

+0

@ γηράσκω δ 'αεί πολλά διδασκόμε 고맙습니다. 조금 문제가있는 것 같지만. 편집 : – Chris