function and methods

BlitzMax Forums/BlitzMax Programming/function and methods

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

right or wrong i don't know, but basicly how i would do it is:

if i want it to be called from outside the type i use a function, and if i want it to be called from inside the type i use a method.

here is a VERY BASIC example of what i mean

Graphics 800,600

'gfx
Global enemy:timage = LoadImage("enemypic.png")


'list to hold the enemies
Global EnemyList:TList = CreateList()

'***********************MAIN LOOP**************************
While Not KeyDown(key_Escape)
	Cls
	
	'call the CreateEnemy function if 'E' is pressed
	If KeyHit(key_E) Then TEnemy.CreateEnemy(Rand(100,700),Rand(100,500))
	
	'call the Update function for TEnemy
	TEnemy.Update()
	
	Flip
Wend
End
'**********************************************************


Type TEnemy()
	Field x#,y#			'current position
	Field tx#,ty#		'move target position
	Field ismoving		'is the enemey moving?
	
	Function CreateEnemy:TEnemy(x#,y#)
		'stuff to create an enemy goes here
		
		Local e:TEnemy = New TEnemy
		
		e.x# = x#
		e.y# = y#
		
		EnemyList.AddLast e
	End Function
	
	Method Move()
		'very basic move structure
		
		If Self.x# < Self.tx# Then Self.x#:+0.5
		If Self.x# > Self.tx# Then Self.x#:-0.5
		If Self.y# < Self.y# Then Self.y#:+0.5
		If Self.y# > Self.y# Then Self.y#:-0.5
		
		If Self.x# > Self.tx# And Self.x# < Self.tx# Then Self.x# = Self.tx#
		If Self.y# > Self.ty# And Self.y# < Self.ty# Then Self.y# = Self.ty#
		
		If Self.x# = Self.tx# And Self.y# = Self.ty# Then Self.ismoving = 0
	End Method
	
	Method GetMoveTarget()
		'get new target for enemy to move to
		
		Self.tx# = Rnd(100,700)
		Self.ty# = Rnd(100,500)
		Self.ismoving = 1
	End Method
	
	Method Draw()
		'draw enemy image here
		
		DrawImage enemy,Self.x#,Self.y#
	End Method	
	
	Function Update()
		For Local e:TEnemy = EachIn EnemyList			
			'call the Draw method for this particular enemy
			
			e.Draw()
			
			If e.ismoving = 0
				'call the GetMoveTarget method for this particular enemy
				
				e.GetMoveTarget()
			Else
				'call the Move method for this particular enemy
				
				e.Move()
			End If
		Next
	End Function
End Type


i hope that helps some