- Home /
Writing javascript for web and android
I'm getting multiple errors when I try to go from building a web to building for android. My program runs fine when it's in web mode, but for android it's giving me
Assets/Scripts/GameScript.js(26,13): BCE0005: Unknown identifier: 'i'
The error in the code below is actually on line 24, because it deletes the spaces at the top of my program when I copy it to here
Here is my code
//variables
var cols:int = 4; //the number of columns in the card grid
var rows:int = 4; //the number of rows in the card grid
var totalCards:int = cols * rows; //the number of cards in the game
var matchesNeededToWin:int = totalCards * 0.5; //if there are 16 cards, the player needs 8 matches to clear the board
var matchesMade:int = 0; //starting amount of matches made
var cardW:int = 100; //cards width and height
var cardH:int = 100;
var aCards:Array; //store all the cards made in this array
var aGrid:Array; //this array will keep track of the shuffled, dealt cards
var aCardsFlipped:ArrayList; //store the two cards that the player flips over
var playerCanClick:boolean; //flag this boolean to prevent the player from pressing buttons when we don't want them to
var playerHasWon:boolean = false; //store whether or not the player has won. Should probably start out false :)
function Start ()
{
playerCanClick = true; //we should let the player play, shouldn't we?
//initialize the arrays as empty lists
var aCards = new Array();
var aGrid = new Array();
var aCardsFlipped = new ArrayList();
/* -->this is where the error is*/ for(i=0; i<rows; i++)
{
aGrid[i] = new Array(); //create a new, empty array at index i
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();
}
class Card extends System.Object
{
var isFaceUp:boolean = false;
var isMatched:boolean = false;
var img:String;
function Card()
{
img = "robot";
}
}
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();
}
Always leave #pragma strict at the top of all your scripts, so you don't end up with any surprises when switching platforms like that.
Also, never use the JS Array class, and never use ArrayLists either. Always use typed collections such as built-in arrays or generic Lists.
O$$anonymous$$ that solved my unknown identifier problem, and I also included #pragma strict so i can get used to it since I'll be program$$anonymous$$g for mainly android. But now I get this error:
Assets/Scripts/GameScript.js(66,48): BCE0048: Type 'Object' does not support slicing.
Here is my code now. I put an arrow as I did before so it may be easier to see it. Thanks for the help.
#pragma strict
//variables
var cols:int = 4; //the number of columns in the card grid
var rows:int = 4; //the number of rows in the card grid
var totalCards:int = cols * rows; //the number of cards in the game
var matchesNeededToWin:int = totalCards * 0.5; //if there are 16 cards, the player needs 8 matches to clear the board
var matches$$anonymous$$ade:int = 0; //starting amount of matches made
var cardW:int = 100; //cards width and height
var cardH:int = 100;
var aCards:Array; //store all the cards made in this array
var aGrid:Array; //this array will keep track of the shuffled, dealt cards
var aCardsFlipped:ArrayList; //store the two cards that the player flips over
var playerCanClick:boolean; //flag this boolean to prevent the player from pressing buttons when we don't want them to
var playerHasWon:boolean = false; //store whether or not the player has won. Should probably start out false :)
function Start ()
{
playerCanClick = true; //we should let the player play, shouldn't we?
//initialize the arrays as empty lists
var aCards = new Array();
var aGrid = new Array();
var aCardsFlipped = new ArrayList();
for(var i=0; i<rows; i++)
{
aGrid[i] = new Array(); //create a new, empty array at index i
for(var j=0; j<cols; j++)
{
(aGrid[i] as Array)[j] = new Card();
}
}
}
function OnGUI ()
{
GUILayout.BeginArea (Rect (0,0,Screen.width,Screen.height));
BuildGrid();
GUILayout.EndArea();
}
class Card extends System.Object
{
var isFaceUp:boolean = false;
var is$$anonymous$$atched:boolean = false;
var img:String;
function Card()
{
img = "robot";
}
}
function BuildGrid()
{
GUILayout.BeginVertical();
for(var i=0; i<rows; i++)
{
GUILayout.BeginHorizontal();
for(var j=0; j<cols; j++)
{
error is right here --> var card:Object = aGrid[i][j];
if(GUILayout.Button(Resources.Load(card.img), GUILayout.Width(cardW)))
{
Debug.Log(card.img);
}
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
}
Like I said, don't use Array or ArrayList, use a generic List. Also don't use Object as a type, use the actual specific type.
Can you give me an example? I did some googleing but can't get my program to work when trying different examples.
Answer by aldonaletto · Aug 05, 2013 at 04:26 AM
It seems that #pragma strict is automatically set when compiling for Android, thus you must explicitly declare all variables you use. You must also define the variable type either explicitly or by initializing it with a constant of known type. In this case, you should modify the for statement:
for(var i=0; i<rows; i++) // declare i...
{
aGrid[i] = new Array(); //create a new, empty array at index i
for(var j=0; j<cols; j++) // and declare j as well!
{
aGrid[i][j] = new Card();
}
}
Remember to declare all variables used in your code, like in the other for statements.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Make an object move in the direction of touch 0 Answers
opening a webpage? 1 Answer
using Application.OpenURL for opening new tab? 2 Answers
Accelerator and how to limit player movement (Android) 0 Answers