- Home /
How to get an array of sprite arrays to work (javascript)?
I'm trying to make an array of sprite arrays (using javascript) This script handles the sprites of the players
var Classes : Sprite[,];//this doesn't work
var classSpearmanSprite : Sprite[];
var classKnightSprite : Sprite[];
var classFarmhandSprite : Sprite[];
function Start()
{
Classes = [classSpearmanSprite, classKnightSprite, classFarmhandSprite];
}
function CharacterSprites ()
{
var allChars : GameObject[];
allChars = GameObject.FindGameObjectsWithTag("Player");//put all players in an array
for (var i = 0; i < allChars.length; i++) //for each player in the array
{
var classNum = allChars[i].GetComponent(CharStats).charClass; //get class number from the player
var neededSprite : int = allChars[i].GetComponent(PlayerMove).playerDirection;//returns either 1,2,3,4
// the next three lines i'd prefer just to have a single line:
//Classes[classNum,neededSprite], so which ever class a player is it is all done through a single line
//so i don't have to add more lines for every class in the future
if (classNum == 0) allChars[i].GetComponent(SpriteRenderer).sprite = classSpearmanSprite[neededSprite];
if (classNum == 1) allChars[i].GetComponent(SpriteRenderer).sprite = classKnightSprite[neededSprite];
if (classNum == 2) allChars[i].GetComponent(SpriteRenderer).sprite = classFarmhandSprite[neededSprite];
//for each player, get the sprite of the renderer, make it the sprite of the player's class and the character's direction, direction is an int: 1,2,3,4
}
}
thanks in advance
Answer by Odd Sock · Apr 07, 2014 at 03:46 PM
static var Classes : Sprite[,] = new Sprite[3,9];
function Start()
{
for (var j : int =0; j < 9; j++)
{
Classes[0,j] = classSpearmanSprite[j];
Classes[1,j] = classKnightSprite[j];
Classes[2,j] = classFarmhandSprite[j];
}
}
function CharSprites()
{
allChars[i].GetComponent(SpriteRenderer).sprite = Classes[classNum,neededSprite];
}
this works for me, but thanks for the effort fafase
Answer by fafase · Apr 03, 2014 at 06:11 AM
Try to use jagged arrays:
var Classes : Sprite[][] = new Sprite[3][];
var classSpearmanSprite : Sprite[];
var classKnightSprite : Sprite[];
var classFarmhandSprite : Sprite[];
function Start(){
Classes[0] = classSpearmanSprite;
Classes[1] = classKnightSprite;
Classes[2] = classFarmhandSprite;
}
then you use those like:
void Method(){
// Get the first spearman sprite
Sprite sp = Classes[0][0];
}
Whenever I tried to do: var Classes : Sprite[][] = new Sprite[3][]; it gives me an error (insert semicolon)
According to this Js does not take the type:
var Classes = new Sprite[3][];
You could also use a Dictionary:
import System.Collections.Generic;
enum SpriteType{Spearman,$$anonymous$$night,Farmhand}
var dict = new Dictionary.<SpriteType, Sprite[]>();
function Start(){
dict.Add(SpriteType.Spearman,classSpearmanSprite);
dict.Add(SpriteType.$$anonymous$$night,class$$anonymous$$nightSprite);
dict.Add(SpriteType.Farmhand,classFarmhandSprite);
}
function GetSpriteArray(type:SpriteType)
{
return dict[type];
}
Good point with this principle, you can only use sprite type that are declared in the enum.
thanks, yet I'm unsure how to set the sprite of a player using classNum and neededSprite.
The magic of enum is that they also are integers. The first is 0, the second is 1 and so on.
i've got this so far: allChars[i].GetComponent(SpriteRenderer).sprite = dict[whatgoeshere?]; but how do I get the sprite out of it?
Your answer
Follow this Question
Related Questions
Physics.OverlapSphere with a min radius? 1 Answer
Javascript - find a value in an array 0 Answers