How do i make my pickNum array just one index instead of 3 different indexes. - actionscript-3

this will random my numPool and push the random three numbers to my array called pickNum. I need that pickeNum to be just one index instead of three indexes. Thanks and i will appreciate any help thanks.
var numPool:Array = [1,2,3];
var pickNum:Array = [];
var randomCount:Number = 3;
var r:Number;
for (var i = 0; i < randomCount; i++)
{
r = Math.floor(Math.random() * numPool.length);
pickNum[pickNum.length] = numPool.splice(r,1);
}
trace("Number Picked " + pickNum);

Can't say I completely understand what you are saying, but the issue with the code above is that splice returns an Array containing the values that were spliced from the array. Also, would be easiest to just do a push. So the proper line for getting that value and pushing it into the pickNum array would be :
pickNum.push(numPool.splice(r,1).pop());
And if you are saying that you want only 1 random number from the pool, then why do you need a loop ?

Related

Array of stagewebviews

I am in the need of multiple staged WebViews for holding multiple loaded websites at the same time.
I was hoping to manage this by making an array of webviews object, so i could call them later as view[i].
var view:Array=[webview0, webview1, webview2];
for each (var v in view){
var v:StageWebView = new StageWebView();
This gives error: 1086: Syntax error: expecting semicolon before left bracket.
Does someone know how to make an array like that?
You're doing something really weird there in terms of syntax. If you just want an Array of freshly created instances, it goes like that:
// Initialize the array.
var Views:Array = new Array;
// This loop counts 0,1,2.
for (var i:int = 0; i < 3; i++)
{
// Create a new instance.
// Yes, you can omit () with new operator if there are no arguments.
var aView:StageWebView = new StageWebView;
// Assign the new element to your array.
Views[i] = aView;
}
Or, if you need only 3 then you don't need to go algorithmic.
var Views:Array = [new StageWebView, new StageWebView, new StageWebView];
Not on topic but related:
Here is an example of one HTML page hold multiple StageWebViews
https://www.w3schools.com/graphics/tryit.asp?filename=trymap_basic_many

how to catch duplicate random number

I want to display random number in bb1,bb2,bb3,bb4.
alphaset:Array = [1,2,3,4,5,6,7,8,9,10];
bb1 = randomNumber(0, alphaset.length);
bb2 = randomNumber(0, alphaset.length);
bb3 = randomNumber(0, alphaset.length);
bb4 = randomNumber(0, alphaset.length);
in above, i want to delete duplicate entry of each variable (i.e., bb1,bb2,bb3,bb4)
finally i want random number without duplicate entry of each variable
please anyone help me to achieve it.
The above four var are options for addition program. And i have to compare the above four var and bbb = randomNumber(1,10) and display bbb value in that four var randomly i,e., [bbb=bb1 or bbb=bb2 or bbb=bb3 or bbb=bb4] coz, i have to select the bbb value to display output.
If all the values in the array are unique to begin with, just remove them when they're used:
var list:Array = [1,2,3,4,5,6,7,8,9,10];
function getRandom(list:Array):*
{
var i:int = Math.random() * list.length;
return list.splice(i, 1)[0] || null;
}
var bb1:int = getRandom(list);
var bb2:int = getRandom(list);
var bb3:int = getRandom(list);
var bb4:int = getRandom(list);
One option is to shuffle the entire list of random elements - then you can take the first N elements, where N is the number you need, and know those will all be unique and randomly chosen. If you use a large amount of the total random element list, this is faster than sampling and removing.
If you can't find a standard implementation of shuffle, implement the Fisher-Yates Shuffle: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm It is important that there are n! possible ways for the shuffle to come out, not n^n which you incorrectly get if you take each element one at a time and allow it to be exchanged with any other element, including previous elements, otherwise certain configurations will be more common than others.

How to push instantiated MC into Array dynamically?

Im really stuck. I have 5 MC´s that are being spliced from one array at a certain time. In that same function I want to push another movieclips into another array. The two arrays holds mc's that represent right or wrong answers. So when one question is being given a correct answer that questions visualisation alters.
This function holds a incrementing variable as I do want the mc's to be pushed by the user and one at the time. The thing is I cant seem to refer them properly.
I´ve tried
pQuestSum = this[pQuest + pQuestNumber];
and
pQuestSum = this[pQuest] + pQuestNumber;
and pretty much everything I´ve imagined would work...but the problem is I havent tried
the right thing.
when I trace pQuestSum (which would be the reference) I get an error saying thats its not a number.
this is one of 5 mc's named from 1-5:
var passedquest1:PassedQuest = new PassedQuest();
this is the vars that i try to to build a reference of
var pQuest = "passedquest";
var pQuestNumber = 1;
var pQuestSum;
var questCorrArray:Array = [];
if(event.target.hitTestObject(questArray[ix])){
removeChild(questArray[ix]);
questArray.splice(ix,1);
pQuestNumber ++;
pQuestSum = this[pQuest] + pQuestNumber;
trace("pQuestSum"); // NaN
questCorrArray.push(pQuestSum);
//trace(questArray.length);
pointsIncreased = false;
questPoints = 0;
}
How do I refer an existing movieclip when the reference consists of both a string and a number? Hope I made myself somewhat clear:)
If you had an instance of an object on your timeline called "passedquest1" (as an example), then you could access it this way:
var myObj = this["passedquest" + 1];
Or,
var pQuest = "passedquest";
var pQuestNumber = 1;
var myObj = this[pQuest+ pQuestNumber.toString()];
When you do this: pQuestSum = this[pQuest] + pQuestNumber;, you are trying add the number to an object (this[pQuest]), unless you have number/int var called "passedquest", this will result in NaN.

AS3 - getting variable named in order within a function

I am sorry if this is a beginner's question.
I made some Arrays named like map01, map02 and so on... As you can see, I'm making a tile-based flash here. And I need to make a function that when you input a number like: createmap(1); it will get the variable map01 and use the information.
Can I do anything like: var temp:Array = Array(["map" + valueInput]);??
Please tell me if you need anything more.
First, instead of having variables with indices in their names, you should create an array of them. Here, an array of arrays.
So you just have to call var temp:Array = maps[valueInput] as Array;.
If you really don't want to do that and stick with your n variables, you can write
var index:String = valueInput.toString();
if (index.length == 1)
index = "0" + index; //have the index on two digits "01", "02"
var temp:Array = this["map" + index];
Note that it will only work for your 99 first variables (oh God...)

Difficulty outputting an array of indexes from an existing array consisting of strings

So I am trying to create a function that searches through an array based on a searchTerm. If the elements within the array have the searchTerm in it, it should output ALL of indexes inside of MyArray[];.
I hope I have explained clearly, thanks in advance.
Here's a corrected version:
var colours = ["I like the colour red", "I hate the colour yellow", "I love the colour blue"];
function myFunction(colours, searchTerm) {
var myArray = [];
searchTerm = searchTerm.toLowerCase();
for (var i = 0; i < colours.length; i++) {
if (colours[i].toLowerCase().indexOf(searchTerm) >= 0) {
myArray.push(i);
}
}
return myArray;
}
alert(myFunction(colours,"colour")) //Should return indexes 0,1,2 in myArray
And a working demo here: http://jsfiddle.net/jfriend00/GDM9R/.
I had to fix a lot of issues:
You weren't adding results to myArray properly.
You weren't adding the index to myArray.
You weren't testing the results of .indexOf() properly (it returns -1 when no match).
You were iterating over the length of the search phrase, not the number of items in the array.
You didn't declare i as a local variable so it was an implicit global variable.
myArray = colours[i] does not append to the array.
myArray.push(a);