Using variable A to control variable B - arrays

I have a function called FarmClick, and I want it to check the current MovieClip's array. For example:
var farmSpot1:("id",int,int);
var farmSpot2...
I need the clickEvent to capture the MovieClips name and check it's array for the 0th element to see whether it's empty, or not...
My code so far:
public function FarmClick(event: MouseEvent): void {
var CurrentSlot = event.currentTarget.name
if ([event.currentTarget.name[0]] = "empty") {
stage.addChild(menu);
menu.x = 400;
menu.y = 90;
this.menu.buyCornBtn2.addEventListener(MouseEvent.CLICK,buyCorn2);
} else if (farmEmpty == true && itemSelected != "none") {
selected();
} else if (farmEmpty == false) {
farmHarvest();
}
}

It took me a minute to figure out just exactly what it was that you're asking.
So basically, your movie clip's name would be farmSpot[someNumber] and you have an array with the same name?
in that case, to reference the array variable, you need to use the this keyword.
you can add any array subscripts after you've referenced it like so:
private var farmSpot1:Array = ["id", int, int];
public function FarmClick(event:MouseEvent): void
{
if (this[event.currentTarget.name][0] == "") // wasnt sure if you meant the string "empty" or just an empty string, so i just put an empty string
{
// do whatevs
}
}

Related

Flutter : How to get the value of the other properties from a List?

