Message Box Appearing too early
Message Box Appearing too early
Message Box Appearing too early
Message Box Appearing too early
Message Box Appearing too early
Message Box Appearing too early Message Box Appearing too early Message Box Appearing too early Message Box Appearing too early Message Box Appearing too early Message Box Appearing too early Message Box Appearing too early Message Box Appearing too early
Message Box Appearing too early Message Box Appearing too early
Message Box Appearing too early
Go Back  Xtreme Visual Basic Talk > > > Message Box Appearing too early


Reply
 
Thread Tools Display Modes
  #1  
Old 06-21-2011, 02:41 PM
AbarthGT AbarthGT is offline
Newcomer
 
Join Date: Jun 2011
Posts: 8
Default Message Box Appearing too early


Hi Everyone, I am new to this world and need your help. I want 2 images that when they hit each other a message box comes up. I made a bee and a flower.

I want to bee to touch the flower and message box say "You win" But the problem is that sometimes it appears too soon without even touch the flower.

Could anybody try help me. Thanks

Its always at the same place.

2.jpg

1.jpg

Quote:
Private Sub form_keydown(keycode As Integer, shift As Integer)
If (imgbee.Top + imgbeheight > imgflower.Top) And (imgbee.Left < imgflower.Left + imgflower.Width - 1400) Then
MsgBox "You Win"
Else
Select Case keycode
Case vbKeyUp
imgbee.Top = imgbee.Top - 100
Case vbKeyDown
imgbee.Top = imgbee.Top + 100
Case vbKeyLeft
imgbee.Left = imgbee.Left - 100
Case vbKeyRight
imgbee.Left = imgbee.Left + 100
End Select
End If
End Sub


Private Sub imgbee_Click()
MsgBox "Hi"
End Sub

Private Sub imgflower_Click()
MsgBox "Touch Me"
End Sub

Reply With Quote
  #2  
Old 06-21-2011, 03:01 PM
Cerian Knight's Avatar
Cerian KnightMessage Box Appearing too early Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,199
Default

As a starting point for discussion, if you were to treat each object as a sphere and knew its center and radius, you could simply check the distance between the two objects and see if it is <= the sum of the two radii.

Again, for simplicity we would then take 1/2 the height of each object (1 and 2) to be the radius (R) and add that value to the top and left to get the center point (X,Y).

This gives us: Sqr((X1-X2)^2 + (Y1-Y2)^2) <= (R1 + R2) when the spheres are in a collision state.
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').
Reply With Quote
  #3  
Old 06-22-2011, 09:20 AM
AbarthGT AbarthGT is offline
Newcomer
 
Join Date: Jun 2011
Posts: 8
Default

Sorry I am a complete n00b ! So what Do I do with this? I know what is means but not too sure where to put it or If i need to make any modification.

Thanks
Reply With Quote
  #4  
Old 06-22-2011, 11:26 AM
passel's Avatar
passelMessage Box Appearing too early passel is offline
Sinecure Expert

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

Your current code is only checking two conditions.

