AS3 Array 1 to 100 - actionscript-3

I'm having trouble finding a way to do an array that doesn't make me type all the numbers , for example :
(i found this code online)
var array:Array=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
var odds:Array=[],
evens:Array=[],
odds_sum:int=0,
evens_sum:int=0
;
for(var i:int = 0; i < array.length; i++){
if(array[i] % 2 == 1){
odds.push(array[i]);
odds_sum += array[i];
} else {
evens.push(array[i]);
evens_sum += array[i];
}
}
trace(odds);
trace(odds_sum);
trace(evens);
trace(evens_sum);
i wanted the code to trace all the numbers between 1 and 100 (including the 1 and 100) but don't have to type them all down.
Help solving this would be much appreciated

Your question is ambiguous , If you simply need all the numbers between 1 and 100 you simply do
for(var i:int = 1; i <= 100; i++){
trace(i);
}
or if you want them in your array
var arr:Array = new Array();
for(var i:int = 1; i <= 100; i++){
arr.push(i);
}
or if you want odd and even numbers in separate array
var odds:Array = new Array();
var evens:Array = new Array();
for(var i:int = 1; i <= 100; i++){
if(i%2==0)
evens.push(i);
else
odds.push(i);
}
where the array odds/even have appropriate numbers between 1 and 100.

Related

adding X amount of addChild per maxvalue in loop

The assignment is the spawn several light and dark feathers according to score points from a quiz. The light feathers symbolize the correct points (light_feather), and the dark feather are the incorrect points (dark_feather) (Each are being tracked). All the feathers are supposed to line up on one line, meaning first light feathers, followed by the dark feathers. I got the quiz dynamics figured out, and the function I have posted here is only for when they press end quiz.
var light_feather:LightFeather = new LightFeather();
var dark_feather:DarkFeather = new DarkFeather();
var good_answers:uint = 0;
var bad_answers:uint = 0;
function avsluttFunc (evt:MouseEvent)
{
var sum_LightFeatherX:Number = 0;
for (var i = 0; i < good_answers; i++) {
addChild(light_feather);
light_feather.x += 12 + (i*16);
light_feather.y = 0;
trace("Lys X-verdi: " + light_feather.x);
sum_LightFeatherX += Number(light_feather.x);
return sum_LightFeatherX;
}
trace(sum_LightFeatherX);
dark_feather.x += sum_LightFeatherX;
for (var j = 1; j <= bad_answers; j++) {
addChild(dark_feather);
dark_feather.x += 12 + (j*16);
dark_feather.y = 0;
trace("Mørk X-verdi: " + dark_feather.x);
}
/*
//Resetter poengsummen
good_answers = 0;
bad_answers = 0;
*/
}
You can do what you are looking for using only one for loop, take a look :
var good_answers:uint = 2;
var bad_answers:uint = 4;
function avsluttFunc(evt:MouseEvent)
{
for (var i:int = 0; i < good_answers + bad_answers; i++) {
var feather:DisplayObject = i < good_answers ? new LightFeather() : new DarkFeather();
feather.x += 12 + i * (feather.width + 1);
feather.y = 0;
addChild(feather);
}
}
This code example will create 4 DarkFeather instances next to 2 LightFeather ones.
Edit :
How to add your objects to an array ?
// feathers array should be accessible for both codes (adding and removing objects)
var feathers:Array = [];
for (var i:int = 0; i < good_answers + bad_answers; i++) {
var feather:DisplayObject = i < good_answers ? new LightFeather() : new DarkFeather();
addChild(feather);
feathers.push(feather);
}
then to remove them from the stage, you can do for example :
for (var i:int = 0; i < feathers.length; i++) {
var feather:DisplayObject = DisplayObject(feathers[i]);
feather.parent.removeChild(feather);
}
Hope that can help.

Checking for straight combination in poker

