code problem

BlitzMax Forums/BlitzMax Programming/code problem

emperor42(Posted 2012) [#1]
Very new to blitz, just trying to make a set of objects appear horizonantally every 100 pixels, but it seems to have a problem with the p variable for some reason.




Strict

Const WIDTH=1280,HEIGHT=960
Const DEPTH=32,HERTZ=60

Incbin "lunar_surface.png"

Global tile:TImage = LoadImage("incbin::lunar_surface.png")

Global objectlist:TList = New TList

Type Ttile
Field x#,y#

Method New ()
If objectlist = Null
objectlist = New TList
EndIf
objectlist.AddLast Self
End Method

Function Create:Ttile (x#, y#)
Local p:Ttile = New Ttile
p.x = x
p.y = y
objectlist.Addlast p
Return p
End Function

Function makechunk ()
Local n#
Local x=0
Local y=0
For n=1 To 8
x=x+100
Ttile.Create (x,y)
Next
End Function

Function drawtile ()
For p:Ttile = EachIn objectlist
DrawImage tile,x,y
Next
End Function

End Type

Graphics WIDTH,HEIGHT,DEPTH,HERTZ

Ttile.makechunk ()

While Not KeyHit( KEY_ESCAPE )

Cls

Ttile.drawtile ()

Flip

Wend

End


Muttley(Posted 2012) [#2]
Function drawtile ()
    For Local p:Ttile = EachIn objectlist	
        DrawImage tile,x,y
    Next
End Function


Try that.

You're not defining the variable before you're using it.

Last edited 2012


emperor42(Posted 2012) [#3]
thanks that seems to have worked and ive changed the drawtile () and makechunk () functions into methods, but now it cant recognise where ive called the Ttile.makechunk () method just before the main loop.


Yasha(Posted 2012) [#4]
That's because methods have to be called on an object: calling it with the expression "Ttile.makechunk" isn't doing that at all, just looks like it (it's actually trying to retrieve the "makechunk" constant function field from the type itself, which would work if makechunk was a constant function belonging to the whole type). You can only access functions that way.

Unless the procedure needs to use the object's fields (the members of the type that exist once per-instance rather than once per-program), it should stay a function, rather than a method. Different tools for different tasks.


Muttley(Posted 2012) [#5]
@emperor42: I think it might be helpful if you work through this excellent introduction to OOP in BlitzMax: http://www.alsbonsai.com/john/BlitzMax_OOP_Tutorial.pdf

It will clear up a lot of concepts for you hopefully.


Jesse(Posted 2012) [#6]
yes, go through the tutorial and this should make more sense

I believe this is what you are looking for:
[bbcode]
Strict

Const WIDTH=1280,HEIGHT=960
Const DEPTH=32,HERTZ=60

Incbin "lunar_surface.png"

Global tile:TImage = LoadImage("incbin::lunar_surface.png")

Global objectlist:TList = New TList

Type Ttile
Field x#,y#

Method New ()
If objectlist = Null
objectlist = New TList
EndIf
objectlist.AddLast Self
End Method

Function Create:Ttile (x#, y#)
Local p:Ttile = New Ttile
p.x = x
p.y = y
objectlist.Addlast p
Return p
End Function

Function MakeChunk ()
Local n#
Local x=0
Local y=0
For n=1 To 8
x=x+100
Ttile.Create (x,y)
Next
End Function

Function DrawChunk () '
For local p:Ttile = EachIn objectlist
p.drawtile()
Next

End Function

Method DrawTile () 'use of method
DrawImage tile,x,y
End Method

End Type

Graphics WIDTH,HEIGHT,DEPTH,HERTZ

Ttile.makechunk ()

While Not KeyHit( KEY_ESCAPE )

Cls

Ttile.DrawChunk ()

Flip

Wend

End
[/bbcode]
although, personally, I don't think(like) the "MakeChunk" and "DrawChunk" belong in the class but I think that is a personal preference.

Last edited 2012


emperor42(Posted 2012) [#7]
yeah thanks for the link ill look over it, as i said im very new to this so these tutorials are very useful. I tried running Jesse's code but it i says that the variable p is not identified.


Jesse(Posted 2012) [#8]
I didn't test it but it is supposed to be "For local p:Ttile". there might be some other minor errors as I were not able to test since I don't have the images.