Creating array? - arrays

I have this class but i wanna make it 2D dimensional Array. And i do not know how to do it...Everyone tells me to create 2D array so i can easy call it and read array...but i found this way of making items.
Now array or gird that will store this times would be helpful.
Any code to transfer this class to an array would be helpful.
Weapon base is Inheriting 2 additional Classes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace RPG
{
class WeaponList
{
private WeaponBase WoodenSword;
private WeaponBase RustySword;
private WeaponBase IronSword;
private WeaponBase SteelSword;
private WeaponBase BoardSword;
private WeaponBase MythrilSword;
private WeaponBase BloodSword;
private WeaponBase CoralSword;
private WeaponBase AncientSword;
public void CreateWeapon()
{
WoodenSword = new WeaponBase();
WoodenSword.ItemName = "Wooden Sword";
WoodenSword.ItemDescription = "A basic traning sword...can do some damage";
WoodenSword.IsStackable = true;
WoodenSword.Attack = 4;
WoodenSword.WeaponType = WeaponBase.WeaponTypes.Sword;
WoodenSword.ItemID = 1;
WoodenSword.Price = 10;
RustySword = new WeaponBase();
RustySword.ItemName = "Rysty Sword";
RustySword.ItemDescription = "Old rusty sword....still better than wooden one";
RustySword.Attack = 5;
RustySword.IsStackable = true;
RustySword.WeaponType = WeaponBase.WeaponTypes.Sword;
RustySword.ItemID = 2;
RustySword.Price = 50;
IronSword = new WeaponBase();
IronSword.ItemName = "Iron Sword";
IronSword.ItemDescription = "This sword has a broad and sturdy blade, but its iron construction makes it very heavy.";
IronSword.IsStackable = true;
IronSword.Attack = 6;
IronSword.WeaponType = WeaponBase.WeaponTypes.Sword;
IronSword.ItemID = 3;
IronSword.Price = 100;
SteelSword = new WeaponBase();
SteelSword.ItemName = "Steel Sword";
SteelSword.ItemDescription = "Hardend version of iron sword";
SteelSword.IsStackable = true;
SteelSword.Attack = 7;
SteelSword.WeaponType = WeaponBase.WeaponTypes.Sword;
SteelSword.ItemID = 4;
SteelSword.Price = 500;
BoardSword = new WeaponBase();
BoardSword.ItemName = "Borad Sword";
BoardSword.ItemDescription = "This broad-bladed sword is suited for large slashing strokes. It is inexpensive, but not particularly powerful. ";
BoardSword.IsStackable = true;
BoardSword.Attack = 8;
BoardSword.WeaponType = WeaponBase.WeaponTypes.Sword;
BoardSword.ItemID = 5;
BoardSword.Price = 800;
MythrilSword = new WeaponBase();
MythrilSword.ItemName = "Mythril Sword";
MythrilSword.ItemDescription = "A sword forged from the metal known as mythril. Its brilliantly shining blade is incredibly lightweight.";
MythrilSword.IsStackable = true;
MythrilSword.Attack = 10;
MythrilSword.WeaponType = WeaponBase.WeaponTypes.Sword;
MythrilSword.ItemID = 6;
MythrilSword.Price = 1600;
BloodSword = new WeaponBase();
BloodSword.ItemName = "Blood Sword";
BloodSword.ItemDescription = "The blade of this sword is a deep crimson, as if it were drenched in blood. It is cruelly sharp.";
BloodSword.IsStackable = true;
BloodSword.Attack = 8;
BloodSword.WeaponType = WeaponBase.WeaponTypes.Sword;
BloodSword.ItemID = 7;
BloodSword.Price = 1400;
BloodSword.specialAttack = "Drains Foe's HP";
CoralSword = new WeaponBase();
CoralSword.ItemName = "Coral Sword";
CoralSword.ItemDescription = "The handle of this single-edged sword has been decorated with intricate coral piecework.";
CoralSword.IsStackable = true;
CoralSword.Attack = 11;
CoralSword.WeaponType = WeaponBase.WeaponTypes.Sword;
CoralSword.ItemID = 8;
CoralSword.Price = 2200;
CoralSword.specialAttack = "Element: Thunder";
AncientSword = new WeaponBase();
AncientSword.ItemName = "Ancient Sword";
AncientSword.ItemDescription = "A sword constructed using ancient techniques that have long since perished from the world.";
AncientSword.IsStackable = true;
AncientSword.Attack = 11;
AncientSword.WeaponType = WeaponBase.WeaponTypes.Sword;
AncientSword.ItemID = 9;
AncientSword.Price = 3300;
}
}
}