All of my Json data is already inside in my model. And I have also a list of data inside "selectedBranches[]". What do I want is to get the other value of the other properties which is the BranchCode and BranchId based on the value from the selectedBranches list and put it to the another list.
My selectedBranches List is consist of list of branchFullName properties from my json data.
But dart says The argument type 'List' can't be assigned to the parameter type 'Pattern'.
#Code Snippet#
selectedBranches = [ex1,ex2,ex3,ex4];
selectedAssignBranches = [];
assignBranch() {
branchModel1 = GetBranchList.fromJson(jsonData1);
var newArr = branchModel1.data;
for (var u in newArr) {
AssignBranch assignModel = new AssignBranch();
if (u.branchFullName.contains(selectedBranches/*error*/)) {
assignModel.BranchCode = u.branchCode;
assignModel.BranchID = u.brID;
selectedAssignBranches.add(assignModel);
}
}
}
#Model#
class Datum {
String branchCode;
String branchName;
String stat;
String brID;
String branchFullName;
I already solve the problem. You just need to have a nested for loop. Thats it!
assignBranch() {
branchModel1 = GetBranchList.fromJson(jsonData1);
var newArr = branchModel1.data;
for (var i in selectedBranches) {
for (var u in newArr) {
if (u.branchFullName == (i)) {
AssignBranch assignModel = new AssignBranch();
assignModel.BranchCode = u.branchCode;
assignModel.BranchID = u.brID;
selectedAssignBranches.add(assignModel);
}
}
}
}

2sxc: foreach how to identify first element

In a foreach loop, how do I identify the first element? For example, in the snippet below, can I identify the Content item that is the first (or last) item?
#foreach(var Content in AsDynamic(Data["Default"]))
{
??
}
You can do something like this:
#{int i = 1;}
#foreach(var Content in AsDynamic(Data["Default"]))
{
if(i==1)
{
//this is first element
}
i++;
}
Another option is to compare the current item to the First() or Last(). But you may run into some surprises because of the Dynamic wrapper caused by AsDynamic. So my pseudocode recommendation is a bit like this
var firstI = Data["Default"].FirstOrDefault(); // returns null if nothing
var lastI = Data["Default"].LastOrDefault(); // null...
#foreach(var Content in AsDynamic(Data["Default"]))
{
var isFirst = AsEntity(Content) == firstI; // unwrap and compare
//etc.
}

How can I move back to Element 0 in an array to reset a loop?

I have an array that loops through a bunch of images to make an animation but will change to another array animation once a condition is met. After that array is over, it does return to the first. However, it picks up where it left off instead of resetting. I need it to return at Element 0 each time. Any ideas?
private var csScript : Main;
var textures : Texture[];
var changeInterval : float = 0.33;
var textures2 : Texture[];
var changeMe : boolean = false;
function Awake() {
//Get the CSharp Script
csScript = this.GetComponent("Main");
//Don't forget to place the 'CSharp1' file inside the 'Standard Assets' folder
}
function Update() {
if( textures.length == 0 ) // nothing if no textures
return;
// we want this texture index now
var index : int = Time.time / changeInterval;
var index2 : int = Time.time / changeInterval;
// take a modulo with size so that animation repeats
if(csScript.janneleIsWaiting) {
index = index % textures.length;
// assign it
renderer.material.mainTexture = textures[index];
}
if(csScript.janelleWorried) {
print("so so worried");
index2 = index2 % textures2.length;
// assign it
renderer.material.mainTexture = textures2[index2];
reset();
}
}
function reset() {
yield WaitForSeconds(.35);
csScript.janneleIsWaiting = true;
csScript.janelleWorried = false;
csScript.janelleHappy = false;
}

Array getting Keyboard Input

This code is from a tutorial, I do understand it - just not the array. I know arrays hold values in their indexes (array[1]=something, array[2]=something...)
But how does the array "keys" have the Keyboard.RIGHT value in the update function?
package
{
-imports here-
public class Main extends Sprite
{
var keys:Array = [];
var ball:Sprite = new Sprite();
public function Main():void
{
ball.graphics.beginFill(0x000000);
ball.graphics.drawCircle(0, 0, 50);
ball.graphics.endFill;
addChild(ball);
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight / 2;
ball.addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
private function update(e:Event):void {
if (keys[Keyboard.RIGHT]) {
ball.x += 5;
}
}
function onKeyDown(e:KeyboardEvent):void {
keys[e.keyCode] = true;
}
function onKeyUp(e:KeyboardEvent):void {
keys[e.keyCode] = false;
}
}
}
(Because, to give a value to a variable in Pascal for an example, I'd have to write readln (array[1]) - this would give whatever value the user typed to array[1]).
So, I don't see how keys[] is getting keyboard input :P
e.keyCode is an integer. When the onKeyDown gets called, the code uses the integer to set a value in the array to true.
For example, if the RIGHT key is pressed on the keyboard, e.keyCode would be 39, so the code is the same as keys[39] = true;.
If you look at the documentation for Keyboard you will see that Keyboard.RIGHT is defined as 39. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/ui/Keyboard.html
On the next frame the update event fires.
When you write an if statement without an ==, it kinda does it for, so the if statement is essentially saying
if(keys[39] == true) {
ball.x += 5;
}

Registrer in array and new variable

I am trying to make a program where you can register expenses on someone.
i have 2 TextInputs, named "txt1" and "txt2"
I want to make an eventlistener where
If you put in a new name in “txt1”, it will be registered in an array, and a new variable will be created, and the number in “txt2” will be added to that variable.
If you put in a name that’s already in the array, the number in “txt2” will be added the variable which was created when you typed in the name the first time.
Here's what i got so far
var names:Array = new Array();
stage.addEventListener(KeyboardEvent.KEY_DOWN, regi)
function regi(evt)
{
if (evt.keyCode == 13)
{
var k:String = txt1.text
if (names.indexOf(k) != -1)
{
txt1.text+txt2.text
}
else
{
names[names.length] = k
var txt1.text = txt2.text
}
}
}
you can use Dictionary to do that ex:
var names:Dictionary = new Dictionary();
stage.addEventListener(KeyboardEvent.KEY_DOWN, regi);
function regi( evt:KeyboardEvent ):void
{
if (evt.keyCode == 13)
{
if( names[txt1.text] ) names[txt1.text] += txt2.text;
else names[txt1.text] = txt2.text;
}
}

Resources