help with tile map
Monkey Forums/Monkey Beginners/help with tile map
| ||
| looked at different codes and trying to understand tile maps, 2d arrays and matricies, but having a little trouble. I managed to get this to load but "error null object access" keeps happening. for some reason i have to put field bg on both calsses or i get error bg not declared. pls help have to understan tile maps. should i have went with screen width divide by tile size idk also can someone point me to scrolling single image background heres the code.
Import mojo
Class Mygame Extends App
global level:Level
Field width:Int = 320
Field hieght:Int =240
Global bg:Image
Method OnCreate()
SetUpdateRate(60)
level= New Level()
bg = LoadImage("level.png",128,128,8,Image.MidHandle)
End
Method OnUpdate()
End
Method OnRender()
Cls(0,0,0)
level.Draw()
End
End
Class Level
Field tileH:Int =128
Field tileW:Int =128
field bg:Image
Field cols:Int = 5
Field rows:Int = 3
Field map:Int [][]= [[1,1,1,1,1],
[2,2,2,2,2],
[3,6,2,5,4]]
Method Draw:Void()
For Local i:Int = 0 until rows
For Local j:Int = 0 until cols
If map[i][j] =1 Then DrawImage (bg,i*tileW,j*tileH,1)
'Select map[i][j]
'Case 1
' DrawImage (bg,i*tileW,j*tileH,1)
' End
End
End
End
End
Function Main:int()
New Mygame
End
|
| ||
| Putting a global variable inside a class makes the variable global only to the class. it means that you can only access it from methods and functions with in that class. your "bg" variable is only directly accessible from "MyGame" class. to access it from any other class you need to pass it as a parameter to that class functions or methods or create the global outside any individual class. having the same global in two different classes only means that both are two completely different variables. |
| ||
| success!!! that did it jesse thanks. i did know about that but forgot hehe. thanks for pointing that out! |
| ||
| I thought globals where scoped to the module, ie. anything in the current file has access? |
| ||
| Globals outside classes are accessible to classes, methods and function of any other file the uses the file that contain the Global. Globals within Classes are only accessible "directly" to methods and function with in the class. class globals can be access from outside the the class by prefixing it with the class name or an instance of the class: MyClass.MyGlobal or myClassInstance.MyGlobal" |