Diposing a picturebox from a form
Diposing a picturebox from a form
Diposing a picturebox from a form
Diposing a picturebox from a form
Diposing a picturebox from a form
Diposing a picturebox from a form Diposing a picturebox from a form Diposing a picturebox from a form Diposing a picturebox from a form Diposing a picturebox from a form Diposing a picturebox from a form Diposing a picturebox from a form Diposing a picturebox from a form
Diposing a picturebox from a form Diposing a picturebox from a form
Diposing a picturebox from a form
Go Back  Xtreme Visual Basic Talk > > > Diposing a picturebox from a form


Reply
 
Thread Tools Display Modes
  #1  
Old 06-24-2011, 02:32 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default Disposing a picturebox from a form


Hi there, I've tried so many ways to get rid of picture boxes for my space invaders game, yet it's not working. The bullet hits the alien, the alien goes invisible (which it should) but it doesn't go to a new location or get disposed. And the bullet then intersects with it again but because it's invisible the user just see the bullet reset out of no where and the score gets added which it shouldn't.

Code:
 If Bullet1.Bounds.IntersectsWith(AlienArray(0).Bounds) Or Bullet2.Bounds.IntersectsWith(AlienArray(0).Bounds) Or Bullet3.Bounds.IntersectsWith(AlienArray(0).Bounds) Or Bullet4.Bounds.IntersectsWith(AlienArray(0).Bounds) And AlienHitArray(0) = False Then
            AlienArray(0).Visible = False
            AlienArray(0).Location = New Point(SpaceInvadeX_Dialog.DeadAlienArea) ' equal a new point on the dialog (hidden)
            AlienHitArray(0) = True
            BulletIntersectionResults()
End if
So, basically my question is how do I remove aliens (pictureboxes) off the board or disabled the picture box in space invaders when they are hit. (my bullet collision works fine). Thanks

Last edited by 45minutes; 06-24-2011 at 03:29 AM.
Reply With Quote
  #2  
Old 06-24-2011, 03:36 AM
DrPunk's Avatar
DrPunkDiposing a picturebox from a form DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Wouldn't adding the check of AlienArray(0).Visible to the collision detection at least get you round the problem of the bullet colliding with hidden aliens?

As for removing pictureboxes, you just have to remove the control from the form's control collection and then dispose of it.
__________________
There are no computers in heaven!
Reply With Quote
  #3  
Old 06-24-2011, 03:38 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Quote:
Originally Posted by DrPunk View Post
Wouldn't adding the check of AlienArray(0).Visible to the collision detection at least get you round the problem of the bullet colliding with hidden aliens?

As for removing pictureboxes, you just have to remove the control from the form's control collection and then dispose of it.
Oh dam, never thought of that... I was thinking to my self the whole time i'm missing something, there must be an easier way. I'll give this concept a try, making it check for it's visibility. It sucks that the other ways I tried never worked. I did have a variable saying when an alien gets hit, it must be true to execute. Maybe I'll do two if statements.

