furybowser
furybowser

Reputation: 13

How do I make a TImage move (like a DVD logo)

I am trying to make a TImage move like a DVD logo, but the TImage is not moving.

This is the code I used:

void __fastcall TForm1::DVDLogoTimer(TObject *Sender)
{
    image->Left+=xPos; image->Top+=yPos;

    if (image->Left <= invisibleHelperObject->Left) xPos=-xPos;
    if (image->Top <= invisibleHelperObject->Top) yPos=-yPos;
    if (image->Left+image->Width >= invisibleHelperObject->Width) xPos=-xPos;
    if (image->Top+image->Height >= invisibleHelperObject->Height) yPos=-yPos;

    Label1->Caption = IntToStr(xPos) + " | " + IntToStr(yPos);
}

(X and Y variables are not even changing (stays at 0))

Upvotes: 0

Views: 33

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596206

In C++Builder 6 (and the "classic" Borland compiler in modern versions), you can't use compound operators like += with properties. Doing so will read the property value into a temporary and then modify the temporary, but will not assign the temporary back to the property. Using compound operators on properties requires a modern Clang-based compiler:

Differences Between Clang-enhanced C++ Compilers and Previous Generation C++ Compilers, __property: Compound and Chained Assignment

Clang-enhanced C++ compilers support compound assignment of __property, while BCC32 does not.

The objects of the keyword __property are not like fields or members. They should be used in simple assignments.

Although both BCC32 and the RAD Studio Clang-enhanced C++ compilers allow __property to be used in compound assignments such as:

Form1->Caption += DateToStr(Now());

BCC32 only invokes the getter, not the setter. Therefore we recommend that you avoid such constructs when targeting multiple platforms.

None of these compilers support the usage of __property in chained assignment, as in:

Button2->Caption = Button1->Caption = DateToStr(Now()); // Error

So, in your situation, when you invoke image->Left += xPos; for instance, it acts as-if you had written this instead:

//image->Left += xPos;
int temp = image->Left;
temp += xPos;

So, you need to use the + and = operators separately instead, eg:

void __fastcall TForm1::DVDLogoTimer(TObject *Sender)
{
    image->Left = image->Left + xPos;
    image->Top = image->Top + yPos;
    ...
}

Upvotes: 0

Related Questions