quick question

BlitzMax Forums/BlitzMax Beginners Area/quick question

chwaga(Posted 2008) [#1]
why doesn't this work??

'FIRST ATTEMPT AT BASIC OOP
Graphics 1280, 1024
SeedRnd(MilliSecs())

AutoMidHandle 1
Global List:TList = New TList
Type dude

	Field x, y, rot
	
	Method New()
		x = Rnd(0, 1280)
		y = Rnd(0, 1024)
		rot = Rnd(0, 360)
		List.AddLast(Self)
	End Method
	
	Method draw()
		For dude = EachIn List
			SetRotation(rot)
			DrawRect(x, y, 15, 15)
		Next
	End Method
	
	Method update()
		If KeyDown(KEY_LEFT) Then rot:-1
		If KeyDown(KEY_RIGHT) Then rot:+1
		draw()
	End Method

End Type

Global d:dude = New dude

While Not KeyHit(KEY_ESCAPE)

	d.update()
	Flip

Wend
End 



Gabriel(Posted 2008) [#2]
Because you haven't declared a variable called dude.

Local D:Dude
For D=EachIn List



chwaga(Posted 2008) [#3]
oops, thanks!


chwaga(Posted 2008) [#4]
so how can I do that except spawning multiple dudes, all of which are affected by the left/right arrow keys?
(sorry, totally new to bmax)


tonyg(Posted 2008) [#5]
If you only want one dude then only create one.
If you want multiple then try having a create function and calling it multiple times.
You can get a lot from :
Beginners guide to BlitzMax
and Learning Object-Oriented Programming in Blitzmax



chwaga(Posted 2008) [#6]
so why doesn't this work:
'FIRST ATTEMPT AT BASIC OOP
Graphics 1280, 1024
SeedRnd(MilliSecs())

AutoMidHandle 1
Global List:TList = New TList
Type dude

	Field x, y, rot
	
	Method New()
		x = Rnd(0, 1280)
		y = Rnd(0, 1024)
		rot = Rnd(0, 360)
		List.AddLast(Self)
	End Method
	
	Method draw()
	
		SetRotation(rot)
		DrawRect(x, y, 15, 15)
		
	End Method
	
	Method update()
		If KeyDown(KEY_LEFT) Then rot:-1
		If KeyDown(KEY_RIGHT) Then rot:+1
		Local d:dude
		For d = EachIn List
			draw()
		Next
	End Method
	
	Function Create:dude()
		Return New dude
	End Function

End Type

For n = 0 To 100
	d:dude = dude.Create()
Next

While Not KeyHit(KEY_ESCAPE)

	Cls
	d.update()
	Flip

Wend
End 



Htbaa(Posted 2008) [#7]
While Not KeyHit(KEY_ESCAPE)

	Cls
	For Local d:dude = EachIn List
		d.update()
	Next
	Flip

Wend



Czar Flavius(Posted 2008) [#8]
For n = 0 To 100
	d:dude = dude.Create()
Next

While Not KeyHit(KEY_ESCAPE)

	Cls
	d.update()
	Flip

Wend


Each time in the loop you create a new dude variable, d, and store in it a New dude. But each time in the loop you are overwriting d with a link to the next dude.

So after the loop you have overwritten d 99 times and the d variable contains a link to the last dude that you made. You probably noticed that only one dude updates? That is why. d does not contain all the dudes, only one. Each time you given a new dude, it forgets the old one.

(But the dude does still exist.. it's in the list, see below)

While Not KeyHit(KEY_ESCAPE)

	Cls
	For Local d:dude = EachIn List
		d.update()
	Next
	Flip

Wend


This will work because of the list you've made! Yipee!!

Each time you make a new dude it is added to the list. Unlike d, List can contain more than one dude - it has 100!

The for loop used by Htbaa shows how to access each item in the list. It tells the program to creatue a variable d, (which can only hold one dude remember, but that's ok because it's temp variable). For each dude in the list, it is stored temporarily in d. Then the update method is used on d. Because of the for loop, though, every dude is updated.

Hopefully I've explained this so that you can understand it. The key thing is to understand where all these dudes are going (in the list) and how this works, and exactly what each variable means.

In your for loop to create the dudes you could even just say dude.Create() on its own, without d:dude =, because you never actually use that d :)


tonyg(Posted 2008) [#9]
This is what I would do with your code :
1) Get used to using Superstrict it makes you define each variable. At first this is a pain but solves problems before they happen.
2) Move you Dude list into the Due type. It is more self-contained that way.
3) Have a Dude update function so you don't need to loop a list in the mainloop.
4) Have your create loop handle the number to create rather than your main code.