Problem: das einbinden einer Klasse

xyzPainter

Grünschnabel
Hallo ihr CProfis.
Das Thema Klassen ist noch sehr neu für mich und komme deshalb nicht weiter.
Ein Image soll von den Wänden abprallen wenn es auf eine trifft (quasi Pong).
Ich vermute der Fehler liegt beim Timer, in der die Klasse implementiert ist.
Wie kann man den Fehler beheben.
Und ließe sich das ganze noch besser strukturieren?

Ich danke im vorraus für jede konstruktive Hilfe *danke*


//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "frmpong.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

typedef int T;

class Image
{
private:
T Hoehe,Breite,
movx,
movy,
speed,
signx,
signy,
posx,
posy;
public:
Image (int nHoehe, int nBreite);
public:
void Pos (int x, int y);
void Speed (int nspeed);
void Speed (int nmovx, int nmovy);
void Col (int nhoehe, int nbreite);
void Sign ();
void Mov ();
void Draw ();
};

Image::Image (int nHoehe, int nBreite)
{
Hoehe = nHoehe;
Breite = nBreite;
posx = 0;
posy = 0;
speed = 4;
movx = random(speed);
movy = random(speed);
}

void Image::pos (int x, int y)
{
posx = x;
posy = y;
}

void Image::Speed (int nspeed)
{
speed = nspeed;
movx = nspeed;
movy = nspeed;
}

void Image::Speed (int nmovx, int nmovy)
{
Sign();
movx = nmovx * signx;
movy = nmovy * signy;
}

void Image::Sign ()
{
signx = random(2)-1;
if (signx == 0) signx = 1;

signy = random(2)-1;
if (signy == 0) signy = 1;

movx = movx * signx;
movy = movy * signy;
}

void Image::Mov()
{
posx = posx + movx;
posy = posy + movy;
}

void Image::Col (int nhoehe, int nbreite)
{
if ((Breite+speed)>=posx) {
movx = movx*(-1);
posx = Breite+speed+1;
}
if ((Hoehe+speed)>=posy) {
movy = movy*(-1);
posy = Hoehe+speed+1;
}
if (posx>=((nbreite-Breite)-speed)) {
movx = movx*(-1);
posx = (nbreite-Breite)-speed;
}
if (posy>=((nhoehe-Hoehe)-speed)) {
movy = movy*(-1);
posy = (nhoehe-Hoehe)-speed;
}
}

void Image::Draw ()
{
Form1->Image1->Left = posx;
Form1->Image1->Top = posy;
}

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Image s(Width, Height);
s.Pos(Image1->Left, Image1->Top);
s.Sign();
s.Col(Form1->Width, Form1->Height);
s.Mov();
s.Draw();
s.Speed(3);
}
//---------------------------------------------------------------------------
 
Der Timer ist da wirklich Dein Verhängnis!

Du initialisierst andauernd deine Klasse mit "Image s(Width,Height);" neu, dadurch erstellst Du immer wieder neue Objekte, anstatt dass Du das gleiche benutzt.

"Image s(Width,Height);" muss nach dem schließen deiner klasse erfolgen!

Code:
 //---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
   Image s(Width, Height);
   s.Pos(Image1->Left, Image1->Top);
   s.Sign();
   s.Col(Form1->Width, Form1->Height);
   s.Mov();
   s.Draw();
   s.Speed(3);
}
//---------------------------------------------------------------------------

gruß gerd
 
Zurück