Problem with my Pong game (Vars not set)

BlitzMax Forums/BlitzMax Beginners Area/Problem with my Pong game (Vars not set)

qim(Posted 2009) [#1]
Hi,

I'm trying to create my first game, of course a Pong clone. ;-)

It's OOP, and I have a problem with my vars here.

AutoMidHandle(True)
Graphics 640,480
HideMouse()

Global GameObjectList:TList = CreateList()

Type TGameObject
	Field X:Int = 0
	Field Y:Int = 0
	Field Image:TImage
	
	Method DrawSelf()
		DrawImage Image, X, y		
	End Method
	
	Method UpdateState() Abstract
End Type

Type TBall Extends TGameObject
  Field OrigSpeed:Int = 3
	Field SpeedX:Int = OrigSpeed
	Field SpeedY:Int = OrigSpeed

Function Create:TBall(Filename:String, xstart:Int = 320, ystart:Int = 240)
		Local Ball:TBall = New TBall
		Ball.X = xstart
		Ball.Y = ystart
		Ball.Image = LoadImage(filename)
		Ball.SpeedX = Rand(-OrigSpeed, OrigSpeed)
		Ball.SpeedY = Rand(1, OrigSpeed)
		ListAddLast GameObjectList, Ball
		Return Ball
	End Function

	Method UpdateState()
	  DrawText (X, 0, 0)
		DrawText (Y, 0, 20)
		DrawText (SpeedX, 0, 30)
		DrawText (SpeedY, 0, 40)
		X:+SpeedX
		Y:+SpeedY
		If X > 640
			SpeedX = -OrigSpeed
		End If
		If X < 0
			SpeedX = OrigSpeed
		End If
		If Y > 480
			SpeedY = -OrigSpeed
		End If
		If Y < 0
			SpeedY = OrigSpeed
		End If
	End Method
End Type

Ball:TBall = TBall.Create("ball.png")


Repeat
	Cls
	For o:TGameObject = EachIn GameObjectList
		o.DrawSelf()
		o.UpdateState()
	Next
	Flip
Until KeyDown(KEY_ESCAPE) Or AppTerminate()


That's the code for my ball class, missing the player classes (which works). The ball is set, but whatever I try, it always stays in the center of the screen. I thought, with setting the speed vars within my Create function they would move. But they don't.

Can anyone tell me, why this happens?

Thanx,
Oliver


DavidDC(Posted 2009) [#2]
Perhaps
Const OrigSpeed:Int = 3
?


qim(Posted 2009) [#3]
Nope, that isn't the problem. Even if I remove the OrigSpeed var the ball doesn't move.


DavidDC(Posted 2009) [#4]
Well it's bouncing about here and that's all I changed as far as I can recall :-)


qim(Posted 2009) [#5]
To what did you change it? :)


DavidDC(Posted 2009) [#6]


Works fine here (XP) The SuperStrict was just me hinting that you should use it :-) Didn't affect the bounce.


qim(Posted 2009) [#7]
So it was just using SuperStrict? Well, I definetly should use it then. ;-) Thanks!


DavidDC(Posted 2009) [#8]
Well not quite. SuperStrict just picked up the error.

You were incorrectly referencing a Field from within a Function. Changing that Field to a Const or Global would have fixed it. Or you could have gone

Ball.SpeedX = Rand(-Ball.OrigSpeed, Ball.OrigSpeed)

etc


qim(Posted 2009) [#9]
Ah, that explains it. Thanks a lot!

- qim