Wave's~ BlitzMax Tutorial for NG | ~ November, 2015 ~ Version 11
Beginners guide to BlitzMax |
Functions in Types
In the tank-example it would be appropriate to add a function too. This function could take care of the creation of the Tank. Methods can only be called if you have an instance of the Type. Like "Tank.Draw()" while functions are called from the Type itself e.g. TTank.Create(). Functions are like global methods bundled with the Type. Remember that functions can be standalone too. You can always have functions that are outside of a type, as in BlitzBasic, but if you put them inside your type your code gets more modular, you can for example have several functions in different types with the same name. I added a function to the Tank-example and made the TankList a part of the Tank-Type. Note that functions in types can refer directly to Globals and constants in that type, because they are global too. See "ModelName". What I mean is that there is no reason to, in a Type-Function, to write TTank.ModelName = "Alpha 11", because you can write, ModelName = "Alpha 11" instead.
Strict Global Number_of_Tanks = 20 Type TankType 'Renamed from TTank to TankType Field X#,Y# Field Dir, Armor = 100 Field Speed# = 0.2, Size = 25 Global ModelName$ = "Delta 11" Global List:TList 'Keep our tanks inside our TankType Method Draw() DrawRect( X, Y, Size, Size ) End Method Method Move() X:+Speed*Cos( Dir ) Y:+Speed*Sin( Dir ) End Method Function Create() Local NewTank:TankType NewTank = New TankType NewTank.Armor = Rand( 5 )*10 + 150 NewTank.X = Rand(5, 800 ) NewTank.Y = Rand( 5, 600 ) NewTank.Dir = Rand( 0, 360 ) If Not List Then List = CreateList() End If List.AddLast( NewTank ) End Function End Type Graphics 800,600,0 For Local N = 1 To Number_of_Tanks TankType.Create() Next While Not KeyDown(Key_Escape) Cls For Local Tank:TankType = EachIn List 'Global in this type Tank.Draw() DrawText "Number of Tanks : "+List.Count(), 20, 20 DrawText "Press ESC to exit", 20, 40 Tank.Move() Next Flip Wend
This time the code didn't become smaller but we did it easier to work with in the future. Note the For-Eachin-Tank.List-Loop, this will give an error if we haven't already created a list. The List is created as soon as you create your first tank, but sometimes the game starts before you have an instance of a certain type. Because of this it is wise to add an if-statement around the For Eachin TankList Loop. We can make this code even more object oriented by adding an UpdateAll() Function. Instead of an if-statement I exit the function if no list has been created.
Function UpdateAll() If Not List Return 'Exit function if List = null For Local Tank:TankType = EachIn List 'Global in this type Tank.Draw() DrawText "Number of Tanks : "+List.Count(), 20, 20 DrawText "Press ESC to exit", 20, 40 Tank.Move() Next End Function
Call the function from the main-loop. Replace the while/wend loop above with the following while/wend loop.
While Not KeyDown(Key_Escape) Cls TankType.UpdateAll() Flip Wend
To Index | Next Page | page 11