I want to check for a straight combination in a poker game.
So, I have this array: var tempArr:Array = new Array;
I have this for sorting the array:
for (i = 0; i < 7; i++)
{
tempArr[i] = pValue[i];
}
tempArr.sort( Array.NUMERIC );
pValue is the value of the cards, it's have range from 2 to 14.
So, if I have this Array: tempArray = [2,3,3,4,5,5,6];
How can I check if I have a straight combination in my hand?
Set a bucket array to save if you got a card in hand
var t:Array = [];
//t[2] = 1;mean you have 2
// t[3] = 0;mean you don't have 3
//first initialize t
for(var i:int = 0; i < 15; i++)
{
t[i] = 0;
}
//then set the values from tempArray
for (var j:int = 0; j < tempArray.length; j++)
{
t[tempArray[j]] = 1;
}
//if you have a straight combination in your hand
//you will get a k that t[k] & t[k-1]& t[k-2] & t[k-3] & t[k-4] == 1
var existed:boolean = false;//the flag save if you got a straight combination
for (var k:int = t.length - 1; k >= 4; k--)
{
if (t[k] == 0)
{
continue;
}
if ((t[k] & t[k-1] & t[k-2] & t[k-3] & t[k-4]) == 1)
{
existed = true;
break;
}
}

Random 100 numbers in an array are increased by 1 but not sum to 100

My friend posted this problem on Facebook, and I don't know how it happened either.
Consider this code:
var num:Array = new Array();
var i:int, sum:int = 0;
for(i=0; i<100; i++) num[i] = 0;
for(i=0; i<100; i++) num[Math.floor(Math.random()*100)] += 1;
for(i=0; i<100; i++) sum += num[i];
trace(sum);
It should sum to 100, but it prints different number each time it's running, sometimes sum becomes greater than 100. (It's same even when sum has type Number or *, and 1 is changed to some other numbers such as 0.1; the result is not close to 10.0 - something not like 9.9999993.)
Meanwhile, these code are working correctly.
var num:Array = new Array();
var i:int, sum:int = 0;
for(i=0; i<100; i++) num[i] = 0;
for(i=0; i<100; i++) num[Math.floor(Math.random()*100)]++;
for(i=0; i<100; i++) sum += num[i];
trace(sum);
and
var num:Array = new Array();
var i:int, sum:int = 0;
for(i=0; i<100; i++) num[i] = 0;
for(i=0; i<100; i++){
var tmp:Number = Math.floor(Math.random()*100);
num[tmp] += 1;
}
for(i=0; i<100; i++) sum += num[i];
trace(sum);
all correctly prints 100 all the times.
Plus, this code in JavaScript works correctly
var num = new Array();
var i, sum = 0;
for(i=0; i<100; i++) num[i] = 0;
for(i=0; i<100; i++) num[Math.floor(Math.random()*100)] += 1;
for(i=0; i<100; i++) sum += num[i];
console.log(sum);
while it isn't when console.log is just changed to trace and run as an AS2 or AS3 code. What caused this behavior?
(Adobe Flash CS6 is used, and it seems that it does not matter which Flash Player is targetted (installed version is 12.0.0.38) and whether AS2 or AS3 is used; even "AIR 3.2 for Desktop" behaves equally.)
Other person on Facebook just commented on my friend's post:
function foo():int {
trace("foo()");
return 0;
}
var arr:Array = new Array();
trace("bar");
arr[foo()] = 0;
trace("bar");
arr[foo()] += 10;
this prints
bar
foo()
bar
foo()
foo()
, which show that when a += b is evaluated, a is evaluated twice.

How to make a variable loop in actionscript 3

