How to create a new object from within a method?
BlitzMax Forums/BlitzMax Beginners Area/How to create a new object from within a method?
| ||
Strict Incbin "ball.png" Global ball_list:TList = CreateList() Type ball Field x:Int,y:Int,s:Int Global ballcount = 0 Method create() b:ball = New ball ListAddLast ball_list,b b.x = Rnd(10,630) b.y = Rnd(10,470) b.s = Rnd(3,8) ballcount :+1 End Method End Type Graphics 640,480,16,0 Repeat Cls For b:ball = EachIn ball_list If b.ballcount < 100 b.create EndIf Next FlushMem;Flip Until KeyHit(KEY_ESCPE) The above code does not work. I have a method that creates a new ball. The problem is when I try to call the mothod from within a loop it throws an error. Can anyone help? Just to say that I have been using BlitzMax now for quite some time but without trying any of the OOP stuff. This is my second attempt at OOP and its beyond me for now. |
| ||
This wont work will it? I think because I have to create an object of type ball first then call b.create. So maybe the way I have set it up is wrong. |
| ||
Without testing, something like this might be more like what you want: |
| ||
You need to use a Type Function (not a Method): There were other problems with undeclared variables and a misspelled key const. |
| ||
What Beaker said. You can only invoke methods on pre-existing objects. There is however nothing to pprevent you from creating objects in a method - provided you have your creational responsabilities sorted out. For example: A Ball should not be able to spawn additional balls, however it is perfectly reasonable that the balls' collector object (TList here) spawns additional balls. |