types

BlitzMax Forums/BlitzMax Beginners Area/types

how can i use types to create a visible object in a window?

like this ? not quite sure what you mean.


Strict

Graphics 800,600,32


Type TMyObject

	Field x:Int
	Field y:Int
	Field xSize:Int
	Field ySize:Int

	Function Create:TMyObject(x_size:Int,y_size:Int)
		Local tmpObj:TMyObject = New TMyObject

		tmpObj.xSize = x_size
		tmpObj.ySize = y_size
		
		Return tmpObj
	End Function
	
	Method Update(x:Int,y:Int)
		Self.x = x
		Self.y = y
	End Method
	
	Method Draw()
		DrawRect Self.x,Self.y,Self.xSize,Self.ySize
	End Method

End Type


Global obj:TMyObject = TMyObject.Create(20,20)

While Not KeyDown(KEY_ESCAPE)
	obj.Update(MouseX(),MouseY())
	obj.Draw()
	Flip
	Cls
Wend



you really need to read this tutorial
http://www.blitzbasic.com/Community/posts.php?topic=42519

see what you get for this:
Type TGameObject
	Field x,y,xv,yv
End Type
Cls
Type TBullet Extends TGameObject
	Field x:Int
	Field y:Int
	Function playershoot(x:Int,y:Int)
		TBullet.x = sx
		TBullet.y = sy
	EndFunction
	Function update()
		TBullet.x = +1
	EndFunction
EndType


sorry... stupid mistake. didnt make a new bullet before i manipulated it.

never mind, still doesnt work!
new code:
ype TGameObject
	Field x,y,xv,yv
End Type

Type TBullet' Extends TGameObject
	Field x:Int
	Field y:Int
	Function playershoot(x:Int,y:Int)
	Local Bullet:TBullet = New TBullet
		TBullet.x = sx
		TBullet.y = sy
	EndFunction
	Function update()
		TBullet.x = +1
	EndFunction
EndType


Hallo,

Me thinks you've still to grasp a thing or two about Max's OO usage ;-)

Looking at your code snippet, you want to create a TBullet object using the playershoot() function. That's cool. Only what you want to do is return that new instance you create back somewhere... eg
Function playershoot:TBullet(x:Int,y:Int)
	Local Bullet:TBullet = New TBullet
	TBullet.x = sx
	TBullet.y = sy
	Return Bullet
EndFunction

...then you would call it using something along the lines of
Local myBullet:TBullet = TBullet.playershoot( x, y)

your myBullet variable would then contain a reference to a TBullet object which you can then processes in an update loop etc...

Now, onto the update() "function". As it stands, you can't do what you have in the code.
I would suggest you change it to a Method instead :
Method update()
	x :+ 1
End Method

This way you can call it in the following way :
myBullet.update()

If you really wanted to use a function and also wanted to reference a types' field using TBullet.x, you would have to change the Field to a Global. But then "x" would be global for all instances of a TBullet oject (that is, every TBullet object you had would have the same x value).

There are some nice OO Max tutorials about which might help get your head around it. I'm sure someone has some links handy they could post? :-)

drnmr, Seriously, take a look at that link I posted, in it is the best tutorial right now available, Beginners Guide to BMax, you need to read that.

now i have a problem with max not recognizing the functions later in my code.

have you gone through the Beginners Guide yet ?

yep

well please post code so people can help you

[codebox]
Type TBullet
Field x:Int
Field y:Int
Function playershoot(x:Int,y:Int)
Global Bullet:TBullet
Bullet = New TBullet
Bullet.x = sx
Bullet.y = sy
EndFunction
Function draw()
DrawImage imbullet, bullet.x,bullet.y
EndFunction
Function update()
Bullet.x = +1
EndFunction
[\codebox]

If you put the function in the Type you must call it like

TBullet.playershot(x,y)

not playershot(x,y)

i am.
Type TBullet
Field x:Int
Field y:Int
Function playershoot(x:Int,y:Int)
Global Bullet:TBullet
Bullet = New TBullet
Bullet.x = sx
Bullet.y = sy
EndFunction
Method draw()'doesnt recognise TBullet.draw() later on
DrawImage imbullet, bullet.x,bullet.y
EndMethod
Method update()
Bullet.x = +1
EndMethod

Type TBullet
Field x:Int
Field y:Int
endtype

Global Bullet:TBullet

Function playershoot(x:Int,y:Int)

Bullet = New TBullet
Bullet.x = sx
Bullet.y = sy
EndFunction

Function draw()
DrawImage imbullet, bullet.x,bullet.y'now it says it dosnt recognize the variables "x" and "y"
EndFunction

Function update()
Bullet.x = +1
EndFunction 


You delcared it global within a function.

I think dreamora thinks the whole thing is within a type because you didnt put a end type. And if that is the case you only use global stuff within type functions.

And in that case the below should work
Type TBullet
global x:Int
global y:Int




Function playershoot(x:Int,y:Int)
TBullet.x = sx
TBullet.y = sy
EndFunction

Function draw()
DrawImage imbullet, TBullet.x,TBullet.y'now it says it dosnt recognize the variables "x" and "y"
EndFunction

Function update()
TBullet.x = +1
EndFunction 


But I should tell you now it looks like your going down the wrong direction.

You should be looking at list, methods, etc.

Editings:
Heres what i would do...
Type TBullet

	Field x%, y%
	
	Function Create:TBullet(_x%, _y%)
	
		Local b:TBullet = New TBullet
		b.x = _x
		b.y = _y
		Return b
		
	End Function
	
	Method Draw()
	
		DrawRect x, y, 10, 10
		
	End Method
	
	Method Update()
	
		x:+ 10
			
	End Method
	
End Type

Type TPlayer

	Field x%, y%
	Field bulletList:TList
	
	Function Create:TPlayer(_x%, _y%)
	
		Local p:TPlayer = New TPlayer
		p.bulletList = CreateList()
		p.x = _x
		p.y = _y
		Return p
		
	End Function
	
	Method Update()
	
		If MouseDown(1) Then
		
			bulletList.AddLast ( TBullet.Create(x, y) )
			
		EndIf
		
		For Local b:TBullet = EachIn bulletList
		
			b.Update()
			If b.x > GraphicsWidth() + 10 Then 
				bulletList.Remove(b)
				b = Null
			EndIf
			
		Next
		
	End Method
	
	Method Draw()
	
		DrawOval x, y, 20, 20
		
		For Local b:TBullet = EachIn bulletList
		
			b.Draw()
			
		Next
		
	End Method
	
End Type

Graphics 800, 600

Local player:TPlayer = TPlayer.Create(MouseX(), MouseY())

While Not KeyHit(KEY_ESCAPE)

	Cls
	
	player.x = MouseX()
	player.y = MouseY()
	
	player.Update()
	player.Draw()
	
	Flip
	
Wend

End


Tho that dose work I would suggest reading and understanding the tutorials that have been written about the subject of OOP design and BMax OOP.

no offense, but almost nothing in this topic has helped so far.