Actionscript 3: each line from text file as a element in array - arrays

So my problem is that trace within the function does trace the first element of the array, but the trace outside if the function does not. I do declare the array variable outside the function, but the data wont save to the array variabel.
var oppgaveLoader:URLLoader = new URLLoader();
oppgaveLoader.load(new URLRequest("oppgaver.txt"));
var oppgaveNr = 0
//store line of text on an array called oppgaver
var oppgaver:Array = []
var oppg:Array = new Array()
oppgaveLoader.addEventListener(Event.COMPLETE, onLoaded);
function onLoaded(e:Event){
oppgaver = e.target.data.split(/\n/)
trace(oppgaver[0]) //This one traces the frist item in the array
}
trace(oppgaver[0])//This one does not trace the first one in the array
Does anyone know why and/or how to fix it if posible?
The file "oppgaver.txt" is located in the same directory as my .fla file
The file "oppgaver.txt" is laid out like this (the text is in norwegian, but each line is going to be a item in the array):
Hvor gjelder forbudsskilt hvis ikke annet er oppgitt?
Hvordan foretar du best mulig bremsing og unnastyring?
Hvordan bør du normalt plassere bilen på en vanlig 2-felst vei?

It's a synchronicity problem.
the last trace happens immediately after you set up your arrays, but those arrays are still empty.
only when the onLoaded function gets called, asynchronously by the URLLoader, they get populated and you can trace their values.
that event listener basically lets you react to an event that happens in the future at some point.

Related

How to store data to an array inside foreach loop in Laravel?

I have a collection of $linesheetItems, now I need to loop these $linesheetItems inside a foreach loop and store a seasons array by using line sheet item's season code ($linesheetItem['season']). But according to my current code, it returns an empty array.
Code:
$seasons = [];
foreach($linesheetItems as $linesheetItem) {
$seasons = Season::where('code', $linesheetItem['season'])->get();
}
dd($seasons);
How to achieve this, and what are the modifications should I do to my code?
In your code, you are overriding the $seasons variable each time the loop runs. In order to add an item to an array you have to set $seasons[] = Season::where('code', $linesheetItem['season'])->get();. This will always push a new item into the array. If you want to have custom keys on the array, you can do $seasons['your-key'] = Season::where('code', $linesheetItem['season'])->get();

Remove random item from array and push to new array

Thank you in advance! I'm new to coding and learning how to deal with Arrays. I am trying to remove random items from an array (deck of cards), and populate a new array (called hand). The problem I always seem to have with arrays is taking the results of one and creating a new function/array/ etc.. Currently, I am outputting 2 separate arrays and I can't seem to push them into one.
let deck = ["dA","dQ","dK","dJ","d10","d09","d08",
"d07","d06","d05","d04","d03","d02","hA","hQ","hK",
"hJ","h10","h09","h08","h07","h06","h05","h04","h03"];
var hand = deck.splice(Math.floor(Math.random()*deck.length),1);
console.log(hand)
var hand = deck.splice(Math.floor(Math.random()*deck.length),1);
console.log(hand);
In your code, just you need to push the value returned from splice method rather than directly assigning it.
By this way every time a new value that gets deleted and will be added to the new array called hand. Hope this helps. :-)
let deck = ["dA","dQ","dK","dJ","d10","d09","d08",
"d07","d06","d05","d04","d03","d02","hA","hQ","hK",
"hJ","h10","h09","h08","h07","h06","h05","h04","h03"];
var hand = [];
const getSelectedCard = () => deck.splice(Math.floor(Math.random()*deck.length),1)
let selectedCard = getSelectedCard();
hand.push(...selectedCard) //or hand.push(selectedCard[0])
console.log(hand);
selectedCard = getSelectedCard();
hand.push(...selectedCard)
console.log(hand);

actionscript 3: how to access to elements of an array created in a loop dynamically

