Jump to content

Javascript Class Functions?


Nohana

Recommended Posts

Hello,I want to create a Map class with the private functions "MultiDimensionalArray(r,c)", "CreateRandomMap()" and "SetRandom(x)". Now in my html file, I want to call "CreateRandomMap()". How should I do this?I thought it would be like in Java, and onclick="Map.CreateRandomMap()" would do the trick, but it doesn't work at all (JS hates me).I figured it could also be some problem with my script... So I'm posting it below, if anyone has the patience to check it. ('w')Please let me know how I can get CreateRandomMap() to work. Thanks!!! >ω<Oh and please, ignore all the alerts. I just use them to know till what point my code is working as expected.

function Map() {	var map = MultiDimensionalArray(12,12);	alert(map[11][11]);			//creates multidimensiona array	function MultiDimensionalArray(iRows,iCols)	{	var i;	var j;	   var a = new Array(iRows);	   for (i=0; i < iRows; i++)	   {		   a[i] = new Array(iCols);		   for (j=0; j < iCols; j++)		   {			   a[i][j] = 0;		   }	   }	   return(a);	} 		//place all 39 objects on map	function CreateRandomMap()	{		var objects = 0;		alert("Running here");		while( objects < 10 )		{			if (SetRandom(1) == true) object++;			alert("bullion");		}		while( objects < 14 )		{			if (SetRandom(2) == true) object++;			alert("gems");		}		while( objects < 17 )		{			if (SetRandom(3) == true) object++;			alert("chest");		}		while( objects < 35 )		{			if (SetRandom(4) == true) object++;		}		while( objects < 38 )		{			if (SetRandom(5) == true) object++;		}		while( objects < 39 )		{			if (SetRandom(6) == true) object++;			alert("DONE!");		}		return true;	}		//places object in a random tile if it's unused	function SetRandom(x)	{		var pos = 1;		pos = Math.round(Math.random*(144-1)+1);		if ( map[pos/12][pos%12] == 0 )		{		map[pos/12][pos%12] = x;		return true;		} else 			return false;	}}

Link to comment
Share on other sites

Nvm you know that already. I have no clue.

Link to comment
Share on other sites

I gave up on the class idea, anyway, I only need the CreateRandomMap() function to work that's all...The code seems to break off around the while's, I have no clue why. If you copy paste this code to any file you should be able to inspect the code.The alerts which I use here to let me know where the code is currently running, seem to (randomly) stop before the function completes its task. It goes like:1,2,3,4,false,4,5,6,false,6,7,8,9,10, Over! When it should go up to 39. :)Can anyone please help me fix this buggy code? It seems perfect to me, I don't know why it suddenly breaks out of the while's and end the function.(Code below)

var map = MultiDimensionalArray(12,12);	//creates multidimensiona arrayfunction MultiDimensionalArray(iRows,iCols){var i;var j;   var a = new Array(iRows);   for (i=0; i < iRows; i++)   {	   a[i] = new Array(iCols);	   for (j=0; j < iCols; j++)	   {		   a[i][j] = 0;	   }   }   return(a);} //place all 39 objects on mapfunction CreateRandomMap(){	var objects = 0;	while ( objects < 39 )	{		while( objects < 10 )		{			alert(objects);			if ( SetRandom(1) == true) objects++;		}		while( objects < 14 )		{			alert(objects);			if (SetRandom(2) == true) objects++;		}		while( objects < 17 )		{			alert(objects);			if (SetRandom(3) == true) objects++;		}		while( objects < 35 )		{			alert(objects);			if (SetRandom(4) == true) objects++;		}		while( objects < 38 )		{			alert(objects);			if (SetRandom(5) == true) objects++;		}		while( objects < 39 )		{			alert(objects);			if (SetRandom(6) == true) objects++;		}			}}//places object in a random tile if it's unusedfunction SetRandom(x){	var pos = 0;	pos = Math.round(Math.random()*143);	if ( map[Math.round(pos/12)][pos%12] == 0 )	{		map[Math.round(pos/12)][pos%12] = x;		return true;	} else {		alert("false");		return false; }}

Link to comment
Share on other sites

I don't know if this helps any. I see what you're doing but you probably know more than me. However, sometimes an outsider has a different perspective on the code and, though less knowledgeable, may still be able to see something out of whack? so here are some things I see in hope it may help you.

var pos = 0;	pos = Math.round(Math.random()*143);	if ( map[Math.round(pos/12)][pos%12] == 0 ) // Your second pos is % instead of / Is that what you want?	{		map[Math.round(pos/12)][pos%12] = x; // here too		return true;	} else {		alert("false");		return false; }

function MultiDimensionalArray(iRows,iCols) // do you mean iRows and "j"Cols ? not iCols{var i;var j;   var a = new Array(iRows);   for (i=0; i < iRows; i++)   {	   a[i] = new Array(iCols);	   for (j=0; j < iCols; j++)	   {		   a[i][j] = 0;	   }

Link to comment
Share on other sites

I don't know if this helps any. I see what you're doing but you probably know more than me. However, sometimes an outsider has a different perspective on the code and, though less knowledgeable, may still be able to see something out of whack? so here are some things I see in hope it may help you.
Haha, Thanks for the help, I appreciate it. But I fixed this a while ago. I just had to change from Math.round to Math.floor. Math.round would sometimes give me 12 as a result, which results in an array out of bound error. Took me quite long before I figured that out (I just hate JS). :/The [pos%12] is fine, since I don't want any value higher than 11. It will always return between 0 and 11. :)Conclusion, problem's solved.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...