locked
Unwanted image changing between 2 picture boxes RRS feed

  • Question

  • Hi, I started computing at college about a month ago and have been learning to program in Visual Basic 2008 Express. I decided one weekend that I would try and make a pretty simple game that I may be able to use for my coursework at some point (I know Visual Basic isn't everyone's first choice for games programming but I thought I could try), I have learn a great deal from doing this but have encountered a rather odd bug in the program.

     The game plays a bit like galaga (closest thing I can think of) but the player has to avoid or shoot asteroids - however, when you shoot either of the first two asteroids then the asteroid you shot will take on the same image as the other asteroid. So if I shot asteroid1 then the picture box that should be showing the image of asteroid1, now shows asteroid2. I have had my computing teacher look through the code and she can't see anything wrong. It's so annoying that everything was going so well and I was learning alot from it but I can't find the problem.

    I will post the code if people think it would help but I would much rather just email the folder to someone as then they could see it for themselves.
    Any help that people can give me would be GREATLY appreciated.
    Wednesday, October 14, 2009 6:46 PM

Answers

  • Dang,  Sorry for the long delay in getting back to you.  I had forgot about this thread.

    I took a look and here is the problem:

    When you initialize your asteroids you set the images backwards.  So, they start switched but your collision code sets them back to the correct image so they appear to the user to switch inappropriately.

    Check your Form1.Designer.vb for these:

    Me.pctAsteroid1.Image = Global.Asteroid_Game.My.Resources.Resources.Asteroid2
    Me.pctAsteroid2.Image = Global.Asteroid_Game.My.Resources.Resources.Asteroid1

    now compare that to your collision code in AsteroidExplosionTimer_Tick():

    pctAsteroid1.Image = My.Resources.Asteroid1
    pctAsteroid2.Image = My.Resources.Asteroid2


    • Marked as answer by Fulch36 Thursday, December 3, 2009 7:42 PM
    Thursday, December 3, 2009 7:27 PM

All replies

  • Can you just post the code that is used to change the images displaying in the pictureboxes?
    Matt Kleinwaks - MSMVP MSDN Forums Moderator - www.zerosandtheone.com
    Wednesday, October 14, 2009 6:53 PM
  • If lblAsteroidNumber.Text = "1" Then
                pctAsteroid1.Image = My.Resources.Asteroid1
                pctAsteroid1.Location = New Point(RandomNumber(Me.Width - pctAsteroid1.Width), -105)
                pctAsteroid1.Enabled = True
                lblAsteroidNumber.Text = ""
                AsteroidExplosionTimer.Enabled = False
            End If

            If lblAsteroidNumber.Text = "2" Then
                pctAsteroid2.Image = My.Resources.Asteroid2
                pctAsteroid2.Location = New Point(RandomNumber(Me.Width - pctAsteroid2.Width), -115)
                pctAsteroid2.Enabled = True
                lblAsteroidNumber.Text = ""
                AsteroidExplosionTimer.Enabled = False
            End If

    This is the bit that actually changes the images, the value that is in lblAsteroidNumber is assigned when this sub routine is called:

    Private Sub Asteroids(ByVal AsteroidNumber)
            lblAsteroidNumber.Text = AsteroidNumber
        End Sub

    AsteroidNumber is dimensioned under the public class as an integer.
    Wednesday, October 14, 2009 7:01 PM
  • If you want to zip it up and place it someplace I'll be happy to take a peak.
    Wednesday, October 14, 2009 7:31 PM
  • I would set a breakpoint to verify that I was not sending the wrong asteroid ID to the Asteroids function. Although lines like this are really a lot less clear to the compiler than they are to us:

        lblAsteroidNumber.Text = AsteroidNumber

    Text is a string, AsteroidNumber is a number. Ordinarily, you would use something along the lines of AsteroidNumber.ToString() There is an intrinsic difference between "1" and 1...its significant in programming. (Option strict will not even allow you to try this. Its dangerous, unless the compiler makes an assumption, which...I don't like.) It is an implicit conversion...not simply a cast, as you might assume. Using ToString is an explicit conversion, which is much safer, and demonstrates your intent to the compiler.
    • Proposed as answer by MaDFroG20091013 Wednesday, October 21, 2009 1:51 AM
    Wednesday, October 14, 2009 7:52 PM
  • Thanks to both of you! jgalley I uploaded it to megaupload (I prefer it to rapidshare as I still get full download speed for free) as a zip file:

    http://www.megaupload.com/?d=TC4T9JRC

    jinzai, I was planning to do the breakpoint thing if no-one could come up with a reason to explain why it was happening - however I do appreciate the input on improving the code, I have used ToString when dealing with a similar bit of code in the program and will now also be using it here - Thanks!!
    Thursday, October 15, 2009 6:37 PM
  • How about the issue now? I think the code you posted are right. There are must be something wrong in other place.
    Wednesday, October 21, 2009 1:52 AM
  • Still not solved, my teacher is getting me to comment all the code then print it out and she will pass it round the whole ICT department at College. So if that still doesn't work then I can think of a few things to try such as changing the images (or just deleting them and then adding them to the resources again) if none of this works then I can think of a VERY "dirty" way of doing it. I didn't give the full details earlier - the images will change back if you shoot one then the other then before shooting the original again. So if I can find no other way to do it then when the user starts the game I can get the bullet to hit asteroids before the come into the form (they "spawn" off screen). Despite all of this I would still appreciate any input.
    Wednesday, October 21, 2009 2:49 PM
  • Dang,  Sorry for the long delay in getting back to you.  I had forgot about this thread.

    I took a look and here is the problem:

    When you initialize your asteroids you set the images backwards.  So, they start switched but your collision code sets them back to the correct image so they appear to the user to switch inappropriately.

    Check your Form1.Designer.vb for these:

    Me.pctAsteroid1.Image = Global.Asteroid_Game.My.Resources.Resources.Asteroid2
    Me.pctAsteroid2.Image = Global.Asteroid_Game.My.Resources.Resources.Asteroid1

    now compare that to your collision code in AsteroidExplosionTimer_Tick():

    pctAsteroid1.Image = My.Resources.Asteroid1
    pctAsteroid2.Image = My.Resources.Asteroid2


    • Marked as answer by Fulch36 Thursday, December 3, 2009 7:42 PM
    Thursday, December 3, 2009 7:27 PM
  • Thank you so very much! I was a bit disappointed when no replied but that no longer matters because that fixed it! Thank you greatly for pointing out my, quite frankly, stupid mistake. I went on to massively modify this program to save the scores to a text file in binary which was then checksummed and did various other things but never actually solved this issue - unfortunately I handed the project in to be marked earlier this week but I can drop the now fixed version off tomorrow morning.

    Thanks to everyone that posted!
    Thursday, December 3, 2009 7:47 PM