Results 1 to 14 of 14

Thread: [RESOLVED] a need a few pointers

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2009
    Posts
    573

    Resolved [RESOLVED] a need a few pointers

    hi i attached a file should run for u,there is no exe in there.


    my problem is this program will animate 3 ants no problem.
    add another and it stops animating .

    if i should throw this away and start over doing it another way please
    tell me.
    thanks
    Attached Files Attached Files

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: a need a few pointers

    The code you have got seems to be quite good, but it is hard to be sure (or find the problem) because you have made it very hard to read.

    One reason for that is lots of blank lines - you should try to keep it down to just 1 or 2 in a row at most (and usually none).

    Another reason is that your indenting is extremely random. For an explanation/example of how it should be, see the article What is indenting, and why should I do it? from our Classic VB FAQs (in the FAQ forum)

    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).



    You should also take a look at the article Why is using the 'End' statement (or VB's "stop" button) a bad idea?

    In this case you should replace End with this:
    Code:
      antanimation.Enabled = False
      Unload Me
      Exit Sub

  3. #3
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: a need a few pointers

    fly,

    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.

    Bryce

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2009
    Posts
    573

    Re: a need a few pointers

    thanks si i usually clean up last take out lines and such .
    ive been trying to find a way to do that Animateant1 and 2 into one.


    @Bryce im still trynig to learn arrarys and such
    thanks all

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: a need a few pointers

    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.

  6. #6
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: a need a few pointers

    fly,

    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.

    Bryce

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2009
    Posts
    573

    Re: a need a few pointers

    thanks that seams to be easy will probly be faster

  8. #8
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: a need a few pointers

    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
     ...

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2009
    Posts
    573

    Re: a need a few pointers

    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

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: a need a few pointers

    Quote Originally Posted by flyhigh View Post
    i think im going to have to learn bltbit .
    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.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2009
    Posts
    573

    Re: a need a few pointers

    image controls is that in propertys?

  12. #12
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: a need a few pointers

    Quote Originally Posted by flyhigh View Post
    image controls is that in propertys?
    I think this is what si_the_geek is telling you:
    Code:
    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


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: a need a few pointers

    That is exactly what I meant
    (while I said Image controls, PictureBox controls are perfectly valid too)

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2009
    Posts
    573

    Re: a need a few pointers

    hey i got 11 out of 12 to work

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