Methods und Functions in Types
Übersicht

![]() |
DamienXBetreff: Methods und Functions in Types |
![]() Antworten mit Zitat ![]() |
---|---|---|
Da ich heute mal Zeit hatte hab ich mir das OOP Tut von Jolinah mal durchgelesen und ein wenig getüftelt und bemerkt dass sich die Types in BMAX doch etwas anders handlen lassen als in BB.
So wollte ich die sache mal an nem kleinen GUI Beispiel probieren und hab folgendes Problem: Erst mal der Code: Code: [AUSKLAPPEN] Strict Global w:Twindow Type TWindow Field name:String Field pos_x:Int ,pos_y:Int Method CreateGuiWindow (name:String,pos_x:Int,pos_y:Int) w:Twindow = New Twindow w.name = name w.pos_x = pos_x w.pos_y = pos_y End Method EndType w.CreateGuiWindow ("Alex",100,100) ---> Method of Null Object Ich denke die Fehlermeldung kommt daher weil ich die Methode Aufrufe bevor ich das Objekt erstelle was nach meiner Logik nicht gehen sollte. Dann hab ich es mit Functions in Types versucht und da hatte ich ähnliche Probleme. Ich will das Objekt aber nich ausserhalb einer Methode/Funktion erstellen drum frage ich mal nach einer Lösung und hoffe das ich nich einfach Tomaten auf den Augen hab! Grüße |
||
Lets make things better... |
![]() |
FOODy |
![]() Antworten mit Zitat ![]() |
---|---|---|
Hast du es auch schon so probiert?
Code: [AUSKLAPPEN] Strict
Global w:Twindow Type TWindow Field name:String Field pos_x:Int ,pos_y:Int Function CreateGuiWindow:TWindow(name:String,pos_x:Int,pos_y:Int) Local w:Twindow = New Twindow w.name = name w.pos_x = pos_x w.pos_y = pos_y Return w EndFunction EndType w = TWindow.CreateGuiWindow ("Alex",100,100) |
||
![]() |
DamienX |
![]() Antworten mit Zitat ![]() |
---|---|---|
Danke dir das sieht sehr plausibel aus.
Nur stell ich mir die Frage warum du "w" an die Funktion zurückgibst und vor allem was passiert intern wenn ich ein Objekt zurückgebe? |
||
Lets make things better... |
![]() |
FOODy |
![]() Antworten mit Zitat ![]() |
---|---|---|
Mit der "Type"-Funktion CreateGuiWindow erstellst du eine Instanz die dann zurückgegeben wird und mit der du dann weiter arbeiten kannst.
Hier wird die in der Funktion erstellt. Code: [AUSKLAPPEN] Local w:Twindow = New Twindow
Hier werden einige Werte gesetzt: Code: [AUSKLAPPEN] w.name = name
w.pos_x = pos_x w.pos_y = pos_y Und damit die Instanz nicht verloren geht (also ihr Gültigkeitsbereich "nicht verlässt") wird sie hier weitergegeben: Code: [AUSKLAPPEN] Return w
Ansonsten würde der Speicher, der beim erstellen der Instanz reserviert wurde, wieder freigegeben von dem GC. Hoffentlich konnte ich wenigstens 2% verständlich rüberbringen >_> EDIT: Die Funktionen in einem Type sind "Type Intern". Methoden sind "Instanz Intern". Gruß, FOODy |
||
![]() |
DamienX |
![]() Antworten mit Zitat ![]() |
---|---|---|
Danke ja ich denke ich konnte dir folgen. Ich kann also nur mit diesem Objekt weiterarbeiten wenn ich es wieder zurückgebe.
Nur im mom stellt sich mir n neues Problem auf... (nicht verzagen ^^ ![]() Code: [AUSKLAPPEN] Strict Graphics 800,600,16,85 Global w:Twindow Global list:Tlist = New Tlist Type TWindow Field name:String Field pos_x:Int ,pos_y:Int Function CreateGuiWindow:TWindow(name:String,pos_x:Int,pos_y:Int) Local w:Twindow = New Twindow w.name = name w.pos_x = pos_x w.pos_y = pos_y list.addlast(w) Return w EndFunction Function UpdateGui() For w:Twindow = EachIn list DrawRect w.pos_x,w.pos_y,100,200 Next End Function EndType While Not KeyDown (key_escape) Cls w = TWindow.CreateGuiWindow ("Alex",100,100) Flip Wend Warum werden die Werte aus dem Objekt nicht aus der Liste in die Hauptschleife übernommen? Schätze da isn Fehler bei der Übergabe oder der Liste... *schnief* Vorbei die Zeit mit each und co ![]() |
||
Lets make things better... |
![]() |
Farbfinsternis |
![]() Antworten mit Zitat ![]() |
---|---|---|
DamienX hat Folgendes geschrieben: *schnief* Vorbei die Zeit mit each und co
![]() Nö, ist immer noch da die Zeit: Code: [AUSKLAPPEN] SuperStrict Type TTest Global _list:TList Field name:String Function Add:TTest(name:String) Local test:TTest = New TTest If test._list = Null Then test._list = New TList test._list.AddLast(test) test.name = name Return test End Function End Type Local test:TTest test = TTest.Add("Horst") test.Add("Müller") test.Add("Meier") For test:TTest = EachIn test._list Print test.name Next DebugStop() |
||
Farbfinsternis.tv |
![]() |
Rone |
![]() Antworten mit Zitat ![]() |
---|---|---|
1. du erzeugst 85 fenster die Sekunde...
2. Weiß nicht genau was du meinst, aber ich würde die liste als statisches Feld in den Typ machen... Code: [AUSKLAPPEN] Strict Graphics 800,600 Type TWindow Global list:Tlist Field name:String Field pos_x:Int ,pos_y:Int Method New() If Not list Then list = New TList list.addlast Self End Method Method Destroy() list.Remove Self End Method Function CreateGuiWindow:TWindow(name:String,pos_x:Int,pos_y:Int) Local w:Twindow = New Twindow w.name = name w.pos_x = pos_x w.pos_y = pos_y Return w EndFunction Function UpdateGui() If list Then For Local w:Twindow = EachIn list DrawRect w.pos_x,w.pos_y,100,200 Next EndIf End Function EndType Local w:Twindow = TWindow.CreateGuiWindow ("Alex",100,100) While Not KeyDown (key_escape) Cls TWindow.UpdateGui() Flip Wend |
||
![]() |
DamienX |
![]() Antworten mit Zitat ![]() |
---|---|---|
Ja Leude an sowas merkt man dann dass man erst mal ne Pause machen sollte....
Code: [AUSKLAPPEN] Strict Graphics 800,600,16,85 Global w:Twindow Global list:Tlist = New Tlist Type TWindow Field name:String Field pos_x:Int ,pos_y:Int Function CreateGuiWindow:TWindow(name:String,pos_x:Int,pos_y:Int) Local w:Twindow = New Twindow w.name = name w.pos_x = pos_x w.pos_y = pos_y list.addlast(w) Return w EndFunction Function UpdateGui() For w:Twindow = EachIn list DrawRect w.pos_x,w.pos_y,100,200 Next End Function EndType w = TWindow.CreateGuiWindow ("Alex",100,100) <----- !!!! While Not KeyDown (key_escape) Cls Twindow.UpdateGui() <------ !!!! Flip Wend Das wars was ich wollte ^^ Ich hol mir n Kaffee ![]() |
||
Lets make things better... |
![]() |
Jolinah |
![]() Antworten mit Zitat ![]() |
---|---|---|
Eine Variante wäre z.B. auch:
Code: [AUSKLAPPEN] Global controls:TList = new TList Type TControl Abstract Field pos_x:Int, pos_y:Int Method Update() Abstract End Type Type TWindow Extends TControl Function Create:TWindow(x:Int, y:Int) Local w:TWindow = new TWindow w.pos_x = x w.pos_y = y controls.AddLast(w) '<-- Control in die Liste einfügen beim erstellen return w End Function Method Update() pos_x :+ 1 End Method End Type Local win1:TWindow = TWindow.Create(10, 10) 'Alle Controls updaten, egal ob Fenster, Button etc. For Local c:TControl = EachIn controls c.Update() Next 'Oder z.B. nur alle Fenster die in der Liste sind durchgehen For Local win:TWindow = EachIn controls win.pos_x :+ 2 Next So hat jedes Control eine Update Methode. Und egal ob Fenster, Button etc. es lassen sich alle Objekte die von TControl erben auch als Control benutzen, das heisst man muss nichts umwandeln. |
||
![]() |
DamienX |
![]() Antworten mit Zitat ![]() |
---|---|---|
So ich mal wieder ![]() Hab mich n wenig mit gespielt und versuch ein wenig mit vererbung zu experimentieren.. hab in diesem Beispiel aber irgendwo nen Fehler drin. Code: [AUSKLAPPEN] Strict Graphics 800,600,16,85 Global Windows:TList = New Tlist Global Win:TWindow Type TObjects Abstract Field pos_x,pos_y Field size_x, size_y Method Update() Abstract End Type Type TWindow Extends TObjects Function CreateGuiWindow(pos_x:Int , pos_y:Int ,size_x:Int ,size_y:Int) Local w:TWindow = New TWindow w.pos_x = pos_x w.pos_y = pos_y w.size_x = size_x w.size_y = size_y Windows.AddLast (w) Return w End Function Method Update() For Local w:Twindow = EachIn Windows DrawRect (w.pos_x, w.pos_y, w.size_x, w.size_y) Next End Method End Type Win:TWindow = TWindow.CreateGuiWindow(100,100,100,100) While Not KeyDown (key_escape) Cls w.Update() Flip Wend Warscheinlich wieder irgendwo n dicker schnitzer drin.. ^^ Grüße |
||
Lets make things better... |
![]() |
FOODy |
![]() Antworten mit Zitat ![]() |
---|---|---|
Code: [AUSKLAPPEN] Function CreateGuiWindow(pos_x:Int , pos_y:Int ,size_x:Int ,size_y:Int)
Local w:TWindow = New TWindow w.pos_x = pos_x w.pos_y = pos_y w.size_x = size_x w.size_y = size_y Windows.AddLast (w) Return w End Function Der Rückgabewert von CreateGuiWindow ist TWindow. Also muss es so lauten: Code: [AUSKLAPPEN] Function CreateGuiWindow:TWindow(pos_x:Int , pos_y:Int ,size_x:Int ,size_y:Int)
Local w:TWindow = New TWindow w.pos_x = pos_x w.pos_y = pos_y w.size_x = size_x w.size_y = size_y Windows.AddLast (w) Return w End Function ;D EDIT: Fehler 2: Code: [AUSKLAPPEN] While Not KeyDown (key_escape)
Cls w.Update() Flip Wend Ich glaub damit meinst du das: Code: [AUSKLAPPEN] While Not KeyDown (key_escape)
Cls Win.Update() Flip Wend |
||
![]() |
DamienX |
![]() Antworten mit Zitat ![]() |
---|---|---|
Puh dann wars doch nicht GANZ so schlimm... ^^
Danke dir wir lesen uns beim nächsten schnitzer ![]() Grüße |
||
Lets make things better... |
Übersicht


Powered by phpBB © 2001 - 2006, phpBB Group