- Home /
Converting a javascript to C#
Would anyone be so kind as to help me convert this script to c#?
var cols:int = 4;
var rows:int = 4;
var totalCards:int = cols*rows;
var matchesNeededToWin:int = totalCards * 0.5;
var matchesMade:int = 0;
var cardW:int = 100;
var cardH:int = 100;
var aCards:Array;
var aGrid:Array;
var aCardsFlipped:ArrayList;
var playerCanClick:boolean;
var playerHasWon:boolean = false;
function Start()
{
playerCanClick = true;
aCards = new Array();
aGrid = new Array();
aCardsFlipped = new ArrayList();
for(i=0; i<rows; i++)
{
aGrid[i] = new Array();
for(j=0; j<cols; j++)
{
aGrid[i][j] = new Card();
}
}
}
function OnGUI ()
{
GUILayout.BeginArea (Rect (0,0,Screen.width,Screen.height));
BuildGrid();
GUILayout.EndArea();
print("building grid!");
}
function BuildGrid()
{
GUILayout.BeginVertical();
for(i=0; i<rows; i++)
{
GUILayout.BeginHorizontal();
for(j=0; j<cols; j++)
{
var card:Object = aGrid[i][j];
if(GUILayout.Button(Resources.Load(card.img),
GUILayout.Width(cardW)))
{
Debug.Log(card.img);
}
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
}
class Card extends System.Object
{
var isFaceUp:boolean = false;
var isMatched:boolean = false;
var img:String;
function Card()
{
img = "robot";
}
}
Why do you want to convert from one language to another? You can use both Javascript and c# in a Unity project.
I find converting something you will need to do sometimes because I have had troubles when using multiple languages in Unity with complication errors and such. Still can't figure how to resolve that so I tend to rewrite the code or use the language ins$$anonymous$$d.
Here's some links I found useful in converting between C# / JS :
http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html
Answer by cdrandin · Jan 24, 2013 at 09:51 PM
I will convert a few of them as they have similar syntax
int cols = 4;
int rows = 4;
int totalCards = cols*rows;
int matchesNeededToWin = totalCards * 0.5;
int matchesMade = 0;
public void Start()
{
playerCanClick = true;
aCards = new Array();
aGrid = new Array();
aCardsFlipped = new ArrayList();
for(i=0; i<rows; i++)
{
aGrid[i] = new Array();
for(j=0; j<cols; j++)
{
aGrid[i][j] = new Card();
}
}
}
I think this is right.
Answer by Hyperion · Jan 24, 2013 at 10:02 PM
I won't convert everything for you, but I'll tell you how.
For everything that says var something:int=number, change that to int something=number, and the same applies to bools, floats, etc...
For all 'function's, change it to void.
Don't forget to encompass everything in a class (which I'm sure you know how to do)
Unfortunately, I've never tried having two classes in one script, so I don't know how to change that part to C# .
Hope this helps.
Your answer
Follow this Question
Related Questions
Need help converting Javascript to C# 3 Answers
this script in Javascript?? 0 Answers
convert c# to JavaScript Problem 0 Answers
Convert c# to Js? 1 Answer
TouchPad in C# 3 Answers