-
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;
};
-
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.
-
cheers, that works fine :D
thx