Autor Beitrag
Warheart
Hält's aus hier
Beiträge: 2



BeitragVerfasst: Fr 02.05.08 20:21 
Hallo alle miteinander!

Ich bin noch recht neu mit Delphi und hab folgendes problem:
Ich möchte gerne das Spiel 4 gewinnt programmieren... allerdings bekomme ich immer iwelche access violations, wenn ich auf eins von den shapes klicke...
hier ist, was ich bisher habe:


//nicht erschrecken vor der länge... ;) - der part worum es geht ist eh nur sehr klein^^
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Shape1: TShape;
    Shape2: TShape;
    Shape3: TShape;
    Shape4: TShape;
    Shape5: TShape;
    Shape6: TShape;
    Shape7: TShape;
    Shape8: TShape;
    Shape9: TShape;
    Shape10: TShape;
    Shape11: TShape;
    Shape12: TShape;
    Shape13: TShape;
    Shape14: TShape;
    Shape15: TShape;
    Shape16: TShape;
    Shape17: TShape;
    Shape18: TShape;
    Shape19: TShape;
    Shape20: TShape;
    Shape21: TShape;
    Shape22: TShape;
    Shape23: TShape;
    Shape24: TShape;
    Shape25: TShape;
    Shape26: TShape;
    Shape27: TShape;
    Shape28: TShape;
    Shape29: TShape;
    Shape30: TShape;
    Shape31: TShape;
    Shape32: TShape;
    Shape33: TShape;
    Shape34: TShape;
    Shape35: TShape;
    Shape36: TShape;
    Shape37: TShape;
    Shape38: TShape;
    Shape39: TShape;
    Shape40: TShape;
    Shape41: TShape;
    Shape42: TShape;
    Label1: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure xy(Sender: TObject);
    procedure pruefe(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  feld: array[1..7,1..6of TShape;
  farbe:TColor;
  spalte,zeile:integer;
  const
  NamePrefix = 'Shape';

implementation

{$R *.dfm}

procedure TForm1.pruefe (Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
   gewonnen,p,i,n:integer;
   lol:Tcolor;
begin
   xy(Tshape(Sender));
   n:=1;
   p:=0;
   if farbe = clred then
         farbe := clyellow
      else
         farbe := clred;
   feld[1,1].brush.color := farbe;
   {while p = 0 do
   begin
      if feld[n,zeile].Brush.color = clwhite then
      begin
         feld[n,zeile].brush.color := farbe;
         p:=1;
      end
      else
      begin
         n:=n+1;
         if n =8 then
         begin
            showmessage('Achtung, diese Spalte ist bereits voll!');
            exit;
         end;
      end;
   end;}

   edit1.Text :=inttostr(spalte);
   edit2.Text :=inttostr(zeile);
   {for i:=1 to 3 do
   begin
      if feld[zeile+i,spalte].Brush.color=farbe then
         begin
            gewonnen:=gewonnen+1;
            if gewonnen=3 then showmessage('Gewonnen!');
         end;
   end;}

   //if feld[1,1].brush.color = lol then edit1.text:='lol';


end;

procedure TForm1.FormCreate(Sender: TObject);  //Aufruf der Form, auf alles shapes.onmousedown wird die prozedur pruefe gelegegt, und das shape auf den array gesetzt
var
  i,x,y: Integer;
begin
  for i := 1 to 42 do
    begin
       with TShape(FindComponent(NamePrefix + IntToStr(i))) do
       begin
         onmousedown :=pruefe;
       end;
     end;

     for y:=1 to 7 do
     begin
        for x:=1 to 6 do
        begin
           feld[x,y]:=TShape(FindComponent(NamePrefix + IntToStr(i))); //feld[x,y] wird auf das angeklickte shape gesetzt
        end;
     end;
end;

procedure TForm1.xy(Sender: TObject);
var
   i,a,b :integer;
begin
   a:=1;
   b:=1;
   spalte:=1;
   zeile:=1;
   for i := 1 to 42 do
   begin
      if TShape(Sender) = TShape(FindComponent(NamePrefix + IntToStr(i))) then
      begin
         spalte := b;
         zeile := a;
      end
      else
      begin
         a := a+1;
         if a = 7 then
         begin
            a:=1;
            b:=b+1;
         end;
      end;
   end;

end;

end.


wie gesagt, compilen kann ich, sobald ich allerdinfs auf eines der shapes klicke, würgt dat ding komplett ab :/
Ich glaube es liegt daran, dass ich die shapes iwie falsch auf den array gesetzt habe.. oder so ^^ sorry(feld[x,y]:=TShape(FindComponent(NamePrefix + IntToStr(i)));)
Ich weiss nicht genau wie das richtig geht, und es wäre prima, wenn mir einer von euch weiterhelfen könnte ;)

MfG
Euer Warheart


Moderiert von user profile iconNarses: Topic aus Sonstiges (Delphi) verschoben am Fr 02.05.2008 um 20:29
Fabian E.
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 554

Windows 7 Ultimate
Visual Studio 2008 Pro, Visual Studion 2010 Ultimate
BeitragVerfasst: Fr 02.05.08 20:31 
Hast du das gnaze schonmal durchdebugged? an welcher stelle gibts denn die exception?

gruß
Jann1k
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 866
Erhaltene Danke: 43

Win 7
TurboDelphi, Visual Studio 2010
BeitragVerfasst: Fr 02.05.08 21:00 
Der Fehler steckt hier:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
     for y:=1 to 7 do
     begin
        for x:=1 to 6 do
        begin
           feld[x,y]:=TShape(FindComponent(NamePrefix + IntToStr(i))); //feld[x,y] wird auf das angeklickte shape gesetzt
        end;
     end;


Du musst einfach x und y vertauschen (entweder bei der arrayzuweisung oder die variablen für die Schleifen), dann bleibst du innerhalb des arrays.
Warheart Threadstarter
Hält's aus hier
Beiträge: 2



BeitragVerfasst: Fr 02.05.08 23:01 
also die exception kommt an dieser stelle:

feld[1,1].brush.color := farbe;

von daher kann man auch aussschliessen, dass der array außerhalb liegt...
ich glaub das problem liegt eher hier:
feld[x,y]:=TShape(FindComponent(NamePrefix + IntToStr(i)));
der setzt das zwar auf auf das shape.. aber aber die eigenschaften wzb .brush.color übernimmt der nicht.. kann das sein?
denn das feld 1,1 muss zwangsläufig existieren...

MfG