Well I'm not sure what you're exactly doing, but making a 2D Array is quite simple:
int[,] MyArray = new int[3,4];
// Access like this:
MyArray[0,0] = 1;
MyArray[0,1] = 2;
MyArray[0,2] = 3;
// Etc...
// To Populate using a For Loop:
int i = 0;
for(int x=0; x < 3; x++)
for(int y=0; y < 4; y++)
{
i++;
MyArray[x, y] = i;
}
And that's how you make a 2D Array.
Edit:
Making it for a class is a similar process:
MyClass[,] MyArray = new MyClass[3,4];
// Access like this:
MyClass[0,0] = new MyClass();
MyClass[0,1] = new MyClass();
MyClass[0,2] = new MyClass();
// Etc...
// To Populate using a For Loop:
for(int x=0; x < 3; x++)
for(int y=0; y < 4; y++)
{
MyClass[x, y] = new MyClass();
}
As you can see making a 2D Array for a class is pretty much the same.

I suggest this way (vb-pseudo code).
Class WeaponBase
ItemName
ItemDescription
IsStackable
...
End Class
Class Weapons
inherits list (of WeaponBass)
Sub CreateWeapon
' here i will read data from XML or JSON instead of this below
w = new WeaponBase();
w.ItemName = "Wooden Sword";
w.ItemDescription = "A basic traning sword...can do some damage";
w.IsStackable = true;
add(w)
w = new WeaponBase();
w.ItemName = "Metal Sword";
w.ItemDescription = "A basic traning sword...can do some damage";
w.IsStackable = true;
add(w)
End Sub
Sub CreateNewItem(_name, _desc, _stackable)
w = new WeaponBase();
w.ItemName = _name;
w.ItemDescription = _desc;
w.IsStackable = _stackable;
add(w)
End Sub
End Class
and now you have a list of weapons that you can easily access and do whatevery you wish.

Related

Unable to add more entries to Firebase after 11

