Drawing commands from within Methods go boom.

Monkey Forums/Monkey Programming/Drawing commands from within Methods go boom.

ErikT(Posted 2011) [#1]
I get the below error in the browser for html5 and TypeError: Error #1009 for flash builds. This happens whenever I try to use Drawline/Drawrect commands from within a Method.


<localhost>

TypeError: Cannot convert 'bb_graphics_renderDevice' to object



Code in question:

Method Draw(offsetx:Float = 0, offsety:Float = 0, boundingbox:Int = False)
	DrawLine (tl_corner.x - offsetx, tl_corner.y - offsety, br_corner.x - offsetx, tl_corner.y - offsety)', False)
	DrawLine (br_corner.x - offsetx, tl_corner.y - offsety, br_corner.x - offsetx, br_corner.y - offsety)', False)
	DrawLine (br_corner.x - offsetx, br_corner.y - offsety, tl_corner.x - offsetx, br_corner.y - offsety)', False)
	DrawLine (tl_corner.x - offsetx, br_corner.y - offsety, tl_corner.x - offsetx, tl_corner.y - offsety)', False)
End Method



Any ways around this?


therevills(Posted 2011) [#2]
I think we need more code to look at to help you.

Are you setting up the mojo app correctly?


ErikT(Posted 2011) [#3]
The mojo app is set up within the collision module where the tlBox class is. Without it I get an error: Identifier 'DrawLine' not found.

#rem 
	bbdoc: Type For handling Axis Aligned Bounding Boxes
	about: <p>This type can be used To create bounding boxes For the purpose of collision checking. This is the type used To stored objects in #tlQuadTree. It's
	extended by #tlCircle And #tlPolygon. To implement collision checking in you game/app will probably End up inlcluding these as a Field within
	your own types, And possibly extending these types so that they can contain a Field linking back To your own entity/actor types. Use #SetPosistion And #Move
	To align them in your game world, using these methods also ensures that they will be updated within the quadtree If they belong in one.</p>
	<p>It's worth noting that if you want a bounding box that can be orientated then create a 4 sided poly using a #tlPolygon.</p>
	<p>The world coordinates are stored as a vector within the Field World, so you can use world.x And world.y To retreive the coordinates of the box.</p>
#End
Class tlBox

	Field vertices:tlVector2[]
	Field tformvertices:tlVector2[]
	Field normals:tlVector2[]
	Field tformmatrix:tlMatrix2 = New tlMatrix2
	
	Field tl_corner:tlVector2	'top left corner
	Field br_corner:tlVector2	'bottom right corner
	
	Field width:Float
	Field height:Float
	
	Field quadlist:TList = CreateList()	'list of quad nodes this rect is in
	Field quads:Int						'number of quads the rect is in
	Field AreaCheckCount:Int
	
	Field quadtree:tlQuadTree
	Field collisiontype:Int
	
	Field world:tlVector2
	Field boxoffset:tlVector2 = New tlVector2
	Field scale:tlVector2 = New tlVector2
	Field velocity:tlVector2 = New tlVector2
	
	Field handle:tlVector2 = New tlVector2

	Method New()
		tformmatrix.Create()
		boxoffset.Create(0, 0)
		scale.Create(1, 1)
		velocity.Create(0, 0)
		handle.Create(0, 0)
	End Method
	
	Field collisionlayer:Int
	
	Field _data:Object
		
	#rem
		bbdoc: Create a New #tlBox
		returns: New tlBox
		about: Creates a New Bounding box that you can use For collision checking And adding To a #tlQuadTree. The x And y coordinates represent
		the top left corner of the bounding box. You can also assign some data To the boundary as handy way To store some extra info about the boundary.
	#End
	Method Create:tlBox(x:Float, y:Float, w:Float, h:Float, layer:Int = tlLAYER_1, Data:Object = Null)
		If w < 0
			x+=w
			w = Abs(w)
		End If
		If h < 0
			y+=h
			h = Abs(h)
		End If
		vertices = New tlVector2[4]
		handle.x = w / 2
		handle.y = h / 2
		vertices[0] = New tlVector2
		vertices[0].Create(-handle.x, -handle.y)
		vertices[1] = New tlVector2
		vertices[1].Create(-handle.x, h - handle.y)
		vertices[2] = New tlVector2
		vertices[2].Create(w - handle.x, h - handle.y)
		vertices[3] = New tlVector2
		vertices[3].Create(w - handle.x, -handle.y)
		normals = New tlVector2[4]
		tformvertices = New tlVector2[4]
		For Local c:Int = 0 To 3
			normals[c] = New tlVector2
			normals[c].Create(0, 0)
			tformvertices[c] = New tlVector2
			tformvertices[c].Create(0, 0)
		Next
		handle.x = 0
		handle.y = 0
		tl_corner = New tlVector2
		tl_corner.Create(0, 0)
		br_corner = New tlVector2
		br_corner.Create(0, 0)
		world = New tlVector2
		world.Create(x + w / 2, y + h / 2)
		UpdateNormals()
		TForm()
		collisionlayer = layer
		_data = Data
		Return Self
	End Method

'	More methods...

	#rem
		bbdoc: Draw this tlBox
		about: Use this If you need To draw the bounding box For debugging purposes
	#End
	Method Draw(offsetx:Float = 0, offsety:Float = 0, boundingbox:Int = False)
		DrawLine (tl_corner.x - offsetx, tl_corner.y - offsety, br_corner.x - offsetx, tl_corner.y - offsety)', False)
		DrawLine (br_corner.x - offsetx, tl_corner.y - offsety, br_corner.x - offsetx, br_corner.y - offsety)', False)
		DrawLine (br_corner.x - offsetx, br_corner.y - offsety, tl_corner.x - offsetx, br_corner.y - offsety)', False)
		DrawLine (tl_corner.x - offsetx, br_corner.y - offsety, tl_corner.x - offsetx, tl_corner.y - offsety)', False)
	End Method



The method is called like so:

	' test obstacle
	Scene_Wall[1] = CreateBox(946, 212, 40, 220)
	Scene_Wall[1].Draw(946,212)



... where Scene_Wall[1] is a tlBox object.


therevills(Posted 2011) [#4]
This works okay:



What version Monkey are you using? And the age old Monkey question - Have you deleted your build folder?


muddy_shoes(Posted 2011) [#5]
Are you calling the Draw method somewhere other than inside the context of an OnRender() call?


ErikT(Posted 2011) [#6]
Hmm.. your example works fine for me too. Monkey version is 42. I tried deleting the build folder and it made no difference. Should I delete this? Won't monkey overwrite everything in there anyway?

EDIT: @muddy_shoes: checking it now.

EDIT2: I did! Put it inside the OnRender call and it works. Dang I feel stupid now, hehe :P Thanks for the help!


therevills(Posted 2011) [#7]
When moving from versions of Monkey it is best to delete the build folder.

Where were you trying to draw it? I tried OnUpdate and Monkey tells me off saying Rendering must be done within the OnRender method.


ErikT(Posted 2011) [#8]
I did it in OnUpdate() too with no errors until it got opened in the browser. I had it put inside an initialization function though and that only gets executed once.


therevills(Posted 2011) [#9]
When I move the box.Draw() into the OnUpdate method, it compiles but when the browser opens I get:

"Rendering operations can only be performed inside OnRender"

I'm using v42b...


Rixarn(Posted 2011) [#10]
ErikT, I'm just curious... you´re commenting your code as if it were bmax code. Can you document it with docmods? Or is just the habit of commenting like that... because i still dont get a hang on the monkey documentation system... I'm really tempted to write my own documentation tool...


ErikT(Posted 2011) [#11]
@therevills
I'm using Opera, maybe the error message varies across browsers?

@Rixarn
It's just a habit. I have no idea what docmods is to be honest :)