Displaying all items in an array in a dynamic textfield [Actionscript 3] - arrays

I have an array of items which I would like to be displayed within a dynamic textfield to form a high score list.
The amount of items within the arraylist with vary depending on how many high scores are added to it. It is created as standard like this:
var lvl1ScoreArray:Array = new Array();
And items are added to it within the following code:
if (currentLevel == 1)
{
lvl1highScores.push({score:int(vinylCollected) , player:String(highScoreInput.text)});
lvl1highScores.sortOn("score", Array.DESCENDING | Array.NUMERIC);
}
I can obviously trace all the items in the array as follows:
for (var i:int = 0; i < lvl1highScores.length; i++)
{
trace(lvl1highScores[i].score, lvl1highScores[i].player);
}
But I would like to do this within a dynamic textfield called highScoreTxt.. Any suggestions?

That is simple, just create a movie clip with a text field in it with an instance name of txt. Name the movie clip HighScoreTF and set its linkage to HighScoreTF Then your for loop will looking something like so
for (var i:int = 0; i < lvl1highScores.length; i++)
{
var tf:HighScoreTF = new HighScoreTF();
tf.txt.text = lvl1highScores[i].score + " - " + lvl1highScores[i].player;
tf.y = i * tf.height; //-- you can replace tf.height with a number to adjust spacing
addChild(tf);
}

Related

Google Script: adding up arrays

I want to write a google spreadsheet with two main parts: an individual order form to calculate price & a system to count the total orders.
I made a simplified version here: https://docs.google.com/spreadsheets/d/1_vRnX-qT3-0puYhtBCclYIVu504A-donE4nU8rQn41k/edit?usp=sharing
After noting an individual order and the order is paid, the green button is clicked. The button activates the payment-script.
The payment script needs to add the values of the individual order to the total orders and afterwards empty the individual orders.
function payment() {
var individualOrder = SpreadsheetApp.getActiveSheet().getRange('E3:E6').getValues();
var currentTotal = SpreadsheetApp.getActiveSheet().getRange('O3:O6').getValues();
var newTotal = [0,0,0,0];
for (var i = 0; i < 4; i++) {
newTotal[i] = currentTotal[i] + individualOrder[i];
}
SpreadsheetApp.getActiveSheet().getRange('O3:O6').setValues([newTotal]);
// Reset individual order after payment
SpreadsheetApp.getActiveSheet().getRange('E3:E6').setValue(0);
}
EDIT: Now it does work to replace the new Total values by using the code below, but there is another problem. When summing the array values 1 + 0, it cocatenates them to 10 instead of using math-values. Any ideas?
function payment() {
var individualOrder = SpreadsheetApp.getActiveSheet().getRange('E3:E6').getValues();
var currentTotal = SpreadsheetApp.getActiveSheet().getRange('O3:O6').getValues();
var newTotal = currentTotal;
Logger.log(individualOrder);
Logger.log(currentTotal);
for (var i = 0; i < 4; i++) {
newTotal[i] = [currentTotal[i] + individualOrder[i]];
}
Logger.log(newTotal);
SpreadsheetApp.getActiveSheet().getRange('O3:O6').setValues(newTotal);
// Reset individual order after payment
SpreadsheetApp.getActiveSheet().getRange('E3:E6').setValue(0);
}
If all you want to do is set 'individualOrder' to 0 try:
function payment() {
var individualOrder = SpreadsheetApp.getActiveSheet().getRange('E3:E6').getValues();
for (var i = 0; i <individualOrder.length ; i++) {
individualOrder[i]=[0];
}
SpreadsheetApp.getActiveSheet().getRange('E3:E6').setValues(individualOrder);
}
I solved it by making the sum in the spreadsheet and doing a .getValues on that range and using this in .setValues. A lot easier, only 2 lines of code.

Add datelist to Array collections C#

I have 30 string[4] elements which contains user data. I have 30 date generic list collection. How to add each date as 5th element in each one string[] collection?
List<string> _datefromexcel = new List<string> ();
foreach(DataColumn c in dtRow.Table.Columns) {
if (Information.IsDate(c.ColumnName)) {
_datefromexcel.Add(c.ColumnName);
}
}
List<string> newlist = lst.GetRange(startrange, count);
int i = 0;
var query = from s in newlist
let num = i++
group s by num / 4 into g
select g.ToList();
var results = query.ToArray();
foreach(var item in _datefromexcel) {
results.insert(5, item);
}
This is insert all date value after 5th element.
Please advice the best way to insert each date value as 5 element in array collection.
For starters, you won't be able to "insert" anything into an array, since it has a static size. You can add things to a list, however, because a list can dynamically resize itself.
var results = query.ToList();
foreach (var item in _datefromexcel) {
results.Add(item.ToString());
}
If you need to, you can call ToArray after the foreach loop is finished:
return results.ToArray();
You can use for loop instead:
var results = query.ToArray();
for (int i = 0; i < results.Length; i++)
results[i].Add(_datefromexcel[i]);
The LINQ way will be a bit ugly:
var results = query.Zip(_datefromexcel, (l, d) => l.Concat(new[] { d }).ToList()).ToArray();

Push instance name of movieClip into array using a for-loop

