Bueno..
No tengo mucha idea de usar clases y he intentado crear algunas.
El problema es que cuando asigno varias variables de la misma clase si creo y destruyo una de las dos variables me cargo los datos de la otra. No se porque.
Adjunto la unidad que he creado a ver si algun sabio de los de aqui me ayuda a ver que es lo que esta mal. Y como hacerlo mejor.
Un saludo..
Código:
unit pathmovil;
interface
uses Windows,graphics,extctrls,controls,math;
CONST
MAXSEQ=512;
MAXTRAD=2;
MAXREC=50;
PIXELSMETRO=200; // un metro recorrido por el robot equivale en pantalla a 200 pixels
NULLREC=$FFFF;
type
TTipoMov=(mvStop,mvAdelante,mvAtras,mvDerecha,mvIzquierda,mvNoreste,mvsureste,mvsuroeste,mvnoroeste);
TTradMovil=record
Movi: TTipoMov;
TiempoMov : TTime;
end;
Tpathmovil = record
Origenx, Origeny, Destinox, Destinoy : Integer;
Seleccion: integer;
TiempoPath: cardinal;
CteMetro, CteGiro: Cardinal;
Tradmov: Array [1..MAXTRAD] of TTradMovil;
end;
TSecuencia =class
public
Numero : integer;
Nombre :String;
Descripcion: String;
Tiempo: cardinal;
Color: TColor;
Recorr : TpathMovil;
constructor Create;
destructor Destroy;
function Add(var MIrec:Tpathmovil):integer;
function Delete(Indice:integer):integer;
procedure SetOrigen(x,y:integer);
procedure SetDestino(xx,yy:integer);
procedure SetOriDest(x,y,xx,yy:integer);
procedure SetCtes(Ct,Cg: cardinal);
function GetDeltaX:integer;
function GetDeltay:integer;
function GetModulo:double;
// function GetAngulo:double;
function GetTimems:double;
function GetLonmetros:double;
procedure SetColor(ClColor:TColor);
procedure DibujarRecorrido(x,y:integer;Canvas:Tcanvas);
function Rotate2D(x,y:integer; alpha:double): TPoint;
private
Count:integer;
end;
implementation
constructor Tsecuencia.Create;
begin
inherited;
//Count:=0;
Numero:=0;
Nombre:='';
Descripcion:='';
Tiempo:=0;
SetOriDest(NULLREC,NULLREC,NULLREC,NULLREC);
//Metodo create de recorrido
end;
destructor Tsecuencia.Destroy;
begin
inherited;
end;
procedure TSecuencia.SetColor(ClColor:TColor);
begin
Color:=clColor;
end;
function Tsecuencia.Add(var MIrec:Tpathmovil):integer;
begin
(* inc(count);
if Count<=MAXREC then
begin
// if Recorr[Count]=nil then
// Recorr[Count]:=Tpathmovil.Create;
*)
Recorr:=Mirec;
(* result:=Count;
end
else result:=-1 *)
end;
function Tsecuencia.Delete(Indice:integer):integer;
begin
if (indice>0) or (indice<=Count) then
begin
// hacer rutina de borrado de elemento
end
else result:=-1;
end;
function Tsecuencia.GetModulo:double;
var
dx: Double;
dy: Double;
begin
dx := GetDeltax;
dy := GetDeltay;
Result := Sqrt(dx * dx + dy * dy);
end;
function TSecuencia.GetTimems:double;
begin
result:=(Getmodulo*Recorr.CteMetro)/PIXELSMETRO // cte tiempo para 9 Voltios 1metro lo recorre en 4000 msegundos
end;
function TSecuencia.GetLonmetros:double;
begin
result:=(Getmodulo/PIXELSMETRO) // 1 metro 200 pixels
end;
procedure Tsecuencia.SetOrigen(x,y:integer);
begin
Recorr.Origenx:=x;
Recorr.Origeny:=y;
end;
procedure Tsecuencia.SetDestino(xx,yy:integer);
begin
Recorr.Destinox:=xx;
Recorr.Destinoy:=yy;
end;
procedure Tsecuencia.SetOriDest(x,y,xx,yy:integer);
begin
Recorr.Origenx:=x;
Recorr.Origeny:=y;
Recorr.Destinox:=xx;
Recorr.Destinoy:=yy;
end;
procedure TSecuencia.SetCtes(Ct,Cg: cardinal);
begin
Recorr.CteMetro:=Ct;
Recorr.CteGiro:=Cg;
end;
function TSecuencia.GetDeltaX:integer;
begin
result:=(Recorr.Destinox-Recorr.Origenx);
end;
function TSecuencia.GetDeltay:integer;
begin
result:=(Recorr.Destinoy-Recorr.Origeny);
end;
procedure TSecuencia.DibujarRecorrido(x,y:integer; Canvas:Tcanvas);
begin
with Canvas do
begin
Pen.Style :=psdot;
Pen.color:=Color;
with Recorr do
begin
if Origenx<>NULLREC then
begin
MoveTo(Origenx+x,Origeny+y);
LineTo(x+Destinox,y+Destinoy);
end;
end; //with ..
end;
end;
//valores 0 a 360 antihorario o 0 180 0 -180
function Tsecuencia.Rotate2D(x,y:integer; alpha:double): TPoint;
var
sinus, cosinus : Extended;
begin
alpha:=DegToRad(alpha);
SinCos(alpha, sinus, cosinus);
result.x := Round(x*cosinus + y*sinus);
result.y := Round(-x*sinus + y*cosinus);
end;
end.
Unidad USprites
Código:
unit sprites;
interface
uses Windows,SysUtils,graphics,extctrls,pathmovil,classes;
type
TOrientacion=(orNorte,orNorEste,orEste,orSureste,orSur,orSurOeste,orOeste,OrNoroeste);
type
TSprite = class
public
x, y, xAnterior, yAnterior: Integer;
ColorTransparente: TColor;
Imagen, Mascara,buffer: TImage;
Seleccion: integer;
deltx,delty:integer;
Orientacion:TOrientacion;
Secuencia: Array [1..MAXSEQ] of TSecuencia;
constructor Create;
destructor Destroy; override;
procedure Cargar( sImagen: string );
procedure Dibujar( x,y:integer; Canvas: TCanvas );
function AddSec(MIsec:TSecuencia):integer;
function Delete(Indice:integer):integer;
private
Count: integer;
end;
implementation
constructor TSprite.Create;
begin
inherited;
Imagen := TImage.Create( nil );
Imagen.AutoSize := True;
Mascara := TImage.Create( nil );
ColorTransparente := RGB( 255, 0, 255 );
Buffer := TImage.Create( nil );
Count:=0;
end;
destructor TSprite.Destroy;
begin
Buffer.Free;
Mascara.Free;
Imagen.Free;
inherited;
end;
procedure TSprite.Cargar( sImagen: string );
var
i, j: Integer;
begin
Imagen.Picture.LoadFromFile( sImagen );
Mascara.Width := Imagen.Width;
Mascara.Height := Imagen.Height;
/// jcsoft
Buffer.Width:= Imagen.Width;
Buffer.Height := Imagen.Height;
/// jcsoft
for j := 0 to Imagen.Height - 1 do
for i := 0 to Imagen.Width - 1 do
if Imagen.Canvas.Pixels[i,j] = ColorTransparente then
begin
Imagen.Canvas.Pixels[i,j] := 0;
Mascara.Canvas.Pixels[i,j] := RGB( 255, 255, 255 );
end
else
Mascara.Canvas.Pixels[i,j] := RGB( 0, 0, 0 );
end;
procedure TSprite.Dibujar( x, y: Integer; Canvas: TCanvas );
begin
Canvas.CopyMode := cmSrcAnd;
Canvas.Draw( x, y, Mascara.Picture.Graphic );
Canvas.CopyMode := cmSrcPaint;
Canvas.Draw( x, y, Imagen.Picture.Graphic );
end;
function TSprite.AddSec(MIsec:TSecuencia):integer;
begin
if Count<=MAXSEQ then
begin
inc(count);
if Secuencia[Count]=nil then
Secuencia[Count]:=TSecuencia.Create;
miSEC.numero:=Count;
if Misec.NOmbre='' then
MIsec.Nombre:='SEC'+IntToStr(Count);
Secuencia[Count]:=Misec;
result:=Count;
end
else result:=-1
end;
function TSprite.Delete(Indice:integer):integer;
begin
if (indice>0) or (indice<=Count) then
begin
// hacer rutina de borrado de elemento
end
else result:=-1;
end;
end.
¿Podria ser porque en el programa principal declaro como privada la estructura ListaSprites?
Código:
type
TForm1 = class(TForm)
........
private
ListaSprites: array [1..MAXBOTS] of TSprite;
...
Si necesitais mas datos no hay problema para darlos..