function and types help

BlitzMax Forums/BlitzMax Beginners Area/function and types help

redmoth(Posted 2011) [#1]
Hi, this might be pretty easy, or a silly mistake, but can I ask why I can't make the code in the updatePlayers() run.
Function UpdatePlayers()
	If player.hasBall = 1
		ballX = player.x
		ballY = player.y
	End If

End Function

Function DrawMatchGFX()

	For Local player:playerType = EachIn playerList
		
		UpdatePlayers()
		DrawImage(imgPlayer,player.x,player.y,0)
		
	Next
	
	DrawImage(imgBall,ballX,ballY,0)
	
End Function




redmoth(Posted 2011) [#2]
I've just tried this, but it doesn't work well, it just runs for each object, and not the one specific object that has 'player.hasBall = 1'


Function UpdatePlayers(hasBall:Int)
	
	If hasBall = 1

		ballX = player.x
		ballY = player.y
	
	End If

End Function

Function DrawMatchGFX()

	For Local player:playerType = EachIn playerList
		
		UpdatePlayers(player.hasBall)
		DrawImage(imgPlayer,player.x,player.y,0)
		
	Next
	
	DrawImage(imgBall,ballX,ballY,0)
	
End Function



redmoth(Posted 2011) [#3]
ah I think I've worked it out with:

Function UpdatePlayers(hasBall:Int,playerx:Int,playery:Int)


Brucey(Posted 2011) [#4]
Or something like this...



Type Drawable
	Field x:Int
	Field y:Int
	
	Field img:TImage


	Method draw()
		DrawImage img, x, y
	End Method

End Type

Type BallType Extends Drawable

End Type

Type PlayerType Extends Drawable

	Method update(ball:BallType)
		If hasBall Then
			ball.x = x
			ball.y = y
		End If
	End Method
	
End Type

Function UpdatePlayers()
	For Local player:playerType = EachIn playerList
		player.update(ball)
	Next
End Function

Function DrawMatchGFX()

	For Local player:playerType = EachIn playerList
		player.draw()
	Next
	
	ball.draw()
	
End Function



Czar Flavius(Posted 2011) [#5]
Where are the variables player and ball defined? Are they global?

If player.hasBall = 1
		ballX = player.x
		ballY = player.y
	End If


This does not find the player for which hasBall is 1. It checks whether one variable you have provided called "player" has the ball, but I can't see where this variable is coming from.


Brucey(Posted 2011) [#6]
Are they global?

Since he's using Functions, one has to assume everything is global...


Czar Flavius(Posted 2011) [#7]
I wasn't sure if he meant he could get it to compile or not.


Brucey(Posted 2011) [#8]
Ah :-)

I hoped to fill in a bunch of gaps with my example - I just like to make things up!


redmoth(Posted 2011) [#9]
thanks brucey,

I'll have to have some practice using methods, as it does look a lot easier