Mouvment problem

This is a discussion on Mouvment problem within the C++ Programming forums, part of the General Programming Boards category; Alright, I'm using Borland C++ Builder, and mostly its working aside from this small problem... I have a Gameloop, where ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    180

    Mouvment problem

    Alright, I'm using Borland C++ Builder, and mostly its working aside from this small problem...

    I have a Gameloop, where everything is handled(drawing, mouvment, etc) and FormKeyUp/Down functions to grab the key presses.

    Whenever I move, it raised exceptions. This coded worked fine before I added the mouvment. To be able to move right or left, i need to wait until there has been a collision between a bullet and an enemy.(at the moment it just auto-shoots...) And mouvming up or down just doesn't work unless I hold the key down, and as soon as I let go, it raises the exception.

    Here is the game loop:

    Code:
    void __fastcall TForm1::GameLoop(TObject*, bool &done)
    {
    // fires a regular shot
    if(laser->y1 <= 0 || laser->Obsolete == true)
    {
            laser->y1=User->y1;
            laser->Obsolete = false;
            laser->x1 =  User->x1 + User->Ship_Sprite->Width / 2;
    
           // PlaySound(MAKEINTRESOURCE(ID_LASER), HInstance, SND_RESOURCE | SND_ASYNC );
    }
    
    laser->regShot();
    
    //check for collision
    laser->Collision(Baddy,laser);
    
    // fires a regular missile
    if(missile->y1 <= 0 || missile->Obsolete == true)
    {
            missile->y1=User->y1;
            missile->Obsolete = false;
    
            missile->x1 = User->x1 + User->Ship_Sprite->Width / 2;
    
    }
    missile->regShot();
    missile->Collision(Baddy, missile);
    
      //move baddies
    for(int k=1;k<5;k++)
    {
            for(int z = 1;z<9;z++)
            {
                    if(Baddy[k][z]->Dead == false)
                    {
                            Baddy[k][z]->MoveLeft();
                            Baddy[k][z]->Baddy_Collision.top = Baddy[k][z]->x1;
                            Baddy[k][z]->Baddy_Collision.left = Baddy[k][z]->y1 + Baddy[k][z]->Enemy_Sprite->Height;
                            Baddy[k][z]->Baddy_Collision.Bottom = Baddy[k][z]->Baddy_Collision.left + Baddy[k][z]->Enemy_Sprite->Width;
                            Baddy[k][z]->Baddy_Collision.right = Baddy[k][z]->x1 + Baddy[k][z]->Enemy_Sprite->Width ;
                   }
            }
    }
    
    
    /*for(int i=1;i<5;i++)
    {
            for(int k=1;k<9;k++)
            {
              // BLaser->
            }
    }   */
    
       //Key presses are acted upon here
      if (keys.right == true)
        {
           User->MoveRight();
        }
    
      if (keys.left == true)
        {
           User->MoveLeft();
        }
    
      if (keys.up == true)
      {
            User->MoveUp();
      }
    
      if (keys.down == true)
      {
       User->MoveDown();
      }
    
    
    
    Draw(BackBuffer, User, Baddy, ClientRect, BackGround, laser, missile, Health);
    
    done = false;
    
    }
    and the key up and down

    Code:
    oid __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key, TShiftState Shift)
    {
     if (Key == VK_RIGHT)  //move piece right
        {
            keys.right = false;
        }
    
      if (Key == VK_LEFT)
        {
         keys.left = false;
        }
    
      if (Key==VK_UP)
      {
         keys.up = false;
      }
    
      if (Key == VK_DOWN)
      {
        keys.down = false;
      }
    
    }
    //---------------------------------------------------------------------------
    void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
    {
     if (Key == VK_ESCAPE) Application->Terminate();
    
     if (Key == VK_RIGHT)  //move piece right
        {
            keys.right = true;
                }
    
      if (Key == VK_LEFT)
        {
         keys.left = true;
        }
    
      if (Key==VK_UP)
      {
         keys.up = true;
      }
    
      if (Key == VK_DOWN)
      {
        keys.down = true;
      }
    }
    Any help would be great!

    Cheers,

    DW

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Which function raises the exception? Your debugger should tell you.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    Here
    Code:
     Baddy[k][z]->Baddy_Collision.top = Baddy[k][z]->x1;
    . This worked fine before I added the mouvment....

    Cheers

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    It's a lot of work for someone else to step through your code for you. If I get time I will do it, but until then, I recommend you attempt it yourself. Set a breakpoint somewhere good and step through each line until you find a logic flaw or something like that. In this case 'k' and 'z' are obviously being set to values that Baddy[][] does not have memory allocated for, thus that line is attempting to access memory it doesn't own. Good luck.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    I've been through it, um....lets see.....100 times now, spent 2 hrs on it. Like I said, the code *did* work, and now it doesn't, so I imagine the memeroy is still allocated properly. If you do have a chance, that would be amazing!

    Cheers

    DW

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    Alright, narrowed down the problem. its in how I created my stuct

    Code:
    struct key{bool left;
               bool right;
               bool up;
               bool down;};
    when the last member (down) is acsessed, it crashes.

    Is there somthing wrong with how i declared this?

    DW

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    so I imagine the memeroy is still allocated properly.
    Exceptions (in this case an access violation) occur when memory isn't allocated properly, or when you specify an index for an array that points to memory you haven't allocated. I am very sure it is memory related here.

    If you do have a chance, that would be amazing!
    I might do it within the next few hours.

    Is there somthing wrong with how i declared this?
    Looks fine to me. However, if it crashes when you access the last data member, we could infer that the bounds of your array are off by a tiny amount, a common problem that occurs using lots of loops and arrays. You probably should have allocated 'something+1' bytes of memory or something like that somewhere.

    Anyway, I'll step through the code and see what I come up with.

    EDIT: Actually, I'm going to need all of your code if I'm going to debug it. I imagine it's quite big so just zip the entire project folder and email it to me: benforbes@optusnet.com.au

    EDIT: Can you also show me exactly which lines cause your exception? Is it every time you access Key::down?
    Last edited by bennyandthejets; 06-01-2004 at 07:13 PM.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    Sure I'll send it. Its made with Borland C++ Builder. Do you have this?


    thx again
    DW

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Hasn't your problem just been solved in the other thread?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    ah.....no not really....kinda.....i'll post there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21