Thread: Moving a picture

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

    Moving a picture

    I'm trying to get this picture to move accross the screen. It keeps raising class exceptions and I don't know why. It only happens when i click the button2. in button1 it works. Can anyone help me please?

    Code:
    void move_ship(Ship_1 *User_Ship, TImage *Ship_sprite)
    {
    int x1;
    int y1;
    
    User_Ship->x1 = User_Ship->x1 + 5;
    User_Ship->y1 = User_Ship->y1+ 5;
    
    x1 = (User_Ship->x1) + 5;
    y1 = (User_Ship->y1) + 5;
    
    Form1->Canvas->Draw(x1,y1,Ship_sprite->Picture->Graphic ) ;
    }
    
    
    
    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    int x;
    int y;
    
    //Creats new ship
    Ship_1 *User_Ship = new Ship_1;
    TImage *Ship_sprite = new TImage(Owner);
    
    //loads pic
    Ship_sprite->Picture->LoadFromFile("ship-1.ico") ;
    
    //sets ship coords
    User_Ship->x1 = 100;
    User_Ship->y1 = 200;
    
    /*x = User_Ship->x1;
    y = User_Ship->y1;  */
    
    move_ship(User_Ship, Ship_sprite);
    }
    //---------------------------------------------------------------------------
    
    void __fastcall TForm1::Button2Click(TObject *Sender)
    {
    int k;
    int l;
    
    k=User_Ship->x1;
    l= User_Ship->y1;
    
    k=k+5;
    l=l+5;
    move_ship(User_Ship, Ship_sprite);
    
    }
    //---------------------------------------------------------------------------
    here is the Ship class if it helps...

    Code:
    class Ship_1
    {
    public:
            TImage *Ship_sprite(Owner);
            TRect *recSprite;
            int *health;
            int x1;
            int y1;
    
    };

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    //Creats new ship
    Ship_1 *User_Ship = new Ship_1;
    TImage *Ship_sprite = new TImage(Owner);
    
    //loads pic
    Ship_sprite->Picture->LoadFromFile("ship-1.ico") ;
    Either
    1. Put this code in button2 as well, so it has an initialised ship.

    2. Put this code somewhere else, so its only done once, not every time the ship moves. LoadFromFile() seems horribly expensive to be doing each time the image moves.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    180
    cheers, that works fine

    thx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C] change picture width and height in Static control (no MFC)
    By pc2-brazil in forum Windows Programming
    Replies: 1
    Last Post: 05-05-2008, 01:17 AM
  2. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  3. 3D moving
    By bluehead in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2005, 05:46 AM
  4. Rotating a picture
    By Bill in forum C Programming
    Replies: 4
    Last Post: 05-18-2004, 09:26 PM
  5. Moving A Picture..slight prob
    By Xterria in forum Windows Programming
    Replies: 4
    Last Post: 02-27-2002, 09:05 PM