## AS: Binary Increasment

• 3,300 Views
• 9 Replies
Inglor
Inglor
• Member since: Jan. 26, 2003
• Offline.
Forum Stats
Member
Level 17
Blank Slate
AS: Binary Increasment 2005-06-28 13:54:55

AS: Main

This is going to be pretty short ;)

this is just a simple technique to save you time and make your 'if's look neater

we rely on the fact that a logical 'true' is equal to 1 and a logical 'false' is equal to 0

for example, let's examine the following situation, we have acceliration in a game and we want it to move towerds 0, now our normal solution would have been

if(object.accel>0){
object.accel--;
}

now let's count on the fact that the '>' operator is a boolean operator, it's either "true" or "false" which as I already said equals to '1' and '0'

therefor we can write it now as

object.accel-=(object.accel>0);

now let's say we want it to increase by two, all we have to do is

object.accel+=(object.accel)*2;

simple eh?

now let's say we want it to increase if they key "down" is pressed

object.accel+=(Key.isDown(Key.DOWN));

nifty ain't it?

Glaiel-Gamer
Glaiel-Gamer
• Member since: Dec. 28, 2004
• Offline.
Forum Stats
Member
Level 28
Game Developer
Response to AS: Binary Increasment 2005-06-28 13:56:38

yeah, but I think it looks neater the other way. Thanks though.

Inglor
Inglor
• Member since: Jan. 26, 2003
• Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Binary Increasment 2005-06-28 14:07:22

everyone and his favourite technique, I'm just offering people the techniques, they don't ahve to use em :P

I-Jaf
I-Jaf
• Member since: Jul. 2, 2003
• Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Binary Increasment 2005-06-28 14:10:09

At 6/28/05 01:54 PM, Inglor wrote: nifty ain't it?

Nifty indeed

Don't forget about the '? : (conditional)'

The '? : (conditional)' is basicly a one line if-else statement, it's the same as a regular if-else but it takes one line, it's best for just assigning variable values depending on a condition, and you can't have other if statements within it. It just saves time and space.

myMood = (random(2)==0) ? "bad" : "good";

That one line of code means the same as these five lines of code:

if(random(2)==0){
} else {
myMood = "good";
}

Inglor
Inglor
• Member since: Jan. 26, 2003
• Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Binary Increasment 2005-06-28 14:25:12

myMood = !random(2) ? "bad" : "good";

you don't need the ==0, you can just use !

I-Jaf
I-Jaf
• Member since: Jul. 2, 2003
• Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Binary Increasment 2005-06-28 14:26:02

At 6/28/05 02:25 PM, Inglor wrote: you don't need the ==0, you can just use !

Inglor wins

Inglor
Inglor
• Member since: Jan. 26, 2003
• Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Binary Increasment 2005-06-28 14:34:05

naa, you win, I failed at mentioning that :P

anyhow, I would still do it like this:
var i:Number=random(2);
delete i;

dELtaluca
dELtaluca
• Member since: Apr. 16, 2004
• Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: Binary Increasment 2005-07-26 08:34:40

At 6/28/05 02:34 PM, Inglor wrote: naa, you win, I failed at mentioning that :P

anyhow, I would still do it like this:
var i:Number=random(2);
delete i;

lol the "bad"*i + "good"*!i looks so weird, i dont think id ever want to use that, its not very good on the readability

Claxor
Claxor
• Member since: Oct. 21, 2005
• Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Binary Increasment 2005-11-05 05:24:06

At 6/28/05 02:34 PM, Inglor wrote: naa, you win, I failed at mentioning that :P

anyhow, I would still do it like this:
var i:Number=random(2);
delete i;

I prefer other ways to do that =P even though that is shorter then my ways, i prefer to keep my code clearer.

Toast
Toast
• Member since: Apr. 2, 2005
• Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Binary Increasment 2007-08-04 16:33:25

At 6/28/05 02:34 PM, Inglor wrote: naa, you win, I failed at mentioning that :P

anyhow, I would still do it like this:
var i:Number=random(2);