I have a bunch of movieclips on the stage with instance names ball1 - ball200. I was hoping I didn't have to create an array and manually set all the instance names into the array
ballArray = [ball1, ball2,ball3, etc];
I was trying to get a for loop to cycle through and add each instance name to my array like so:
function createTheArray():void{
for(var i:int = 1; i < 20;i++){
ballArray.push(ball + i);
trace(newArray[i])
}
}
But I keep getting back undefined array index's. It also tells me that I doesn't know what "ball" is. How would you use part of a instance name and combine it with the index value of the loop. So that the first time through you get ball1 as the first index value of your array?
Dragging out 200 balls onto the timeline and giving them instance names doesn't sound like much fun!
BEST OPTION:
right click the ball object and go to the properties, click "export for actionscript" and give it a unique name. (Lets call it MyBall for this example)
in your timeline code do this:
var ballArray:Vector.<MyBall> = new Vector.<MyBall>();
for(var i:int=0;i<200;i++){
ballArray.push(new MyBall());
addChild(ballArray(ballArray.length-1));
}
NEXT BEST OPTION
if all your balls are on the timeline already, you can still do the step from above (export for actionScript and give it a name) but do the following code:
var ballArray:Vector.<MyBall> = new Vector.<MyBall>();
var i:int = numChildren;
while(i--){
if(this.getChildAt(i) is MyBall) ballArray.push(this.getChildAt(i) as MyBall);
}
ANOTHER OPTION
If your balls are not all the same library objects, if you put them all as the only objects in a movie clip container (let's say you gave it the instance name ballContainer, you can still use this code so you don't have to give them instance names:
var ballArray:Vector.<DisplayObject> = new Vector.<DisplayObject>();
var i:int = ballContainer.numChildren;
while(i--){
ballArray.push(ballContainer.getChildAt(i));
}
You can use a string in brackets to get a property of an object. In your case, your object is referred to as this. So your syntax for getting a ball is this["ball"+index].
Try this:
function createTheArray():void{
for(var i:int = 1; i < 20; i++){
ballArray.push(this["ball" + i]);
}
trace(ballArray);
}
Referencing Properties by String isn't really a great practice though. If it's possible to create your balls dynamically as well, that would be a better implementation. You can create a ball MovieClip on your timeline, and select Export For ActionScript in the properties. Then you can use this code to instantiate 20 or more balls:
//add 20 balls to stage
var ballArray:Array = [];
for(var i:int = 0; i < 20; i++){
var ball:Ball = new Ball();
addChild(ball);
ballArray.push(ball);
}
trace(ballArray);

Composite C1 How would I rewrite this Sql update statement to work in c#?

I have a piece of code in an image sortable grid which sends back a resulting string array of integers based on the user's new sort order for 'propid':
{ 'imgid': '4,2,3,5,6,7,8,9,1','propid':'391' }
The above shows 9 images on the screen. The db image table has both an image id (imgid) field and a sort sequence field (orderseq). I am using a custom namespace datatype:
< connection.Get< ALocal.propimage >()
like all datatype connections in C1.
In direct SQL I would write this:
string []q = imgid.Split(',');
string qry="";
for (int i = 0; i < q.Length; i++)
{
qry += "update ALocal_propimage set propimage_orderseq="+(i+1)+" where prop_id="+propid+" and propimage_id="+q[i]+" ;";
}
sqlHelper obj = new sqlHelper();
obj.ExecuteNonQuery(qry);
return "Record Updated";
How does this convert to writing it using c# into Composite's C1 CMS 'Updating Multiple Data' method as I keep failing at it?
The C1 site 'Updating Multiple Data' method rudimentary example is:
using (DataConnection connection = new DataConnection())
{
var myUsers = connection.Get<Demo.Users>().Where (d => d.Number < 10).ToList();
foreach (Demo.Users myUser in myUsers)
{
myUser.Number += 10;
}
connection.Update<Demo.Users>(myUsers);
}
Any help would be really appreciated.
You would need to split your update code into a get and a update, to let C1 know exactly which entity you would like to update. So something like this
for (int i = 0; i < q.Length; i++)
{
var propimages = connection.Get<ALocal.propimage>().Where(o => o.PropId = propid && p.PropImageId = q[i]);
foreach (var o in propimages)
{
o.OrderSeq = i + 1;
}
connection.Update(propimages);
}

AS3: Sort array descending while keeping strings in correct order

I'm not sure whether there is a simply answer to this but I am assuming there isn't, hence I'm here.
Basically, I want to run a very simple high score table which keeps track of the high scores of a game, but also displays the correct names beside each scores.
This is all easy but I want to be able to do this with just the one array.
For example, I have this code:
var d:Array;
var e:Array;
d = "827-Harry".split("-");
d.push("918-John".split("-"));
trace(d)
Which correctly results this trace:
827,Harry,918,John
My question is, how can I use Array.sort() (or similar) in such a way that the following is produced:
d = 918, John, 827, Harry
It can't be specific to this example. That is, it needs to work with custom names and dynamic scores.
Cheers in advance!
Harry.
create an associative array and use sortOn():
var highscores:Array = new Array();
highscores.push({score: 827, player: "John"});
highscores.push({score: 918, player: "Harry"});
highscores.sortOn("score", Array.DESCENDING | Array.NUMERIC);
for (var i:int = 0; i < highscores.length; i++)
{
trace(highscores[i].score, highscores[i].player);
}
Wouldn't recommend you store your high scores like that but I don't know your reasons so here's exactly how to do what you want.
public function sortArray(arrUnsorted:Array):Array
{
var arrLocal:Array = new Array();
var arrSorted:Array = new Array();
for (var i:int = 0; i < arrUnsorted.length; i += 2)
{
arrLocal.push( { score:int(arrUnsorted[i]), name:arrUnsorted[i + 1] } );
}
arrLocal.sortOn("score", Array.DESCENDING | Array.NUMERIC)
for each(var obj:Object in arrLocal)
{
arrSorted.push(String(obj.score), obj.name);
}
return arrSorted;
}
Then it's as simple as:
var arrUnsorted:Array = ["827", "Harry", "918", "John"];
var arrSorted:Array = sortArray(arrUnsorted);
trace(arrSorted); // 918,John,827,Harry
Hope this is what you're after.

Resources