Results 1 to 6 of 6

Thread: [RESOLVED] Checking ".Left" of whole object [Space Invaders]

  1. #1

    Thread Starter
    Member Shadow45o's Avatar
    Join Date
    Sep 2008
    Posts
    51

    Resolved [RESOLVED] Checking ".Left" of whole object [Space Invaders]

    What I mean by "Checking ".left" of whole object" is making something happen if, for instance, a label were to hit an object. Instead of it needing to hit at the objects exact left, it could hit anywhere on the object.

    Bit confusing...I know...here is an example...

    I am currently making a space invaders game with vb.net 2003 as my final project for my computer programing class. I have been having so much trouble. I have the shot in a timer and when the space bar is hit, it enables this timer.

    Code:
    Select Case e.keycode
     Case Keys.Space
                    If tmrShot.Enabled = False Then
                        tmrShot.Enabled = True'enable shot timer
                        lblShot.Location = imgShip.Location 'moves shot to ships location
                        lblShot.Left += 22 'moves shot over 22(to the right)
                        lblShot.Visible = True 'shows shot
                    End If
            End Select
    The above code is how I have it set up to enable the timer.

    Then when the timer activates it should launch the shot from the ships location.

    Here is the beginning part of my code in the timer.
    Code:
     If lblShot.Top = 12 Then
                lblShot.Location = imgShip.Location
                lblShot.Left += 22
                tmrShot.Enabled = False
                lblShot.Visible = False
            Else
                lblShot.Top -= 4
                If lblShot.Visible = True Then
                    If lblShot.Top = imgR1.Top AndAlso lblShot.Left = imgR1.Left OrElse lblShot.Left = imgR1.Left + 14 OrElse lblShot.Left = imgR1.Left + 28 Then
                        imgR1.Hide()
                        lblShot.Visible = False
                        tmrShot.Enabled = False
                        lblScore.Text = Val(lblScore.Text) + 30
                    ElseIf lblShot.Top = imgR2.Top AndAlso lblShot.Left = imgR2.Left OrElse lblShot.Left = imgR2.Left + 14 OrElse lblShot.Left = imgR2.Left + 28 Then
                        imgR2.Hide()
                        lblShot.Visible = False
                        tmrShot.Enabled = False
                        lblScore.Text = Val(lblScore.Text) + 30
                    ElseIf lblShot.Top = imgR3.Top AndAlso lblShot.Left = imgR3.Left OrElse lblShot.Left = imgR3.Left + 14 OrElse lblShot.Left = imgR3.Left + 28 Then
                        imgR3.Hide()
                        lblShot.Visible = False
                        tmrShot.Enabled = False
                        lblScore.Text = Val(lblScore.Text) + 30
    The Problem:Well first off, the first part of the check works, if the shot hits the left of any of the aliens it will hide it, disable the timer, add the score, and make the shot not visible. Here is the main problemThe other parts of the check where it is seeing if "lblShot" is equal to "imgR3.left + 14" or the same with plus 28, what will happen is that the if the shot is at the "+14" or "+28" mark, it will instantly jump up to the top red alien in that row and then hide only that one and no one else.

    To sum it up:The shot will keep jumping to the top alien unless there it is on the objects exact left. I mainly need some way so the shot can hit anywhere on the alien and then do the code that it needs to.

    Hope I made this clear enough, I was having some trouble thinking about how to describe my problem. So thanks for any help given, and ask questions if you need other information and I will try to post a reply ASAP. So thanks again
    Last edited by Shadow45o; May 20th, 2009 at 04:51 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Checking ".Left" of whole object [Space Invaders]

    Each control has a Bounds property that is type Rectangle. The Rectangle structure has methods to test its relationship to other Rectangles.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member
    Join Date
    Apr 2009
    Posts
    358

    Re: Checking ".Left" of whole object [Space Invaders]

    Quote Originally Posted by jmcilhinney View Post
    Each control has a Bounds property that is type Rectangle. The Rectangle structure has methods to test its relationship to other Rectangles.
    Could you explain the bounds property a little bit? Like explain how you could ensure that one object could not overlap another object.

  4. #4
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: Checking ".Left" of whole object [Space Invaders]

    Test it out for yourself. I was in the middle of a mario game and I was using a loop to check every control on the form for whether its x and y locations and height and width would cause it to hit the character. It took me weeks to get it all worked out and then in 5 minutes of reading jmcs post I have re-written 60 lines of code in less than 10.

    I didn't do any research or questioning on it either. A hint: you still need to loop through all the controls to check if any of them hit your character, but otherwise its one line of code to make that check for each control, just type in your character control and press period, look for the keywords bounds and intersectswith in jmcs post. Really, try to figure some things out on your own.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  5. #5
    Hyperactive Member
    Join Date
    Apr 2009
    Posts
    358

    Re: Checking ".Left" of whole object [Space Invaders]

    Quote Originally Posted by Vectris View Post
    Test it out for yourself. I was in the middle of a mario game and I was using a loop to check every control on the form for whether its x and y locations and height and width would cause it to hit the character. It took me weeks to get it all worked out and then in 5 minutes of reading jmcs post I have re-written 60 lines of code in less than 10.

    I didn't do any research or questioning on it either. A hint: you still need to loop through all the controls to check if any of them hit your character, but otherwise its one line of code to make that check for each control, just type in your character control and press period, look for the keywords bounds and intersectswith in jmcs post. Really, try to figure some things out on your own.
    Alright, I'll try. Basically I could figure out that type of thing, what I can't figure out is the collision.

  6. #6
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: Checking ".Left" of whole object [Space Invaders]

    intersectswith is the collision. Once you figure out its syntax you just put it in an IF statement and if the statement is true then there was an intersection or collision.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width