Be a Supporter!

How to save time using for statment

  • 218 Views
  • 3 Replies
New Topic Respond to this Topic
qwereeer
qwereeer
  • Member since: Jun. 29, 2007
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
How to save time using for statment 2011-03-19 12:04:24 Reply

I want to set 30 kinds of weapons that have different name as variables.

For instance, I'd like to use variables to judge whether player

has aquired the weapons. ( 0 for not aquired, 1 for aquired)

weapon1 = 1
weapon2 = 0
weapon3 = 0
..... And so on

I don't want to repeat weapon4 weapon5 weapon6 all the way

up to weapon30 because it's waste of time.

I know this can be done fast by using for statement

but I dunno how exactly to.

Can someone show me a way to do this?


w

1Kingfire1
1Kingfire1
  • Member since: Dec. 16, 2007
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to How to save time using for statment 2011-03-19 12:22:39 Reply

You could just store the true/false in an array

var weaponArray:Array = [true];
for( var i:int = 1; i < 31; i++ )
{
weaponArray[i] = false;
}
// Saying you've aquired first weapon, but no others.

//Then to check

for( var k:int = 0; k < 31; k++ )
{
if( weaponArray[k] )
{
unlockWeapon("weapon"+k);
}
}

Is that what you meant?


Want to increase knowledge in programming. If you need someone to program something, I will be happy to help for free :) .

Redshift
Redshift
  • Member since: Feb. 12, 2005
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to How to save time using for statment 2011-03-19 12:25:30 Reply

Arrays. Learn them, and learn them well.

You essentially want to create an array with 30 elements:
(also, you should use 'true' and 'false' instead of 1 and 0)

var weapons:Array = new Array(30);
weapons[0] = true; // set the first weapon to true, notice arrays begin at 0, and not 1

// set the rest to false
for (var i:int = 1; i < weapons.length; i++) {
    weapons[i] = false;
}

Then later you can do stuff like set the 15th weapon to true, as an example:

weapon[14] = true; // the 1st element is 0, therefore the 15th element is 14

And if you want, loop through them all again somewhere else and do something with each value:

for (var i:int = 0; i < weapon.length; i++) {
    // do something with 'weapon[i]'
}

#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}

BBS Signature
xKiRiLLx
xKiRiLLx
  • Member since: Feb. 8, 2007
  • Offline.
Forum Stats
Member
Level 09
Game Developer
Response to How to save time using for statment 2011-03-19 12:44:23 Reply

http://kirill-poletaev.blogspot.com/2010 /08/loop-statements-in-as3.html