OOP problem with Bmax
BlitzMax Forums/BlitzMax Beginners Area/OOP problem with Bmax| 
 | ||
| I can't get the following code to work. I created it to learn the basics of OOP. At the line e:enemy=e.create("bmax120.png") the execution stops without a message. Can anyone help me? P.S: I can see the potential of Bmax but Blitzplus was a hell lot easier to learn. Nathan. 
Strict
Graphics 800,600,0
Global List:TList = New TList
Type enemy
	Field x:Int
	Field y:Int
	Field dx:Int
	Field dy:Int
	Field gfx:Timage
	
	Function Create:enemy(f:String)
		e:enemy=New enemy
		e.x=Rnd(0,799)
		e.y=Rnd(0,599)
		e.dx=1
		e.dy=1
		e.gfx=LoadImage(f)
		List.addlast e
		Return e
	End Function
	
	Method Draw()
		DrawImage(gfx,x,y)
		If x>800 Then dx=-1
		If x<0 Then dx=1
		If y>600 Then dx=-1
		If y<0 Then dy=1
		x=x+dx
		y=y+dy
	End Method
End Type
Global e:enemy
Global j:Int
'***** MAIN *****
DebugStop()
List:TList=CreateList()
For j=0 To 2
	e:enemy=e.create("bmax120.png")
Next
While Not KeyDown(KEY_ESCAPE)
	Cls
	DebugStop()
	For e:enemy=EachIn List
		e.draw()	
	Next
	Flip
	FlushMem
Wend
EndGraphics
 | 
| 
 | ||
| It stops without a message as you have debugstop. Without this you get an Unhandled exception (beta doesn't have full debugger). You need to call... e:enemy = enemy.create("bmax120.png") <edit> In fact you only need... enemy.create("bmax120.png") | 
| 
 | ||
| Sorry, I was not clear enough. Debugstop() was only to get me to the debugger. Anyway if I take out this line and change the second line to Graphics 800,600,32 I get the same result the execution stops without ever reaching the while wend loop. | 
| 
 | ||
| Nathan, here is a version that works. I changed a few things cause I'm picky :) Let me know if you have any questions. Strict
Global EnemyList:tList = CreateList()
Type tEnemy
	Field X:Float
	Field Y:Float
	Field VX:Float
	Field VY:Float
	Field Gfx:tImage
	
	Function Create:tEnemy(f:String)
		Local e:tEnemy=New tEnemy
		e.X=Rnd(0,799)
		e.Y=Rnd(0,599)
		e.VX=1
		e.VY=1
		e.Gfx=LoadImage(f)
		EnemyList.AddLast(e)
		Return e
	End Function
	
	Method Draw()
		DrawImage(Gfx,X,Y)
		If X>800 Then VX=-1
		If X<0 Then VY=1
		If Y>600 Then VY=-1
		If Y<0 Then VY=1
		X :+ VX
		Y :+ VY
	End Method
End Type
Graphics 800,600,0
AutoMidHandle (True) 
For Local i:Int = 0 To 2 'Creates 3
	tEnemy.Create("bmax120.png")
Next
While Not KeyDown(KEY_ESCAPE)
	Cls
	For Local e:tEnemy = EachIn EnemyList
		e.Draw()	
	Next
	Flip
	FlushMem
Wend
EndGraphics | 
| 
 | ||
| @Nathan, you missed this bit... You need to call... e:enemy = enemy.create("bmax120.png") <edit> In fact you only need... enemy.create("bmax120.png") | 
| 
 | ||
| @tonyg Thanks for clearing this up. I've got it working now. @altitudems Thanks for the changes. I'm slowly getting used to the syntax. Nathan |