EDIT: New code, still doesn't work. This time it DOES detect the bullet though the alien doesn't disappear and the score is not added (basically the if statement is being executed but nothing inside the if statement.

Code:
 If (AlienArray(0).Visible = True And Bullet1.Bounds.IntersectsWith(AlienArray(0).Bounds) Or Bullet2.Bounds.IntersectsWith(AlienArray(0).Bounds) Or Bullet3.Bounds.IntersectsWith(AlienArray(0).Bounds) Or Bullet4.Bounds.IntersectsWith(AlienArray(0).Bounds)) Then
            AlienArray(0).Visible = False
            AlienHitArray(0) = True
            score = score + 10
            numscore.Text = score
        End If


Code:
Sub Bullet_Collision_Detection()

        For I As Integer = 0 To 27
            If (AlienArray(I).Visible = True And Bullet1.Bounds.IntersectsWith(AlienArray(I).Bounds) Or Bullet2.Bounds.IntersectsWith(AlienArray(I).Bounds) Or Bullet3.Bounds.IntersectsWith(AlienArray(I).Bounds) Or Bullet4.Bounds.IntersectsWith(AlienArray(I).Bounds)) Then
                AlienArray(I).Visible = False
                AlienHitArray(I) = True
                score = score + 10
                numscore.Text = score
            End If

            If I = 27 Then ' keep loop going until game over
                I = 1
            Else
                I += 1
            End If
        Next
    End Sub
I coded this in a loop, yet it doesn't do what I want as stated above. the bullets don't make the aliens go invisible. I used breakpoints and noticed that I goes up by 2 every time I press play which I thought is odd, as I only set the I to increment by 1. If it helps, I have this sub being executed within a timer for all the alien and spaceship movement.

Last edited by 45minutes; 06-24-2011 at 05:27 AM.
Reply With Quote
  #4  
Old 06-24-2011, 09:36 AM
passel's Avatar
passelDiposing a picturebox from a form passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,028
Default

You're using I as the enumerator of your For Loop
For I as Integer = 0 to 27

So, I will be incremented from 0 to 27
In your loop you are also messing with I (you shouldn't).
So the For loop increments it and you increment it, so it goes up by 2.
If you want to loop forever, just do a Do Loop, and control I in the loop as you are.
Code:
Do
  If blah blah
  End If

  If I = 27 Then
    I = 1
  Else
    i += 1
  End If
Loop
My question, is what happend to 0. You reset I to 1, so you only process the 0 index once in the whole game.

Also, you need to group your AND and OR conditions to do what you want, other wise they are just applied left to right.
So your AND with be ORed to the first bullet, and will prevent that bullet from being tested if it is not visible, but it won't prevent the other bullet conditions from being TRUE
If Visible AND (Bullet1 Or Bullet2 Or Bullet3) Then
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.

Last edited by passel; 06-24-2011 at 09:43 AM.
Reply With Quote
  #5  
Old 06-24-2011, 09:54 AM
DrPunk's Avatar
DrPunkDiposing a picturebox from a form DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

That reseting of i in there is my fault from a discussion a while back on how to make a number increment like a for loop but then reset and carry on looping. So i was getting incremented and reset inside a Do While loop.

But I never expected it to be copied into a for loop.

Some people try to fly before they can crawl.
__________________
There are no computers in heaven!
Reply With Quote
  #6  
Old 06-24-2011, 09:55 AM
AtmaWeapon's Avatar
AtmaWeaponDiposing a picturebox from a form AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

You've got your boolean logic mixed up.

Your statement is as follows:
Code:
If A And B Or C Or D...
That's going to be checked in order with no short-circuits. If you put it all together, this is what the checks become:
Code:
$result = A And B
$result = $result Or C
$result = $result Or D
So the only bullet that checks if the alien is visible is the first one; the rest always collide.

You probably wanted something more like:
Code:
(A And B) Or (A And C) Or (A And D)...
That's tedious isn't it? Some rudimentary logic can help you simplify it. Note that every AND term depends on A, and they're ORed together. You can pull all of the AND terms out:
Code:
A And (B Or C Or D)
The Or terms still represent some tedious typing. If only there were some way to put 4 identical variables in a data structure where you could use a number to refer to them; then you could use a loop... Of course I mean you should have a bullet array.

<aside>
*ANY* time you find yourself making variables with the pattern A1, A2, A3 and you mean for them to be treated in a group, stop typing variable names and make them an array. The only time it makes sense to separate them is when you intentionally want to make it impossible to use them in a loop; odds are the names are wrong in that case.
</aside>

If it were me, I'd write it like this:
Code:
Function DoesBulletCollideWithAlien(Dim alien As ???, Dim bullet As ???)
    Return alien.Visible AndAlso bullet.Bounds.IntersectsWith(alien.Bounds)
End Function
Then your code could look like this:
Code:
For alienIndex As Integer = 0 To AlienArray.Length - 1
    For bulletIndex As Integer = 0 To _bullets.Length - 1
        Dim alien = AlienArray(alienIndex)
        Dim bullet = _bullets(bulletIndex)
        If DoesBulletCollideWithAlien(alien, bullet) Then
            alien.Visible = False
            ...
        End If
    End If
End If
Now the collision logic is nicely hidden behind a name, and we only have to write the code to check for one bullet at a time.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #7  
Old 06-25-2011, 06:44 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Quote:
Originally Posted by AtmaWeapon View Post
You've got your boolean logic mixed up.

If it were me, I'd write it like this:
Code:
Function DoesBulletCollideWithAlien(Dim alien As ???, Dim bullet As ???)
    Return alien.Visible AndAlso bullet.Bounds.IntersectsWith(alien.Bounds)
End Function
Then your code could look like this:
Code:
For alienIndex As Integer = 0 To AlienArray.Length - 1
    For bulletIndex As Integer = 0 To _bullets.Length - 1
        Dim alien = AlienArray(alienIndex)
        Dim bullet = _bullets(bulletIndex)
        If DoesBulletCollideWithAlien(alien, bullet) Then
            alien.Visible = False
            ...
        End If
    End If
End If
Now the collision logic is nicely hidden behind a name, and we only have to write the code to check for one bullet at a time.
Hi,

I need some help understanding parts of your code. First off, when you make a function, what's different between a sub and function and when should it be used? Also I used your code but changed alien As picturebox and bullet As picturebox (because thats what they are, however for dim alien it comes up with the Dim underlined and saying error "Specifiers valid only at the beginning of a declaration." what does this mean?


Code:
  Function DoesBulletCollideWithAlien(Dim alien As Picturebox, Dim bullet As Picturebox)

        Return alien.Visible AndAlso bullet.Bounds.IntersectsWith(alien.Bounds)

    End Function
Also I made an array of bullets called BulletArray(4)

Code:
 For alienIndex As Integer = 0 To AlienArray.Length - 1
            For bulletIndex As Integer = 0 To BulletArray.Length - 1
                Dim alien = AlienArray(alienIndex)
                Dim bullet = BulletArray(bulletIndex)
                If DoesBulletCollideWithAlien(alien, bullet) Then
                    alien.Visible = False
                    AddScore()
                End If
            Next
        Next
What do the two dim statements above declare Alien and bullet as? is it just a item in either respective array? also for the line "If DoesBulletCollideWithAlien(alien, bullet) Then" is that reading the function and seeing if the any alien in alienArray is visible andAlso if it is hit by any bullet in bullet array then ... alien.Visible = False, AddScore(). Else if alien is NOT visible then do nothing, i.e do not add score and do not make bullet collide with an invisible alien. (this part does not have to be coded right?)

Also is running two For Next loops safe? Do For Next loops run through a loop once or continuously check every index until program ends? And one last thing you see this line "AlienArray.Length - 1" does that mean I have to code the last alien individually separate from the loop, as it cannot be coded logically without the bounds of the loop/array?

Sorry if I haven't thought this through much, it's just I have a week deadline to meet and I have documentation to do as well. Thank you.
Reply With Quote
  #8  
Old 06-25-2011, 03:02 PM
AtmaWeapon's Avatar
AtmaWeaponDiposing a picturebox from a form AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Quote:
First off, when you make a function, what's different between a sub and function and when should it be used?
A Sub does something and that's it. A Function does something and returns a value. The difference is subtle, and I've seen people abuse ByRef parameters to make Subs return a value but it's bad practice.

Subs and Functions (.NET programmers call them "methods") are used to help you organize your program into logical units. You could theoretically write entire applications as one giant Sub Main. It'd be painful. So when you identify some code that performs some task that can be named and needs to be performed multiple times, it's usually a good idea to make a method.

You make a Sub when the code does something but doesn't have a result that needs to be stored for later. For example, consider this loop that creates a ListViewItem for each line in a text file:
Code:
While Not reader.EndOfStream
    Dim line As String = reader.ReadLine()
    Dim tokens() As String = line.Split()
    Dim item As New ListViewItem(tokens(0))
    item.SubItems.Add(tokens(1))
    item.SubItems.Add(tokens(2))
    ListView1.Items.Add(item)
End While
You could make this easier to read and maintain with a Sub, since there's no use of the item after it's created:
Code:
Sub CreateItemFromLine(ByVal line As String)
    Dim tokens() As String = line.Split()
    Dim item As New ListViewItem(tokens(0))
    item.SubItems.Add(tokens(1))
    item.SubItems.Add(tokens(2))
    ListView1.Items.Add(item)
End Sub
Now the loop can be simplified:
Code:
While Not reader.EndOfStream
    Dim line As String = reader.ReadLine()
    CreateItemFromLine(line)
End While
You make a Function when there's some result from the code that you'd like to track later. For example, this loop reads a string from a text file and calculates a checksum:
Code:
Dim checksums As New List(Of Integer)
While Not reader.EndOfStream
    Dim line As String = reader.ReadLine()
    Dim checksum As Integer = 0
    checksum += CInt(line(0))
    checksum += CInt(line(3))
    checksum /= CInt(line(4))
    checksums.Add(checksum)
End While
You could create a function for the checksum:
Code:
Function CalculateChecksum(ByVal line As String) As Integer
    Dim checksum As Integer = 0
    checksum += CInt(line(0))
    checksum += CInt(line(3))
    checksum /= CInt(line(4))
    checksums.Add(checksum)
    Return checksum
End Function
Again, the loop is easier to read:
Code:
Dim checksums As New List(Of Integer)
While Not reader.EndOfStream
    Dim line As String = reader.ReadLine()
    checksums.Add(CalculateChecksum(line))
End While
Could you have made it as a Sub? Sure, if the list were somewhere the Sub could affect. But if that list of checksums is only important to the method that contains this loop, it'd be silly to make a global variable out of it. Deciding between functions and subs is a bit of an art; you're only "right" if you feel like it's easier to read what you selected.

Quote:
Also I used your code but changed alien As picturebox and bullet As picturebox (because thats what they are, however for dim alien it comes up with the Dim underlined and saying error "Specifiers valid only at the beginning of a declaration." what does this mean?
The error message suggests you put something in the wrong place. Dim is for declaring local variables. You've used it in the parameter list of a function. You don't use Dim for that; you optionally select ByVal or ByRef:
Code:
Function DoesBulletCollideWithAlien(ByVal alien As PictureBox, ...
Confusing? VB's overly verbose syntax has lots of dirty corners like this. C# is much more consistent.

Quote:
What do the two dim statements above declare Alien and bullet as? is it just a item in either respective array?
It's a shortcut. In older versions of VB .NET (2005 and earlier) they would have been Object and I would deserve a wrist slap. In 2008 and later Option Infer is on by default so the compiler tries to infer the type. In this case, the answer is "whatever the type of AlienArray is". Normally I include "As ???" to indicate I've been given no clue as to the type, but in this case I was in a hurry *and* it works out just fine in the end anyway. This is especially useful when working with LINQ and delegates; some code I'm working on right now uses "Func(Of IEnumerable(Of IFoo(Of Double)), IBar(Of Double), Object)" extensively and I type it as little as I have to. I sort of miss C's typedefs.

Quote:
for the line "If DoesBulletCollideWithAlien(alien, bullet) Then" is that reading the function and seeing if the any alien in alienArray is visible andAlso if it is hit by any bullet in bullet array then ... alien.Visible = False, AddScore(). Else if alien is NOT visible then do nothing, i.e do not add score and do not make bullet collide with an invisible alien. (this part does not have to be coded right?)
The function checks if *one* alien is colliding with *one* bullet. That's why I have the double For loop: it's to check each bullet for each alien. If the bullet collides with the alien, it makes the alien invisible and calls the AddScore() method. If the bullet does not collide, nothing happens.

Quote:
Also is running two For Next loops safe?
Yes. This is very typical when you have two arrays A and B and you want to check if something happened for the combination of elements in A and B. One caveat: if the compiler even lets the two loops use the same variable, the end results could make you sad. In other words, don't do this:
Code:
For i As Integer = 1 To 10
    For i As Integer = 10 To 10
Quote:
Do For Next loops run through a loop once or continuously check every index until program ends?
Say you've got "For i As Integer = 1 To 10". The compiler converts it to this:
Code:
Dim i As Integer = 1
While i <= 10
    ' Do Something
    i += 1
End While
Using that knowledge, find out what happens for yourself. Here's something you can slap in a console application to try:
Code:
For x As Integer = 0 To 10
    For y As Integer = 100 To 110
        Console.WriteLine(x & " " & y)
    Next
Next
Quote:
And one last thing you see this line "AlienArray.Length - 1" does that mean I have to code the last alien individually separate from the loop, as it cannot be coded logically without the bounds of the loop/array?
Arrays start their numbering at 0. So a 1-element array only has index 0, a 2-element has 0 and 1, and so on. So an array's Length property is always 1 greater than the last index. Let's say you have 2 aliens in the array (indexes: 0, 1). If I loop from 0 to Length, I'll loop from 0 to 2. When I try to access index 2, the program will throw an IndexOutOfBoundsException. Some people use the GetUpperBound() method to get a number that doesn't need subtraction. I don't like that one because it makes you specify an array rank and that's silly for 1D arrays.

Quote:
Sorry if I haven't thought this through much, it's just I have a week deadline to meet and I have documentation to do as well.
You could have answered many of these questions very quickly by testing them out yourself. I'm not saying I'm tired of answering them (I like answering questions!) but it's almost always faster to write a quick test of a concept than it is to wait up to 24 hours for an answer from a forum. Everything but the "Dim" instead of "ByVal" question should have had about a million search results with tutorials. Learn to answer questions on your own and you'll get things done faster
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #9  
Old 06-28-2011, 07:51 PM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Turns out all I needed was 1 line of code to delete an alien. (Alienimg is an array index). Everything about what I wanted to do is running flawlessly now. Thanks for everyone's help.


Code:
  Me.Controls.Remove(alienimg) ' delete alien picturebox from form
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Diposing a picturebox from a form
Diposing a picturebox from a form
Diposing a picturebox from a form Diposing a picturebox from a form
Diposing a picturebox from a form
Diposing a picturebox from a form
Diposing a picturebox from a form Diposing a picturebox from a form Diposing a picturebox from a form Diposing a picturebox from a form Diposing a picturebox from a form Diposing a picturebox from a form Diposing a picturebox from a form
Diposing a picturebox from a form
Diposing a picturebox from a form
 
Diposing a picturebox from a form
Diposing a picturebox from a form
 
-->