I'm trying to make a loop that won't crash my flash application. I want variable CN to go from 1 to 10, and then 10 turns into 1 (1,2,3,4,5,6,7,8,9,10,1....). This is what I have so far...
var CN:int = 1;
for(int CN = 1; CN<100; CN++);
NumberCounter.text = String(CN);
Please help. I don't get this at all :( I'm a novice programmer so a lot of things I do won't make much sense.
Your question is a bit unclear. Are you trying to go from 1 to 10 then from 10 to 1 once (20 steps) or to go back and forth between 1 and 10 in 100 steps ?
If it's the first, you can try something like this:
for(var i:int = 0, j:int = 0; i < 20; i++){
if(i < 10) j++;
else j--;
trace(j);//put this in your text field
}
if it's the second:
for(var i:int = 0; j:int = 1, k:int = 0; i < 100; i++){
if(i % 10 == 0) j *= -1; //every 10 steps flip (multiply by -1) the increment direction(increase/decrease)
k += j;//increment k based on j which will either increase or decrease
trace(k);//use this value
}
However the textfield will update right away. If you want to display this change in time you can use the ENTER_FRAME event to increment (rather than the for loop) or a tween engine to animate the value
For an infinite loop:
while(true){
for(var CN:int=1; CN <= 10; CN ++) NumberCounter.text = String(CN);
}
To do it 10 times, which looks like your code is hinting at:
for (int x= 0; x< 10; x++){
for(var CN:int=1; CN <= 10; CN ++) NumberCounter.text = String(CN);
}
Try this
if(CN>1)
{
var CN:int = 1;
for(int CN = 1; CN<10; CN++);
NumberCounter.text = String(CN);
}
else
{
}
var Numberofwins = 0;
CN.addEventListener(Event.ENTER_FRAME, checkFrame);
function checkFrame(event:Event):void{
if(CN.currentFrame == 11){
CN.gotoAndPlay(1);
}
}
import flash.events.MouseEvent;
Submit.addEventListener(MouseEvent.CLICK, CheckIf8);
function CheckIf8(event:MouseEvent):void
{
if(CN.currentFrame == 8)
{
Numberofwins++;
trace (Numberofwins);
Scorebox.text = String(Numberofwins);
}
else
{
gotoAndStop("Loose1");
}
}
This was my solution

reading out multi dimensional array

the multi array is read in correctly
but reading out the array he messes up.
the action that suppose to be done on each element from array in array, he doesn't do it anymore when more indexes in 2 array are made.
when a second index in 2 array i made he doesn't do this action anymore on previous index, only the last made index.
thanks
private var Enemy:Array = new Array();//1st array
private var EnemyHull:Array = new Array();
private var waves:Array = new Array();//2 array ==> array 1 get in this
private function enterFrame(e:Event):void
{
//Enemy Ai
for(var i2:uint; i2 < waves.length; i2++){
for(var i:uint; i < Enemy.length; i++){
waves[i2][i].x -= 1;//when a second index in 2 array i made he doesn't do this action anymore on previous index, only the last made index.
}
}
}
private function enemySpawnen(event:TimerEvent):void
{
for(var i:uint = 0; i < hoeveelheidEnemy;i++){
//
}
if(Enemy[i] != null){;
viewContainer.addChild(Enemy[i]);
//
}
}
waves[iwaves] = Enemy;
iwaves++;
}
function shoot(e:Event):void
{
//
try{
for(var i2:uint; i2 < waves.length; i2++){
for(var i:uint = Enemy.length-1; i >= 0;i--){
if(kogel.hitTestObject(waves[i2][i])){
//
}
}
}
}
}
catch(e:Error){
}
}
}
Your for loops misses an important thing to work properly : you have to initialize your iterator (i or i2) to a starting value.
You wrote :
for(var i2:uint; i2 < waves.length; i2++){
for(var i:uint; i < Enemy.length; i++){
waves[i2][i].x -= 1;
}
}
But what you need is :
for(var i2:uint = 0; i2 < waves.length; i2++){
for(var i:uint = 0; i < Enemy.length; i++){
waves[i2][i].x -= 1;
}
}
Dont forget the = 0 in your loop declaration !