|
-
May 20th, 2009, 04:02 PM
#1
Thread Starter
Member
[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.
-
May 20th, 2009, 09:33 PM
#2
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.
-
May 21st, 2009, 12:32 AM
#3
Hyperactive Member
Re: Checking ".Left" of whole object [Space Invaders]
 Originally Posted by jmcilhinney
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.
-
May 21st, 2009, 04:11 PM
#4
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.
-
May 21st, 2009, 05:55 PM
#5
Hyperactive Member
Re: Checking ".Left" of whole object [Space Invaders]
 Originally Posted by Vectris
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.
-
May 21st, 2009, 06:56 PM
#6
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|