In the library of the .fla file I have a square exported as Class "cuad" on frame 1
I want to create an Array with 100 squares so as to move them later
So I do like this:
for (var i:uint = 0; i<100;i++)
{
var cuad_mc = new cuad();
addChild(cuad_mc);
myArray.push("cuad_mc");
trace(myArray[i]);
}
I have a runtime error
The error you experience is
Error #1069: Did not find alpha propiety in the String and there is not any value predetermined
The problem comes from your line
myArray.push("cuad_mc");
What you are doing here is pushing a String Object into your Array, not the cuad Object you want. String Objects don't have Alpha values, or x values.
What you want to do is
myArray.push(cuad_mc);
cuad_mc (without the " quotation marks) is a reference to the object you just created.
This should solve your problem. I also recommend using Vectors instead of Array if you only need to store one type of Object. Like this:
var myArray:Vector<cuad> = new Vector<cuad>();
for(var i:int=0;i<100;i++){
var cuad_mc:cuad = new cuad();
addChild(cuad_mc);
myArray.push(cuad_mc);
trace(myArray[i]);
}
Vectors are just like Arrays, but they only allow one specific type, so that a situation like yours doesn't occur.

using a variable name to load movie clip to stage using As3

I have created a Spelling Puzzle in AS3 that loads a list of words from an XML file into an Array.
As the code loops through the array, it assigns each word to a variable called "current_word", then scrambles the letters of "current_word" and displays them on stage.
I would like to add an animated MovieClip as a visual aid with the class name that matches the value of current word.
For example if the current word is 'bear', then a MovieClip with the class name 'bear' is loaded from library to stage.
I was trying to create an empty movie clip called "tempItemClip" and overwrite its value with the value of the var current_word.
No errors, but it's not working. I am new to ActionScript. Can someone advise me on the best solution?
public function getWord()
{
current_word=pWord[ques_num];
setTiles(current_word.length);
ques_num++;
trace(current_word);
var tempItemClip:MovieClip = new MyItem();
Puzzle_screen.addChildAt(tempItemClip,4);
tempItemClip.x=380;
tempItemClip.y=130;
//var myClip:Object = getDefinitionByName(current_word);
var myClip:MovieClip = new MovieClip();
tempItemClip[current_word] = myClip;
tempItemClip.addChild(myClip);
}
It's not clear what you are asking, but I believe this is what you want :
var clipClass:Class = getDefinitionByName(current_word) as Class;
var myClip:MovieClip = new clipClass();

ActionScript: How to push to multidimensional arrays and later retrieve just one 'row'

I am reading a set of latitude & Longitude Coordinates that define a polygone area. They are keyed to an area ID and I retrieve them from a SQL database. So for example, Area ID 153 might have 20 coordinates and area ID 77 might have 11 coordinates. I wish to save these in a 2-D array indexed by the area ID, and where each coordinate pair is combined into one Google LatLng object. At a later point I wish to retrieve just one row i.e. the set of coordinates for one area, and send them to a function that accepts an array of coordinates and draws the polygon on a map. Here's what I have:
private var coordsFromSql:ArrayCollection = new ArrayCollection();
var polyArray:Array = new Array();
for each(var item:COORDINATES in coordsFromSql)
{
// add coordinates to the array for each Area id
polyArray[item.AREA_ID].push( new LatLng(item.LATITUDE, item.LONGITUDE) );
}
So this is where the first problem ocurrs. I don't know how to add a variable number of new items to a 2-D array into a known index. i.e considering polyArray like a 2-D spreadsheet how do I for example add values to 'row' 77 i.e. polyArray[77] ?
If I run the above code, I get runtime error #1010 'A term is undefined and has no properties'
The second part of the question is how do you extract one 'row' as a new array?
Using the above example to call a drawPolygon function, can I do this?
var polyArraySlice:Array = polyArray[77].slice();
drawPolygon(color, polyArraySlice );
It looks like your loading code is close, but not quite. In your for loop you're doing:
polyArray[item.AREA_ID].push(/*...*/)
but you never actually put anything in the array there.
So your load would probably be something like this:
var polyArray:Array = []
for each(var item:COORDINATES in coordsFromSql)
{
// add coordinates to the array for each Area id
var id:Number = item.AREA_ID;
if(polyArray[id] == null) { polyArray[id] = [] }
polyArray[id].push( new LatLng(item.LATITUDE, item.LONGITUDE) );
}
Getting a copy of the one of the individual locations would work just like you had:
var polyArraySlice:Array = polyArray[77].slice();
drawPolygon(color, polyArraySlice );

Resources