: [Start code]
: For Apos = 0 To 47
: ' If we're touching a Star
: If (e.X > (StarX(Apos) - 10) And e.X < (StarX(Apos) + 10)) And
: (e.Y > (StarY(Apos) - 10) And e.Y < (StarY(Apos) + 10)) Then
: If StHi Then
: Apos = 47
: starClicked = Apos
: HighlightStar(tmpStr1, StarX(Apos), StarY(Apos))
: saveStX = StarX(Apos)
: saveStY = StarY(Apos)
: StHi = True
: Apos = 47
: End If
: ' We;re not touching a Star
: ' Erase Highlight
: If StHi = True Then
: HighlightStar("", saveStX, saveStY)
: TextBox1.Text = GameMessage
: StHi = False
: End If
: End If
: Next Apos
: [End code]
Well, I would love to help you optimize this program to run faster, but I would have to see more code than just this.
I don't know what routine you have this code under, but I get the feeling like you have it under "PictureBox1.MouseMove" or something like that. Basically something you want to keep in mind when you have a loop with If statements in it, it is going to start to slow down, especially since your if statements have several cases.
So there are a few things you could try...
: I have no awesome way of explaining this... but basically you use AndAlso and OrElse instead of And and Or parts of your if statement. So your top if statement would look like this...
If (e.X > (StarX(Apos) - 10) AndAlso e.X < (StarX(Apos) + 10)) AndAlso (e.Y > (StarY(Apos) - 10) AndAlso e.Y < (StarY(Apos) + 10)) Then
What this does is checks each case individually and as soon as one case is fasle it immediately goes to the ElseIf's or Else... Normally, it does all 4 passes on this if statement at once (which is very very slightly faster then the circut logic) however, overall you will save time by using Circut logic on a If statement inbedded in a Loop or a function that gets called alot.
I noticed that you are setting Apos = 47 (the number that ends the for loop) Instead of that, try using "Exit Sub" or "Exit For" depending on what you want to do. If you are finished at that point with all the work that the function is going to do, do an Exit Sub, if you just want to drop out of the For Loop, do an Exit For.
If you want something to run fast, basically try to stay away from If Statements (which is hard in some cases, I wrote code for a panel that I can resize / move with my mouse and the cursor changes in the proper areas and you can resize from corners or sides like a regular windows form works, this panel also 'snaps' or 'docks' into the side of a form if you drag it off... anyways, several revisions later I learned that Select cases are faster than If statements and Circut logic becomes your freind when you have code on the MouseMove function...)
Hope this helps! Feel free to reply to this post with as many questions as you need to ask, I will answer them whenever I see them.
Sean Campbell - Firesickle.com