TileMap code error! Struggling a tiny bit!
Monkey Forums/Monkey Programming/TileMap code error! Struggling a tiny bit!
| ||
I'm basically trying to get the hand of using Arrays etc. I'm porting a game from Max to Monkey. A little help would be appreciated so if anybody can fix my code or point out where I'm going wrong I'd be greatfull. Ta! |
| ||
Your '2D' array indexing looks like it has problems for starters.function index2D(x,y,arrSizeX) index=x+y*arrSizeX return index end Use something like that to calculate your array index into a 1D array to act like a 2D array. |
| ||
Sorry but I'm not following it. Any chance on elaborating on the code maybe with a usage example to fit my code? Hope that's ok? |
| ||
const int MAPSIZEX=10 const int MAPSIZEY=10 mapArray=new int[MAPSIZEX*MAPSIZEY] for x=0 until 10 for y=0 until 10 mapBlock=mapArray[index2D(x,y,MAPSIZEX)] end endSomething like this (untested) to simulate a 2D array. Obviously my nested loop does nothing useful, you would presumably stick a drawimage in there to render your map. The monkenstein example with Monkey does the same thing. |
| ||
Imagine you have a grid: width * height With a 2D array, you would access it like : grid[x, y] With a 1D array, you can access it as JG describes, in order to find the index for the correct x,y coordinate given one long list of entries : index = x + y * width So you are probably better having two loops, For X... For Y... And accessing your array index with the above calculation.. Just a thought :-) EDIT : just like what JG wrote (again)... |
| ||
Thanks guys for that. The extra explanations helped. Ta! |
| ||
Me again. I'm having a slow day. First I'd like to say that the coding structure needed for Monkey-Mojo has stung me a bit and so not to start a seperate thread I'll post what I find difficult here. 1) I am confused as to when to use ":=" or similar because it appears from a build compile point of view, it doesn't throw any errors, that certain algorithms accept both := e.g like a FOR..Next loop, and =. 2) I still need help with working out this code so some more help would be appreciated. |
| ||
:= is an auto typing feature, as far as I can remember. It saves you having to add the type after : |
| ||
Yep, if it is causing confusion, just don't use it. Myself, and probably a lot of other potential help, can't be bothered firing an application up to help out too often, so if you explain what it's doing wrong, you have more chance of a response. In OnRender, you are not specifying the mapblock to draw, just the whole array. Does it even compile? Also, if you are using index2D function, stick with it. You are mixing and matching in OnCreate. |
| ||
why don't you use array of arrays:local array:int[][] ' this initializes the two dimension array if you want the array to be 20x20 you would do something like this: first you declare/initialize the first dimension( the tiles down, can also be the tiles across but it helps if you think of it this way). array = new int[20] this created an array of 20 down x 0 across. at this point it's not yet usable. as you can imagine. now you can create the size of each of the elements of the 1D array by declaring the size of each of the elements making it a 2D array. like this: for local i = 0 until 20 array[i] = new int[20] next so now each of the 1D elements of the array is 20 elements making it a 2d array. one thing to note is that each of the element of the second dimension does not have to be the same size. array[1] can be 20 elements while array[2] can be 40 or even 1 element in length. once you have created the elements the can be accessed like this: array[1][3] = 23 as long as the index is valid. to make it easier you can make a function that creates an array of integers of any size: Function array:Int[][](across:Int,down:Int) Local n:Int[][] n = New int[across] For Local i:Int = 0 Until across n[i] = New Int[down] Next Return n End Function and use it like this local d:int[][] = array(20,20) ' corrected error here [edited] to fix a little error in the last code line |
| ||
Hey Jesse, that's some nice code and explained in such a way that even I get it. :D Thanks for that! |
| ||
Wouldn't be:local array:int[][] ' this initializes the two dimension array array = [new Int[20], new Int[20]] the same? |
| ||
Doesn't that create a 2x20 rather than a 20x20? |
| ||
Function array:Int[][](across:Int,down:Int) Local n:Int[][] n = New int[across] For Local i:Int = 0 Until across n[i] = New Int[down] Next Return n End Function and use it like this local d:int[][] = array(20,20) ' corrected error here Back from a long weekend I've just tried this and I keep getting a Syntax error on the second quoted line. ALso, with people chiming in all sorts of different methods to doing it I'm even more confused now; Jesse code makes sense but I have no idea why I get a syntax error on the second line. |
| ||
Hi, This should work: Import mojo Function Main() Local x_size=10,y_size=20 Local map:Int[x_size][] For Local x=0 Until x_size map[x]=New Int[y_size] Next Print map[0][0] Print map[x_size-1][y_size-1] End There is currently a bug to do with initializing arrays using code like: [ New Int[10],New Int[20] ] |
| ||
Hi, Also, this works for me too - had to change the first 'New'... Function IntArray2D:Int[][](across:Int,down:Int) Local n:Int[][] n = New Int[across][] For Local i:Int = 0 Until across n[i] = New Int[down] Next Return n End Function |
| ||
Thanks Mark! :) |
| ||
Hi, Slightly more efficient version: Function IntArray2D:Int[][](across:Int,down:Int) Local n:Int[across][] For Local i:Int = 0 Until across n[i]=New Int[down] Next Return n End Function |
| ||
Something must be wrong with my code. I still get syntax errors because I'm not sure how to assign, I think, the function to a new Array. Here is my current code. |
| ||
Don't know if you solved the problem already but just so it can help anybody else with the same problem, remove the subfolder of the loadImage file name. it shuld be:tiles = LoadImage("tiles.png", 8, Image.DefaultFlags) |
| ||
As long as there is a Data folder within MyApp.data it will still work:monkeyproject/ MyApp.monkey MyApp.data/ Data/ tiles.png This is how Ive been organising my project: monkeyproject/ MyApp.monkey MyApp.data/ graphics/ tiles.png sounds/ hit.ogg fonts/ ariel.fnt |