Something else that makes it harder to read (and maintain) is the duplication that you have got - Animateant1 and Animateant2 etc have almost identical code, so you should really only have one of them, and pass parameters as apt. For info and examples, see the FAQ article How can I pass a control (textbox/listbox/..) to a sub or function?
If you make those changes the code will be much shorter and easier to read, which will make it much simpler for us to find the problem(s).
First of all, as far as your question about tossing it all and starting over...
Not necessarily, but I would suggest using arrarys instead of having each ant be an "individual."
When I uncommented "ant4," it worked ok for me. What problem are you having? The only problem I saw was with your "bounce" logic. For example, if an ant was traveling downward and to the left, and went off the bottom of the screen, you have "left" as a valid new direction for it. In other words, it won't come back on the screen at that point. I assume it has to travel left until it goes past the x=0 part of the screen, and maybe then it shows up again? Not sure that would even happen all the time, since I'm guessing "down" would be valid to switch to at that point.
Give me some more details about the issues your having and we'll go from there.
It's much better to clean up as you go along, because it reduces the chances of several kinds of mistakes while you are writing the code (because they become much more obvious), as well as making situations like this far easier too.
ive been trying to find a way to do that Animateant1 and 2 into one.
Give it a go based on the article, and if you get stuck let us know what the issue is, and what code you've come up with.
Using Control Arrays as vbfbryce suggested is a good alternative, and could well be better in your situation. If you want to give that a go, there is an article in the "Controls" section of the FAQs which explains them.
some code to get you started with arrays, if that's the way you want to go:
Code:
Option Explicit
Private Directions(1 To 4) As Integer 'need one element for each ant
Private LowerBound As Integer 'will be 1
Private UpperBound As Integer 'I'm thinking 8? represents number of "directions"
Private Sub Form_Load()
Dim i As Integer
LowerBound = 1
UpperBound = 8
Randomize
'Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
For i = 1 To 4
Directions(i) = Int((UpperBound - LowerBound + 1) * Rnd + LowerBound)
'sets a starting direction for each ant, assuming 4 ants
Next i
End Sub
This code will populate an array with random ("direction") numbers, 1 thru 8.
Let's start with this. Let me know if it makes sense to you, and we can go from there.
Couple more points:
1) Move the Randomize statement to Form_Load. It should only be
called once for any program run.
2) Use the code below to get the program to properly unload:
Code:
If GetAsyncKeyState(vbKeySpace) Then
antAnimation.Enabled = False
Unload Me
Exit Sub
End If
...
thanks to all
i think im going to have to learn bltbit .
another question when using multiple forms at once as the ants does that use more memory,and is there a way to use collision detection for forms?
becuse the animationing works fine that way,werd i think
That wouldn't give a noticeable improvement... but you would get a very significant improvement by simply pre-loading the images (possibly in extra image controls), rather than re-loading them every frame for every ant.
another question when using multiple forms at once as the ants does that use more memory,
Yes, and more CPU time etc.
and is there a way to use collision detection for forms?
Yes, the same as you would for controls... but with a few extra calculations.
I wouldn't recommend using multiple forms, as I can't think of any benefit.
becuse the animationing works fine that way,werd i think
Once you have tidied up the code, it will be much easier to work out what the issues are.
Option Explicit
Private Sub Command1_Click() '~~~ When button is pressed
Picture2.Picture = Picture1.Picture '~~~ Copying the preloaded image to a new picturebox, in which we will display it to the user
End Sub
Private Sub Form_Load()
Picture1.Visible = False '~~~ We are hiding this picturebox from the user.
'~~~ Preloading the image to a Picturebox
Picture1.Picture = LoadPicture("c:\test.jpg")
End Sub
...
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video) My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet Social Group:VBForums - Developers from India