If (imgbee.Top + imgbeheight > imgflower.Top) And (imgbee.Left < imgflower.Left + imgflower.Width - 1400) Then
i.e. If (Bee's Bottom is greater than the flower Top) And Bee's left side is less than flower's right side - 1400) Then "You win"
I assume the -1400 was an attempt at a fix, or is adjusting for blank space around the flower image?

Normally for an rectangular intersection you need to test four conditions.
If Bee's Bottom is greater than flower Top And
...Bee's Top is greater than flower Bottom And 'You have a vertical overlap of the two rects
...Bee's Right is greater than the flower's Left And
...Bee's Left is less than the flower's Right Then "You win"

Since you only compare the two, if the bee is anywhere below the top of the flower and to the left of the right of the flower (the lower left quandrant), you Win.

Try adding the other two cases to your existing code.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #5  
Old 06-22-2011, 11:34 AM
AbarthGT AbarthGT is offline
Newcomer
 
Join Date: Jun 2011
Posts: 8
Default

Thanks I will give it a try and reply back. Thanks
Reply With Quote
  #6  
Old 06-22-2011, 12:26 PM
AbarthGT AbarthGT is offline
Newcomer
 
Join Date: Jun 2011
Posts: 8
Default

I did What you said about what you need to find out. I changed the coding to the following but now It wins anywhere on the right , left, and bottom , Just the top is fine.




PHP Code:
Private Sub Form_KeyDown(KeyCode As IntegerShift As Integer)
If (
imgbee.Top imgbeeheight) = imgflower.Top And imgbee.Top > (imgflower.Top imgflowerheightThen
 MsgBox 
"Win 1"
 
ElseIf (imgbee.Left imgbeewidth) > imgflower.Left And imgbee.Left < (imgflower.Left imgflowerwidthThen
  MsgBox 
"Win 2"
 
End
 
Else
    
Select Case KeyCode
    
Case vbKeyUp
      imgbee
.Top imgbee.Top 100 'This move the bee up by using the arrow keys'
    
Case vbKeyDown
      imgbee
.Top imgbee.Top 100 'This move the bee down by using the arrow keys'
    
Case vbKeyLeft
      imgbee
.Left imgbee.Left 100 'This moves the bee to the left by using the arrow keys'
    
Case vbKeyRight
      imgbee
.Left imgbee.Left 100 'This moves the bee to the right by using the arrow keys'
  
  
End Select
 End 
If
End Sub

Private Sub Timer1_Timer()
 
'The time is limited and the game will be over after a period'
  
MsgBox "Time UP! Try Again!"
End Sub


Private Sub imgbee_Click()
MsgBox "Waazeeeeeee Blud!"
End Sub

Private Sub imgfire_Click()
  
MsgBox "You Can't touch me HAHA (EVIL LAUGH)"
End Sub

Private Sub imgflower_Click()
  
MsgBox "Let's Polenate! Beee HELP!"
End Sub 
Attached Files
File Type: zip Bee Test.zip (113.1 KB, 7 views)
Reply With Quote
  #7  
Old 06-22-2011, 03:15 PM
Cerian Knight's Avatar
Cerian KnightMessage Box Appearing too early Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,199
Default

I believe the other two checks need to be part of (ANDed into) the first test, not added as an 'ElseIf'.
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').
Reply With Quote
  #8  
Old 06-22-2011, 03:54 PM
AbarthGT AbarthGT is offline
Newcomer
 
Join Date: Jun 2011
Posts: 8
Default

I tried do it as part of "and" but won't let me have 2 "and" so I don't know how to put the 2 together.
Reply With Quote
  #9  
Old 06-22-2011, 05:16 PM
Cerian Knight's Avatar
Cerian KnightMessage Box Appearing too early Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,199
Default

Something like (untested):
Code:
If (imgbee.Top + imgbeeheight) = imgflower.Top And imgbee.Top > _
 (imgflower.Top + imgflowerheight) And (imgbee.Left + imgbeewidth) > _ 
imgflower.Left And imgbee.Left < (imgflower.Left + imgflowerwidth) Then
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').

Last edited by Cerian Knight; 06-23-2011 at 11:03 AM. Reason: Wrapped my long code line
Reply With Quote
  #10  
Old 06-22-2011, 05:38 PM
AbarthGT AbarthGT is offline
Newcomer
 
Join Date: Jun 2011
Posts: 8
Default

Just Tried it now nothing happens at all! Im loosing hope... This is part of my assignment that needs to be handed in on Friday but can't manage to do this!
This the code I just tried

Quote:
Rem Author: Navisen Naigum
Rem Date: 07/02/11
Rem program description:the purpose of the game is to move the arrows as quick as possible to make the bee reache the flower as quick as possible before the timer runs out
Rem program name Visual Basic (VB)


Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If (imgbee.Top + imgbeeheight) = imgflower.Top And imgbee.Top > (imgflower.Top + imgflowerheight) And (imgbee.Left + imgbeewidth) > imgflower.Left And imgbee.Left < (imgflower.Left + imgflowerwidth) Then
MsgBox "Yh"
End
Else
Select Case KeyCode
Case vbKeyUp
imgbee.Top = imgbee.Top - 100 'This move the bee up by using the arrow keys'
Case vbKeyDown
imgbee.Top = imgbee.Top + 100 'This move the bee down by using the arrow keys'
Case vbKeyLeft
imgbee.Left = imgbee.Left - 100 'This moves the bee to the left by using the arrow keys'
Case vbKeyRight
imgbee.Left = imgbee.Left + 100 'This moves the bee to the right by using the arrow keys'

End Select
End If
End Sub

Private Sub Timer1_Timer()
'The time is limited and the game will be over after a period'
MsgBox "Time UP! Try Again!"
End Sub


Private Sub imgbee_Click()
MsgBox "Waazeeeeeee Blud!"
End Sub

Private Sub imgfire_Click()
MsgBox "You Can't touch me HAHA (EVIL LAUGH)"
End Sub

Private Sub imgflower_Click()
MsgBox "Let's Polenate! Beee HELP!"
End Sub
Reply With Quote
  #11  
Old 06-23-2011, 04:11 AM
DrPunk's Avatar
DrPunkMessage Box Appearing too early DrPunk is offline
Senior Contributor

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

I believe a couple of your comparisons are wrong (I've changed the ones in red that should now be right)...
Code:
If (imgbee.Top + imgbeeheight) > imgflower.Top And _
    imgbee.Top < (imgflower.Top + imgflowerheight) And _
    (imgbee.Left + imgbeewidth) > imgflower.Left And _
    imgbee.Left < (imgflower.Left + imgflowerwidth) Then
Hopefully.
__________________
There are no computers in heaven!
Reply With Quote
  #12  
Old 06-23-2011, 04:47 AM
AbarthGT AbarthGT is offline
Newcomer
 
Join Date: Jun 2011
Posts: 8
Default

Now Nothing Happens!
Reply With Quote
  #13  
Old 06-23-2011, 04:55 AM
DrPunk's Avatar
DrPunkMessage Box Appearing too early DrPunk is offline
Senior Contributor

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

Well I believe the collision logic that I posted above to be correct.

So it could be that you've not set imgbeeheight, imgbeewidth, imgflowerwidth or imgflowerheight. Although I'm not sure why you aren't using imgBee.Height, imgBee.Width, imgFlower.Width and imgFlower.Height.
__________________
There are no computers in heaven!
Reply With Quote
  #14  
Old 06-23-2011, 05:30 AM
DrPunk's Avatar
DrPunkMessage Box Appearing too early DrPunk is offline
Senior Contributor

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

I've got a funny feeling that you've not got variable declaration required set in the options.

That's really bad practice and it's stupid that the environment defaults to off for that setting.

If you haven't already then go to the Options and on the Editor page set "Require Variable Declaration". You'll run into far less problems if you have that set.

Personally, I'd also add a boundary check to the image moving as well to stop the image from leaving the form. So, for example, moving left you'd check to see if imgBee.Left - 100 is greater than 0 and if it is then you let it move left. Moving right you'd check to see if imgBee.Left + imgBee.Width + 100 is less than the width of the form and if it is then you let it move right.
__________________
There are no computers in heaven!
Reply With Quote
  #15  
Old 06-23-2011, 06:11 AM
DrPunk's Avatar
DrPunkMessage Box Appearing too early DrPunk is offline
Senior Contributor

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

The other thing that's bugging me is when you are checking for the collision.

You should really do the move and THEN check for the collision. What you'll find with what you've got is that the images will collide but it's not until you press a button again that it tells you they have collided.
__________________
There are no computers in heaven!
Reply With Quote
  #16  
Old 06-23-2011, 06:34 AM
AbarthGT AbarthGT is offline
Newcomer
 
Join Date: Jun 2011
Posts: 8
Default

Thanks a lot for your help! Ill try from what you just explained.
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
Message Box Appearing too early
Message Box Appearing too early
Message Box Appearing too early Message Box Appearing too early
Message Box Appearing too early
Message Box Appearing too early
Message Box Appearing too early Message Box Appearing too early Message Box Appearing too early Message Box Appearing too early Message Box Appearing too early Message Box Appearing too early Message Box Appearing too early
Message Box Appearing too early
Message Box Appearing too early
 
Message Box Appearing too early
Message Box Appearing too early
 
-->