Type buttons?

Blitz3D Forums/Blitz3D Beginners Area/Type buttons?

I am trying to make a game with kind of fancy windows style hud. I am using types for the buttons. I need some advise on how to do this the best way. Right now I am makeing nine buttons that will be used to ad energy taken from one of three user selected energy bars to compensate for damage done to the ships nine sections. Does that make sence?
Here is the code so far.

  Graphics 800,600

HidePointer 
;make the imiges to be used for the hud
mouseimage=CreateImage(20,20)
makemouse(mouseimage)

mousepoint=CreateImage(1,1)
makepoint(mousepoint)

 ;button=CreateImage(45,15)
;makebutton(button)

;make types to control size, location and function of each button
Type buttons
	Field x
	Field y
	Field length
	Field height
	Field image
	Field mesage$
	Field if_clicked#
	Field energy#
	Field num#
End Type 


Global repair1.buttons = New buttons
repair1\x = GraphicsWidth()-150
repair1\y = GraphicsHeight()-300
repair1\length = 30
repair1\height = 15 
repair1\image = CreateImage(30,20)
repair1\mesage$ = "F1 "
 SetBuffer ImageBuffer(repair1\image)
	Color 100,100,255
	Rect 0,0,repair1\length,repair1\height,0
	Color 140,140,255
	Line 0,0,0,repair1\height-3
	Line 0,0,repair1\length,0
	;message$
	Text 0,0,repair1\mesage$ 

Global repair2.buttons = New buttons
repair2\x = GraphicsWidth()-110
repair2\y = GraphicsHeight()-300
repair2\length = 30
repair2\height = 15 
repair2\image = CreateImage(30,20)
repair2\mesage$ = "F2 "
 SetBuffer ImageBuffer(repair2\image)
	Color 100,100,255
	Rect 0,0,repair2\length,repair2\height,0
	Color 140,140,255
	Line 0,0,0,repair2\height-3
	Line 0,0,repair2\length,0
	;message$
	Text 0,0,repair2\mesage$ 


 



	 







SetBuffer BackBuffer()
While Not KeyHit(1)
Cls

drawimages(mouseimage,mousepoint)
DrawImage repair1\image,repair1\x,repair1\y
DrawImage repair2\image,repair2\x,repair2\y


If ImagesOverlap (mousepoint,MouseX(),MouseY(),repair1\image,repair1\x,repair1\y) Then 


	If MouseDown(1) Then
	;subtract points from selected energy bar and aply it to repair damaged ship
	 Print "Its got focus!"
	     
		
		
	End If

End If
 
Flip

Wend

WaitKey()
End


Function makemouse(mouseimage)
	SetBuffer ImageBuffer(mouseimage)
	Color 100,60,250
	Line 0,0,20,10
	Line 0,0,10,20
	Line 20,10,10,20
	
End Function

Function makepoint(mousepoint)
	SetBuffer ImageBuffer(mousepoint)
	Plot 0,0
End Function



Function drawimages(mouseimage,mousepoint)
	DrawImage mouseimage,MouseX(),MouseY()
	DrawImage mousepoint,MouseX(),MouseY()
	
End Function


My main question is for buttons, am I better off using a for each next loop to create the nine buttons? Or should I continue the way I have started? Or should I abandon types and use data array?

Well, you can't use a For-Each loop to CREATE (New) the type instances because they wouldn't exist yet.

But you could go back and use a For-Each loop after you created all nine button instances.



Personally, I would probably create an array of types and use Data statements to hold the initialization data.

Something like this:
Type buttons
	Field x
	Field y
	;etc...
End Type

Dim repair.buttons(9)

For iter = 1 To 9
	repair(iter) = New buttons
	Read repair(iter)\x
	Read repair(iter)\y
	;etc...
Next


Data 110, 300 ;etc...


That's an excellently clear example of how to make Arrays of Types, Wolron! Thanks - something I've been a little scared of, but would find invaluable!!!

I think you need a repair(iter)=New buttons in there. You can use array of type or just plain types - choice is yours.

done

Ya I think I will stick with plain types if they will work.
I am still just getting the hang of using types.
I'm like Mallice, array of types scare me, but I will experiment with them. What is the advantage to using an
array of types? I'm sure they will make the code shorter,
but can you do things like work on them as a whole?
For instance in my if statment that asks if the mouse is over it, would I be able to just write one "If then" for all nine buttons?

Do exactly what you were doing before :)

For iter = 1 to 9
  If ImagesOverlap (mousepoint,MouseX(),MouseY(),repair(iter)\image,repair(iter)\x,repair(iter)\y)
    ;do something
  EndIf
Next
or...
For thisbutton.buttons = Each buttons
  If ImagesOverlap (mousepoint,MouseX(),MouseY(),thisbutton\image,thisbutton\x,thisbutton\y)
    ;do something
  EndIf
Next


Well I said I would stick to normal types but now I think its unavoidable. Not for a button though, its for a ship
rotation. I used the array of type code from above but I get "image does not exist". I think it is refering to the ship image, but I don't know why it whould say that.
;_______________________________________________________________________________________________________
;Players Space ship Type
;-------------------------------------------------------------------------------------------------------
Type carier
	Field x
	Field y
	Field image
	Field Health
	Field shield
	Field red_power
	Field green_power
	Field blue_power
	Field fighters
End Type

Global player.carier = New carier
player\x = GraphicsWidth()/2
player\y = GraphicsHeight()/2
player\health = 100
player\shield = 100
player\red_power = 100
player\green_power = 100
player\blue_power = 100
player\fighters = 12

player\image = CreateImage(50,50)
SetBuffer ImageBuffer(player\image)

;ship
;drawing of player ship
For i = 1 To 15
	Color 100-(i*5),100-(i*5),100-(i*5)

	Line 15-(i/10),15+i,35+(i/10),15+i ;front underchasi
Next
;....ect

;here I am trying to copy the player\image for the rotation
Dim rplayer.carier(359)
For loop=0 To 359
	rplayer(loop)\image=CopyImage(player\image)
	MidHandle rplayer(loop)\image
	RotateImage rplayer(loop)\image,loop
	DebugLog loop
Next





First of all, carier is spelled carrier :)

At first glance, I can't see why.

Run your program in debug mode and add more DebugLog statements to your program to find out what exactly is causing the error.

I can,t believe it I forgot to put the carier(loop)=new carier in there. lol
Thanks for your help.