Launching Projectile [Game Issue] - Printable Version +- BP Forums (https://bpforums.info) +-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55) +--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56) +---- Forum: VB.NET (Visual Basic 2010/2008) (https://bpforums.info/forumdisplay.php?fid=8) +----- Forum: Programming Help (https://bpforums.info/forumdisplay.php?fid=9) +----- Thread: Launching Projectile [Game Issue] (/showthread.php?tid=561) |
Launching Projectile [Game Issue] - Vinwarez - 07-13-2012 Hello. I am trying to write a Space Shooter game in Visual Basic. Since I've started like a half hour ago, I made the player spaceship movement and the projectile launching. The projectile launching is triggered by pressing the Space bar key. However, the same one will launch all the time. For better understanding, here are two screenshoots: Description: Shortly after the first time I press space. Description: Shortly after the second time I press space while the previous projectile was still in the form. I assume you've read the image descriptions and seen what is the problem. If you still cannot understand what is the problem, let me explain better. If I just launch the projectile, it will start moving, let's say it reached the middle of the form and now I launch it again, the previous projectile won't reach the end of the form and then disappear, it will teleport to the beginning (a bit above the Spaceship). Here is the code I use: Code: Dim pro As New PictureBox So, the main problem is accessing the variable which is inside the sub. When I finish the game, I will post the download link on this forum. Hopefully the game will be a decent one. Anyway, thanks in advance. Re: Launching Projectile [Game Issue] - brandonio21 - 07-13-2012 Well, I can definitely see your problem here! Well, the generally accepted solution to this problem would be to create an array which holds all of the bullets of the player, and every time the timer ticks, each bullet is moved. I have put what I am talking about into code: Code: Private playerBullets As List(Of PictureBox) Pastebin Version: <!-- m --><a class="postlink" href="http://pastebin.com/1t5iazRR">http://pastebin.com/1t5iazRR</a><!-- m --> So, essentially what we are doing is keeping track of all the bullets that the player has actually shot, and removing them once they go off the screen. In order to apply this procedure, simply make the timer call PlayerBullets_Act every time the timer ticks. Re: Launching Projectile [Game Issue] - Vinwarez - 07-13-2012 I get an error when I put the PlayerBullets_Act() in the Timer Tick. Here is the code snippet: Code: Private Sub Fire_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Fire.Tick Here is the error: Quote:1 Argument not specified for parameter 'e' of 'Public Sub PlayerBullets_Act(sender As Object, e As System.EventArgs)'. Re: Launching Projectile [Game Issue] - brandonio21 - 07-13-2012 Alright, just remove the (sender As Object, e As System.EventArgs) from the PlayerBullets_Act method. Re: Launching Projectile [Game Issue] - Vinwarez - 07-13-2012 I am sorry for bothering you so many times in a row, but I still get an error when I try to shoot. Code: For Each bullet As PictureBox In playerBullets Quote:Object reference not set to an instance of an object. Re: Launching Projectile [Game Issue] - brandonio21 - 07-13-2012 Ah, sorry about the faulty code that I provided you with! Change Private playerBullets As List(Of PictureBox) to this Private playerBullets As List(Of PictureBox) = New List(Of PictureBox) Re: Launching Projectile [Game Issue] - Vinwarez - 07-13-2012 Right now, I don't get any errors but nothing fires. I assume it is hard to help "blindly", a.k.a. without looking at the whole code, so I will just provide a link to it. http://pastebin.com/VV9w7D92 Re: Launching Projectile [Game Issue] - brandonio21 - 07-13-2012 Alright, thanks for providing me with that! There are a few things that you are going to want to do now. First of all, make the "Fire" timer enabled all the time. Also, change the sub Public Sub Player_Shoot(Sender as object, e as eventargs) to Public Sub Player_Shoot() Then, when the user presses space, call Player_Shoot() and delete the line that sets Fire.Enabled = true Let's see if that fixes it... Re: Launching Projectile [Game Issue] - Vinwarez - 07-13-2012 Everything works until one bullet reaches the end of the form. Code: Public Sub PlayerBullets_Act() Quote:Collection was modified; enumeration operation may not execute. Re: Launching Projectile [Game Issue] - brandonio21 - 07-13-2012 Oh okay! Alright, this is an easy fix. So this error is popping up because we are modifying the array as we are scrolling through it, so the solution to this is to create an array to keep track of the bullets that we need to remove and remove them once we are finished. So something like this... Code: Public Sub PlayerBullets_Act() Re: Launching Projectile [Game Issue] - Vinwarez - 07-13-2012 Thank you so much for putting this much effort into solving my problem! Even though this might not mean anything to you, I will put your name in the credits. Once again, huge thanks! <!-- s --><img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt="" title="Very Happy" /><!-- s --> Re: Launching Projectile [Game Issue] - brandonio21 - 07-13-2012 Haha, you're very welcome! I'm glad that it worked out for you!! |