I have an empty array collecting a hex value thats randomly selected from another array within a function. i am trying to pull the hex value from the array that gets filled and pass it to another function to randomly change the color value of a particle system...
private var ca:Array = new Array();
private var rc:String = ca; // pseudo...this is the string that needs to get passed
public function addCursor(cursor:Cursor):void {
var cc:Array = new Array("0xFFFF33", "0xFFFFFF", "0x79DCF4", "0xFF3333", "0xFFCC33", "0x99CC33");
var rcc:String = cc[Math.floor(Math.random() * (cc.length))];
ca.push(rcc); //
trace(rcc + ' 1st array');
trace(ca + ' 2nd array');
trace(rc + ' string to pass');
// unrelated stuff happens down here...
the 1st and 2nd arrays both trace the same hex value, but i cant find the right way to capture that string...ive tried several different methods that all return 'null' ...which makes me think maybe the value is leaving the array before i try to snag it?
i removed ca.pop() which is called in a later function, just to see if rc would still return a null and it does.
rcc is a string, not an array.
So
trace(rcc + ' chosen hex as a string');
But I don't see why you need an array (ca) just to hold a single string.
This would work:
private var rc:String;
private var cc:Array = new Array("0xFFFF33", "0xFFFFFF", "0x79DCF4", "0xFF3333", "0xFFCC33", "0x99CC33");
public function addCursor(cursor:Cursor):void {
rc = cc[Math.floor(Math.random() * (cc.length))];
trace(rc + ' the chosen hex');
}
Related
I am trying to get mutableList of mutableList of LatLng pairs in kotlin. Below is my code.
When the function onPauseButtonClicked()is called I get the value in locationlist variable which is mutableList of LatLng pairs.But I am not able to add the value to locationlists variable which is a mutableListOf(locationList) . The value is shown empty. What is wrong with code.
private var locationList = mutableListOf<LatLng>()
private var locationlists = mutableListOf(locationList)
/**
This is function where locationlists is called
*/
private fun onPauseButtonClicked(){
locationlists.add(locationList)
// Toast.makeText(requireContext() ,"$locationList", Toast.LENGTH_SHORT).show()
val c= locationlists[0]
Toast.makeText(requireContext() ,"$c", Toast.LENGTH_SHORT).show()
}
add will add the MutableList to the end of the list. However, when you call mutableListOf(locationList) you already put an empty MutableList as the first element. It's easy to verify in the debugger, that your locationlists will have two elements, eventhough you only call add once.
Therefore, the correct way would be to initialize locationlists with an empty list of MutableList
var locationlists = mutableListOf<MutableList<LatLng>>()
While initializing use
private val locationLists = MutableList<MutableList<LatLang>>(0){mutableListOf()}
I'm trying to loop through the top title row of my spreadsheet to find the index number of a column based on the title name so that if someone inserts a column my code won't break. Here's what I have so far:
var sheet = SpreadsheetApp.getActive().getSheetByName('RawData');
var values = sheet.getRange(1,1,sheet.getMaxRows(),sheet.getMaxColumns()).getValue(); //line that's breaking
range.setNote("inside function");
var i = 1;
while(values[1][i] != name) {
i++;
}return i;
}
The code appears to break on the line where I set 'values,' and I can't figure out how to access the array I created in order to check which one contains the same string as the 'name' parameter. The setNote line is just for testing purposes, you can ignore it.
Thanks in advance.
EDIT
function getColumnByName() {
var name = "Ready For Testing?";
var ss=SpreadsheetApp.getActive().getSheetByName('RawData');
var vA=ss.getRange(1,1,ss.getLastRow(),ss.getLastColumn()).getValues();
var hA=vA.shift();//returns header array vA still contains the data
var idx={};
var index = hA.forEach(function(name,i){idx[name]=i});//idx['header name'] returns index
return index;
}
I set name just for testing purposes, it will be passed as a parameter when I use the actual function
Try
function getColumnByName() {
var name = "Ready For Testing?";
var ss=SpreadsheetApp.getActive().getSheetByName('RawData');
var vA=ss.getDataRange().getValues();
var hA=vA.shift();//returns header array vA still contains the data
var idx= hA.indexOf(name);
if(idx === -1 ) throw new Error('Name ' + name + ' was not found');
return idx + 1; // Returns the name position
}
Instead of
var values = sheet.getRange(1,1,sheet.getMaxRows(),sheet.getMaxColumns()).getValue();
and instead of
var vA=ss.getRange(1,1,ss.getLastRow(),ss.getLastColumn()).getValues();
use
var values = sheet.getDataRange().getValues();
getValue() only returns the value of top-left cell
using sheet.getMaxRows() / sheet.getMaxColumns() returns the sheet last row / column which could cause getting a lot of blank rows / columns.
compared with the second line code (getLastRow() / getLastColumn()) the proposed code line is "cheaper" (one call to the Spreadsheet Service instead of three (1. getRange, 2. getLastRow, 3 getLastColumn Vs. getDataRange) to get the data range and usually is faster too.
Regarding
var index = hA.forEach(function(name,i){idx[name]=i});
forEach returns undefined ref. Array.prototype.forEach
Try this:
function myFunction() {
var ss=SpreadsheetApp.getActive()
var sh=ss.getSheetByName('RawData');
var vA=sh.getRange(1,1,sh.getLastRow(),sh.getLastColumn()).getValues();
var hA=vA.shift();//returns header array vA still contains the data
var idx={};
hA.forEach(function(h,i){idx[h]=i});//idx['header name'] returns index
var end="is near";//set a break point here and run it down to here in the debugger and you can see hA and idx
}
hA is the header name array
idx is an object that returns the index for a given header name
idx is all you need just learn to put these three lines of code in your script in the future and you won't need to use an external function call to get the index.
var hA=vA.shift();//returns header array vA still contains the data
var idx={};
hA.forEach(function(h,i){idx[h]=i});//idx['header name'] returns index
vA still contains all of your data
As the title says. I tried it with a String or a normal Array and it works. But when I try to pass on my 2D Array my class won't get anything. We're talking about an Array 16 width and about 50 in length.
In my XMLLoader.as class I have this:
function getConvoArray():Array
{
trace("convoArray send");
return convoArray;
}
And in my DialogueGenerator.as class I have this:
xmlLoader = new XMLLoader("ConvoLines.xml");
convoArray = xmlLoader.getConvoArray();
I've checked if the variable convoArray is filled in the XMLLoader.as class by tracing it in a for loop; it works perfectly. But then, when I try to pass it on to the DialogueGenerator.as class it seems to be empty. I cannot excess anything and Flash doesn't give me an error or a warning.
I simply have my Array in DialogueGenerator declared as this:
public var convoArray:Array;
But I tried different ways of declaring it.
Is there a solution for this? A workaround?
For loading xml I use something like this....
var fileName:String = "ConvoLines.xml";
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, LoaderComplete);
loader.addEventListener(IOErrorEvent.IO_ERROR, LoaderError);
loader.load(new URLRequest(fileName + "?rnd=" + Math.random()));
// we affix rand to prevent it from caching the file,
// you don't have to add the ? variable if you aren't worried about it
// updating smoothly
Then in the "LoaderComplete" function we just get the xml out. Hope that helps.
var convoXML:XML = new XML(event.target.data);
I have generated a number and sent it to a text box. Now I want to save that number for later use. Arrays seem (to my very limited knowledge) to be the place to keep data you wish to access later on. How do I do this?
my code so far:
roll_btn.addEventListener (MouseEvent.CLICK, rollDice);
function rollDice (Event:MouseEvent):void{
var die1:uint = Math.floor (Math.random()* 6) + 1;
var die2:uint = Math.floor (Math.random()* 6) + 1;
die1_txt.text = die1.toString();
die2_txt.text = die2.toString();
sec1_txt.text = (die1 + die2).toString();
}
I want to add the result in "sec1_txt.text" to an array.
Thanks in advance.
Use this:
var myArray:Array = new Array();
myArray.push(die1 + die2)
You can also use a Vector that is a typized (of uint) array.
I am writing a childrens game and it consists of letter blocks.
The child has to put the blocks in a correct order (following silhouettes) to spell the word
Now my question is I have 2 Arrays.
var myLetters = new Array(
new BlockC(),
new BlockA(),
new BlockB()
);
public static var myLetters2 = new Array(
new BlockC(),
new BlockA(),
new BlockB()
);
So you see this is setup to spell the word C A B.
What i would would like to do is have a string variable that i can put the word in to and then have code fill the array in the correct order.
i.e.
var word:String = "CAB";
Hope this makes sense and i can get some good help from you guys
Thanks
If I understand the question correctly, here is one way of doing it :
var word:String = "CAB";
var letterClassMapping:Object = {
"C":BlockC,
"A":BlockA,
"B":BlockB
};
var myLetters:Array = [];
for(var i:int=0; i<word.length; i++) {
myLetters.push( new letterClassMapping[word.charAt(i)]() );
}
Another way is to use getDefinitionByName to get the class type :
var classType:Class = getDefinitionByName("Block" + word.charAt(i)) as Class;
myLetters.push(new classType());
You seem to need a letter collection with corresponding classes. So, you make yourself an Object of the following srtructure:
private static var LETTERS:Object={A:BlockA,B:BlockB,C:BlockC};
Then you split your word by single letters (copy one letter out of a word into a new string) and then you can get corresponding class via LETTERS[letter], and a new instance of that class via new LETTERS[letter]();
You can also create toString() functions in Your object and thant join array .
In class create function :
public class BlockA {
public function toString():String {
return "A";
}
}
And than You can join array items :
var arr:Array = [new BlockA , new BlockB , new BlockC];
trace(arr.join(""));
// and compare to Your string:
arr.join("") == word;