I have a database in firebase that is meant to hold user data and I'm able to get it to add account data up to number 10 (zero based), but after that, anytime I call the function to add to the database it doesn't show up in Firebase. I am using this for a game in Unity. Here are the function calls.
`
public void populateDatabase()
{
//populate static account data
AccountData _thisAccount = new AccountData();
_thisAccount.accName = Account.instance.NickName();
_thisAccount.accEmail = PlayerAccount._this.getUserEmail();
_thisAccount.accCash = 100;
_thisAccount.accGem = 100;
//Ranking Values
_thisAccount.Rank = 0;
//rookie
_thisAccount.RookieRank = 0;
_thisAccount.RookieAverage = 0;
_thisAccount.RookieScoreOne = 0;
_thisAccount.RookieScoreTwo = 0;
_thisAccount.RookieScoreThree = 0;
//Amature
_thisAccount.AmatureRank = 0;
_thisAccount.AmatureAverage = 0;
_thisAccount.AmatureScoreOne = 0;
_thisAccount.AmatureScoreTwo = 0;
_thisAccount.AmatureScoreThree = 0;
//Semi
_thisAccount.SemiProRank = 0;
_thisAccount.SemiAverage = 0;
_thisAccount.SemiProScoreOne = 0;
_thisAccount.SemiProScoreTwo = 0;
_thisAccount.SemiProScoreThree = 0;
//Pro
_thisAccount.ProRank = 0;
_thisAccount.ProAverage = 0;
_thisAccount.ProScoreOne = 0;
_thisAccount.ProScoreTwo = 0;
_thisAccount.ProScoreThree = 0;
_thisAccount.accTricks = Account.instance.ReturnTricks();
string tName = _thisAccount.getAccName();
Debug.Log("Name: " + tName + " account " + _thisAccount.accTricks.Length);
Debug.Log("Email: " + _thisAccount.accEmail);
Debug.Log("Cash: " + _thisAccount.accCash);
Debug.Log("Attempt to fill Database info");
ConstuctDatabase(usersdb, _thisAccount);
}
private void ConstuctDatabase(DatabaseReference AccountRef, AccountData _thisAccount)
{
int num = 0;
AccountRef.RunTransaction(MutableData =>
{
num++;
List<object> account = MutableData.Value as List<object>;
if (account == null)
{
account = new List<object>();
}
else
{
Debug.Log("continue");
}
Dictionary<string, object> newAccount =
new Dictionary<string, object>();
newAccount["AccountName"] = _thisAccount.accName;
newAccount["AccountEmail"] = _thisAccount.accEmail;
newAccount["Cash"] = _thisAccount.accCash;
newAccount["Gem"] = _thisAccount.accGem;
//Ranking Values
newAccount["Rank"] = _thisAccount.Rank;
//rookie
newAccount["RookieRank"] = _thisAccount.RookieRank;
newAccount["RookieAverage"] = _thisAccount.RookieAverage;
newAccount["RookieScoreOne"] = _thisAccount.RookieScoreOne;
newAccount["RookieScoreTwo"] = _thisAccount.RookieScoreTwo;
newAccount["RookieScoreThree"] = _thisAccount.RookieScoreThree;
//Amature
newAccount["AmateurRank"] = _thisAccount.AmatureRank;
newAccount["AmateurAverage"] = _thisAccount.AmatureAverage;
newAccount["AmateurScoreOne"] = _thisAccount.AmatureScoreOne;
newAccount["AmateurScoreTwo"] = _thisAccount.AmatureScoreTwo;
newAccount["AmateurScoreThree"] = _thisAccount.AmatureScoreThree;
//Semi
newAccount["SemiProRank"] = _thisAccount.SemiProRank;
newAccount["SemiProAverage"] = _thisAccount.SemiAverage;
newAccount["SemiProScoreOne"] = _thisAccount.SemiProScoreOne;
newAccount["SemiProScoreTwo"] = _thisAccount.SemiProScoreTwo;
newAccount["SemiProScoreThree"] = _thisAccount.SemiProScoreThree;
//Pro
newAccount["ProRank"] = _thisAccount.ProRank;
newAccount["ProAverage"] = _thisAccount.ProAverage;
newAccount["ProScoreOne"] = _thisAccount.ProScoreOne;
newAccount["ProScoreTwo"] = _thisAccount.ProScoreTwo;
newAccount["ProScoreThree"] = _thisAccount.ProScoreThree;
//Trick Logic]
Dictionary<string, bool> newTricks =
new Dictionary<string, bool>();
int i = 0;
while (i < _thisAccount.accTricks.Length)
{
newTricks["Trick" + i] = _thisAccount.accTricks[i]._owned;
i++;
}
newAccount["TrickList"] = newTricks;
Debug.Log("ConstructDB1");
account.Add(newAccount);
MutableData.Value = account;
Debug.Log("ConstructDB2");
return TransactionResult.Success(MutableData);
});
}
`
Unfortunately, this is a known issue in 6.6.0. If you pay attention to the release page, I'd recommend upgrading as soon as possible.
Now an explanation of what's going on with a small workaround: an array is being serialized as effectively a dictionary with a numeric key. It's parsing numbers lexicographically (ex: it's doing some thing like 1, 10, 2, 3, 4, 5, 6...) and it breaks at 11. To work around this, rather than adding a list as you currently are, try serializing a dictionary with lexicographically ordered keys (ex: M001, M002, M003, &c). I know this isn't ideal, but it should unblock you for the time being!

instantiate class objects from array values in AS3

Am trying to create new instances of classes in a loop where the class instance names have been defined in an array. how Do i turn the array values from string to objects? here's what i have written so far
var tiles2:Array = new Array("e1", "e2", "e3", ...);
for (var i = 0; i < tiles2.length; i++){
if (i == 0){
xPos = 18;
}else if (xPos > 0 && xPos < maxGridWidth){
xPos =+ xPos + objWidth + horGap;
//trace(xPos);
}else{
xPos = 18;
yPos =+ yPos + verGap + objHeight;
}
var this[tiles2[i]] = new tiles2[i];
this[tiles2[i]].name = tiles2[i];
this[tiles2[i]].x = xPos;
this[tiles2[i]].y = yPos;
tileArray[i] = this[tiles2[i]];
addChild(this[tiles2[i]]);
}
This is where I have a problem var mc = new tiles2[i];. the desired output am looking is something like
var e1 = new e1;
e1.name = tiles2[i];
e1.x = xPos;
e1.y = yPos;
tileArray[i] = e1;
addChild(e1);
if you have a better procedure for doing this i will be glad if you can show me
I believe you are looking for this...
// If tiles2[i] is a string that is the name of a class
var type:Class = getDefinitionByName(tiles2[i]) as Class;
var thing = new type();
// If tiles2[i] is a string instance name for an existing object:
var thing:DisplayObject = getChildByName(tiles[i]);
OR, perhaps you just need to change the line in question to this:
//var mc = new tiles2[i]; <-- line in question
//take out the var, and instantiate the proper class
this[tiles2[i]] = new e1();

How do you make buttons that have a specific length?

I'm having difficulty using a customlistrenderer class and homescreen class.
Here's my Homescreen class that contains the function generateData(), which is supposed to display a scroller of 22 buttons.
private function generateData(): void{
var list: List = new List();
list.width = 235;
list.height = 380;
list.name = "HomeScreenList";
this.addChild(list);
var rows:int = 6;
var iterator:int = 0;
var num:int = 22;
var counter:int = 0;
var obj:Object;
var heroName:Array = ["ana", "bastion", "d.va", "genji", "hanzo", "junkrat", "lucio",
"mccree", "mei", "mercy", "pharah", "reaper", "reinhardt", "roadhog", "soldier76", "symmetra",
"torbjorn", "tracer", "widowmaker", "winston", "zarya", "zenyatta"];
var temp:Array = []
obj = new Object();
while(iterator < rows){
obj.name1 = heroName[counter];
obj.image1 = myResource.getImage(heroName[counter]);
obj.name2 = heroName[counter+1];
obj.image2 = myResource.getImage(heroName[counter+1]);
obj.name3 = heroName[counter+2];
obj.image3 = myResource.getImage(heroName[counter+2]);
obj.name4 = heroName[counter+3];
obj.image4 = myResource.getImage(heroName[counter+3]);
temp.push(obj);
counter+4;
iterator++;
}
var collection:ListCollection = new ListCollection(temp);
//assign the renderer
list.itemRendererType = CustomMenuListRenderer;
list.dataProvider = collection;
list.addEventListener(Event.CHANGE, onChange);
this.addChild(list);
list.validate();
}
and the CustomMenuListRenderer class, under the initialize() function
super.initialize();
firstButton = new Button();
this.addChild(firstButton);
firstButton.x = 20;
firstButton.y = 100;
firstButton.scale = 0.60;
secondButton = new Button();
secondButton.x = 92;
secondButton.y = 100;
secondButton.scale = 0.60;
thirdButton = new Button();
thirdButton.x = 163;
thirdButton.y = 100;
thirdButton.scale = 0.60;
fourthButton = new Button();
fourthButton.x = 235;
fourthButton.y = 100;
fourthButton.scale = 0.60;
firstButton.addEventListener(Event.TRIGGERED, onTrigger);
secondButton.addEventListener(Event.TRIGGERED, onTrigger);
thirdButton.addEventListener(Event.TRIGGERED, onTrigger);
fourthButton.addEventListener(Event.TRIGGERED, onTrigger);
The problem is since the loop displays the 4 buttons each loop, an error occurs at the 23rd button(since I only specified 22 buttons but I declared 4 buttons on the CustomMenuListRenderer class) in the 6th row.
My question is how do I fix this issue?
First question, why define num manually and not use heroName.lenght? Is more secure
var heroName:Array = ["ana", "bastion", "d.va", "genji", "hanzo", "junkrat", "lucio",
"mccree", "mei", "mercy", "pharah", "reaper", "reinhardt", "roadhog", "soldier76", "symmetra",
"torbjorn", "tracer", "widowmaker", "winston", "zarya", "zenyatta"];
var num:int = heroName.lenght;
To fix your error, you have to check is heroName index exists before to get it.
Something like :
obj = new Object();
while(iterator < rows){
var objCounter:int = 1;
while(objCounter <= 4 )
{
if( counter < heroName.lenght )
{
obj["name"+objCounter] = heroName[counter];
obj["image"+objCounter] = myResource.getImage(heroName[counter]);
}
objCounter++;
counter++;
}
temp.push(obj);
iterator++;
}

as3 how to remove/ move position of graphics from reset array

I am trying to reset a scene an move every thing to its original position the reset function resets the array adds the nape bodies back to the stage and attaches the graphics but the original graphics still are on the stage in whatever position they were in when reset was called
private var brickGraphic:MovieClip = new Brick();
private var brick:Body;
private var brickArray:Array;
private function setUp():void
{
brickArray = new Array ;
for (var i:int = 0; i < 10; i++)
{
var brick:Body = new Body(BodyType.DYNAMIC);
var brickShape:Polygon = new Polygon(Polygon.box(10,25));
var brickGraphic = new Brick();
brickGraphic.width = 10;
brickGraphic.height = 25;
addChild(brickGraphic);
brickGraphic.cacheAsBitmap = true;
brick.shapes.add(brickShape);
brick.position.setxy(450, ((ag ) - 30 * (i + 0.5)));
brick.angularVel = 0;
brick.shapes.at(0).material.elasticity = .5;
brick.shapes.at(0).material.density = 150;
brick.cbTypes.add(brickType);
brick.space = space;
brickGraphic.stop();
brick.userData.sprite = brickGraphic;
brick.userData.sprite.x = brick.position.x;
this.brickArray.push(brick);
}
private function reset():void
{
if (contains(brickGraphic)) removeChild(brickGraphic);
space.clear();
setUp();
}
}
this is the final issue i am having on this app and your help would be greatly appreciated
That's because you are not removing them with removeChild.
You need to call removeChild for each brickGraphic object you add to the stage.
Something like :
private function setUp():void
{
brickArray = [];
for (var i:int = 0; i < 10; i++)
{
var brick:Body = new Body(BodyType.DYNAMIC);
var brickShape:Polygon = new Polygon(Polygon.box(10,25));
var brickGraphic = new Brick();
brickGraphic.width = 10;
brickGraphic.height = 25;
addChild(brickGraphic);
brickGraphic.cacheAsBitmap = true;
brick.shapes.add(brickShape);
brick.position.setxy(450, ((ag ) - 30 * (i + 0.5)));
brick.angularVel = 0;
brick.shapes.at(0).material.elasticity = .5;
brick.shapes.at(0).material.density = 150;
brick.cbTypes.add(brickType);
brick.space = space;
brickGraphic.stop();
brick.userData.sprite = brickGraphic;
brick.userData.sprite.x = brick.position.x;
this.brickArray.push(brick);
}
}
private function removeAllBricks():void
{
for(var i:int=0; i<brickArray.length; i++)
{
var dp:DisplayObject = brickArray[i].userData.sprite as DisplayObject;
if(dp && dp.parent)
dp.parent.removeChild(dp);
}
}
private function reset():void
{
removeAllBricks();
space.clear();
setUp();
}

as3 how to remove graphics from removed nape bodies

i have attached multiple instances of a moviecCip to multiple nape bodies and have a reset button to restore them to their original position. when the reset function is called the bodies are reset and have the mc attached to them. the problem is the original mc are still on the stage frozen in the position thy were when reset was called.
private var brickGraphic:MovieClip = new Brick();
private var brickArray:Array;
private function setUp():void
{
var brickType:CbType = new CbType();
var w:int = stage.stageWidth;
var h:int = stage.stageHeight;
var ag:int = stage.stageHeight - 58;// height ofarea above ground
brickArray = new Array ;
//wall
for (var i:int = 0; i < 10; i++)
{
var brick:Body = new Body(BodyType.DYNAMIC);
var brickShape:Polygon = new Polygon(Polygon.box(10,25));
var brickGraphic:MovieClip = new Brick();
brickGraphic.width = 10;
brickGraphic.height = 25;
addChild(brickGraphic);
brickGraphic.cacheAsBitmap = true;
brick.shapes.add(brickShape);
brick.position.setxy(450, ((ag ) - 30 * (i + 0.5)));
brick.angularVel = 0;
brick.shapes.at(0).material.elasticity = .5;
brick.shapes.at(0).material.density = 150;
brick.cbTypes.add(brickType);
brick.space = space;
brickGraphic.stop();
brick.userData.sprite = brickGraphic;
brick.userData.sprite.x = brick.position.x;
brick.userData.sprite.y = brick.position.y;
this.brickArray.push(brick);
}
}
private function reset():void
{
space.clear();
setUp();
}
any help would be greatly appreciated
Add a statement that will remove that linked MovieClip from those nape bodies into your reset() function. I expect this is what you need:
private function reset():void
{
if (contains(brick.userData.sprite)) removeChild(brick.userData.sprite);
space.clear();
setUp();
}

Resources