Efficient way to work with multiple arrays in AS3 - actionscript-3

I'm creating a puzzle game in Flash/AS3. It uses about 30 to 40 groups of words. What I want to do is to load one random group of words in the beginning of the game and after using them, I want to load another random group and so on. What is the best way to solve this? Should I have multiple arrays which I will select from using switch statement based on random number or should I put them all into one multidimensional array? Every group of words could contain up to 2000 words.

It's better you use xml for creating wordgroups and to pull them easily. So, i mean, it's efficient you use multidimensional arrays.
var wordgroups:Array = new Array();
wordgroups.push(new Array("word1", "word3", "word5"), new Array("word2", "word4", "word6")); //you can easily create whole this multidimensional array with xml
wordgroups.shuffle(); //shuffles array (flashpunk library have this function, you may check it out and clone it's function if you can)
var selectedwordgroup:Array;
selectedwordgroup = wordgroups.pop(); //you can easily take a word group from that array and it removes selected wordgroup from the "wordgroups" array automatically with pop() function.
What about XML stuff
Thats a sample *.xml file:
<?xml version="1.0" encoding="utf-8"?>
<wordstuff>
<wordgroups>
<group>word1,word2,word3,word4,word5,word6</group>
<group>word7,word8,word9,word10,word11,word12</group>
<group>word13,word14,word15,word16,word17,word18</group>
</wordgroups>
</wordstuff>
and this is a sample to pull data from xml (you're going to embed your xml stuff to your game, so, you don't need any url loader object, you can embed it like you're embedding image files into your game, with a little difference)
then you can create arrays with this embedded xml
var pulledgroups:Array = new Array();
var my_xml:XML = WORDSTUFF //that's embedded file
var wordgroups:XMLList = my_xml.wordgroups.group;
var group:XML;
for each (group in wordgroups)
{
var pulledwordgroup:Array = group.split(",");
pulledgroups.push(pulledwordgroup);
}
THAT'S ALL, this single code paragraph, pulls all data at the same time

Related

How to display a 2-D array as an html table for a Google Web App Page (using GAS)

Using a code.gs file, I have retrieved data from a Google Spreadsheet and put it into a 2-D array, and then manipulated the array a little bit (with things like transpose, condensing it, etc.).
I am trying to find out how to create a web app / web page that will display this 2-D array as an html table (subject to also being able to set table specifications, like width, color, etc.).
Does anyone have any ideas on how this can be done?
This will tell you how to do it, generally speaking. Just to display the HTML.
https://developers.google.com/apps-script/guides/html/
To choose specific data, you will want to use the .gs file to grab the spread sheet as you have already done. Now, when you're done modifying your array, store it in Properties Service, or Cache Service.
In your HTML page, write a JS function that calls back to the .gs file.
google.script.run
.withSuccessHandeler( function(array) { ... //We'll get to this in a moment... })
.getMyArray();
In your .gs file, write a function 'getMyArray'. Make it retrieve the array from Property or Cache Service and make it return the array.
Now, it the Success Handeler, use your array, and make it a table.
// 'array' here will be the return of .getMyArray()
.withSuccessHandler( function(array) {
for (var i in array) {
// Here, make every object into a string of the table you want
// i.e. "<table><td>" + array[i] + "<td> ..."
// Insert that string into some div.innerHTML
}
.getMyArray();
Easy as pi

SAPUI5 copy model and break binding?

On initialization I read an oData service to get a small list of values and I store the model for further use in the application.
sap.ui.getCore().setModel(oODataJSONModel, "xlist");
At multiple stages, I want to make a copy of the original model, make changes to the values list and use it in a Select drop down. I've tried multiple different things, but every time I update/delete the copied model values, it is instantly reflected in the original model. This seems like a simple ask, but is there a way to break the link between the original model and the copied model, ideally I want to keep the original list intact so that list can be re-used over and over, regardless of what changes are made to the copies?
var oModelCpy = new sap.ui.model.json.JSONModel();
var cpyModelArray = oOrigModel.getData();
cpyModelJsonData = { results : [ cpyModelArray ] };
oModelCpy.setData(cpyModelJsonData );
When I remove entries from the copy model, it also removes entries from the original model, which in this case is not what i want.
Any suggestions?
A better approach is to save your data in the success handler:
oODataJSONModel.read("/yourService",
null,
null,
false,
function(oData, oResponse){
var oODataJSONModel = new sap.ui.model.json.JSONModel();
oODataJSONModel.setData(oData);
this.getView().setModel(oODataJSONModel, "jsonModel");
}
);
EDIT
I just stumbled upon this question while I was browsing through the list of UI5 questions, and it dawned to me what is causing your underlying copy issue! :-)
If you copy an array of objects to a new array (which is also happens if you copy model data to another model), you won't get a new array with new objects
Instead, you actually will get a new array, but with references to the old objects. So any change you make to a value in an object inside an array in model 1, will end up having that same value in model 2
So, in effect, you need to create new objects based on the old ones. Luckily, you don't need costly for loops and hardcoded value-copying logic to achieve this; one single line should be ok.
Let's say your original data is referenced by an array aData.
You then copy this data (a true copy) to a new array using JSON:
var aDataCopy = JSON.parse(JSON.stringify(aData));
If you now set this aDataCopy as the data for your second model, it will not have any references to the old model anymore.
Hope this helps!
Try using jquery extend() method to make a copy of the data. I had similar troubles earlier.
var newObject = $.extend({},oldObject);
Try this for once. Find the reference at http://api.jquery.com/jquery.extend/

d3.js CSV element in-line

Folks-
I'm making a viz based on Mike Bostock's http://bost.ocks.org/mike/uberdata/.
As this will be mailed and viewed locally rather than staged on a server it's necessary to embed the JS library, and any data in the document.
I have successfully integrated the javascript, as well as a JSON object. I'm having difficulty getting the CSV file included and then iterating over it.
var myMatrix = '[[0,557.3,0,0,0,1645.8,0,903.2,895.6,615.7,0,408.1,0,845.3,290.1,325,73.5],[788.1,0,0,0,1204.2,1859.7,1020.5,1160,976.4,1057,1082.4,0,697.2,0,614.2,0,0]]';
svg.append("circle")
.attr("r", outerRadius);
d3.csv("companies.csv", function(companies) {
//d3.json("matrix.json", function(matrix) {
matrix = JSON.parse(myMatrix); //new
// Compute the chord layout.
layout.matrix(matrix);
...<snip>
So basically I need to replace the "d3.csv" function with something that will operate on an in-line data set while still iterating over the rest of the function.
Thanks,
RL
Lars- you're right. I was overthinking it.
I just added the JSON object in and the d3 magically iterated over the items in the object.
var companies = [{name:"one", color:"#8a8a8a"},{name:"two", color:"#5d9a0c"},{name:"three", color:"#005a8b"}}];
thanks,
RL

Create Linked List in AS3

how can I create a linked list in actionScript 3.0? I have a project that I should get some integer numbers from the user and sort them by a tree algorithm for example heap-sort and show the tree in flash, I think I should use linked list to sort the data by tree algorithms.
so anybody know how can I create a linked list which I can insert nodes, delete nodes and pass over the nodes, just like C++ linked list.
Thanks.
SA
You can use or take as an exmaple as3Commons linked list implementation. They provide very beautiful implementation with very good abstraction layer.
If you have access to the mx package, you could use mx.utils.LinkedList.
To construct the LinkedList you could repeatedly push or unshift items onto it.
var input:Array = getInput();
var myList:LinkedList = new LinkedList();
for each (var o:Object in input) {
myList.push(o);
}

AS3 Object Filtering

Ok. so I'm working on an app that retrieves items from a db and builds a gallery. I've done this a ton of times, and it should be simple.
I'm running into problems, because in this gallery, I get results from a db that includes both image files, and other files. Let's just say I can't change anything but the flash, so I need to detect if it's an image, and only display it if it is.
My question is: How the hell can I delete a property from an object without the object staying the same size? I use a count() function to generate pagination data, so I can't just 'null' them, and as I understand it, delete() is not an option either.
My solution for this was to just create another object, filter the good items with a for in loop, then pop them in to another object, but each item in the object is an object, and I have no push() function for objects.
So, in desperation, I am using an increment to add the objects to the new object using an index (goodItemsObject[index] = allItemsObject[object]), but that seems like a really gruesome way of getting around this problem.
Here's some code:
var filteredMO = new Object();
var newFile = 0;
for each(var file in mediaObject){
if(check_file(file)){
filteredMO[newFile] = file;
newFile++;
}
}
mediaObject = filteredMO;
check_file() just returns true or false, mediaObject is all full of objects.
I'd much prefer to be doing this:
for each(var file in mediaObject){
if(check_file(file)){
//remove_from_object_for_reals(mediaObject[file]);
}
}
I realize that might not be possible (would it throw off the for loop?), but something similar would be sweet. I'd love to be able to let the gc grab all these useless objects hanging out.
any ideas?
thanks,
Jesse
What you are using to store your object now is called an associative array. You cannot remove a key-value from an associative array. What you need is a dictionary (which as3 has a build-in one http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/Dictionary.html).
import flash.utils.Dictionary;
var dict:Dictionary = new Dictionary();
then you can remove a key from your dictionary by calling
delete dict["myKey"]