Be a Supporter!

AS: Binary Increasment

  • 2,897 Views
  • 9 Replies
New Topic Respond to this Topic
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
AS: Binary Increasment Jun. 28th, 2005 @ 01:54 PM Reply

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 Jun. 28th, 2005 @ 01:56 PM Reply

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 Jun. 28th, 2005 @ 02:07 PM Reply

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 Jun. 28th, 2005 @ 02:10 PM Reply

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){
myMood = "bad";
} else {
myMood = "good";
}

Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Binary Increasment Jun. 28th, 2005 @ 02:25 PM Reply

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 Jun. 28th, 2005 @ 02:26 PM Reply

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 Jun. 28th, 2005 @ 02:34 PM Reply

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

anyhow, I would still do it like this:
var i:Number=random(2);
mymood="bad"*i+"good"*!i;
delete i;

dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: Binary Increasment Jul. 26th, 2005 @ 08:34 AM Reply

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);
mymood="bad"*i+"good"*!i;
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


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
Claxor
Claxor
  • Member since: Oct. 21, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to AS: Binary Increasment Nov. 5th, 2005 @ 05:24 AM Reply

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);
mymood="bad"*i+"good"*!i;
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.


BBS Signature
Toast
Toast
  • Member since: Apr. 2, 2005
  • Online!
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Binary Increasment Aug. 4th, 2007 @ 04:33 PM Reply

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);
mymood="bad"*i+"good"*!i;
delete i;

Bad multiplated by 0 or 1 plus good multiplated by not 0 or 1. Cause we all know that bad multiplayed by 0 is good. :p

anyway I would recommend this technique to any ASer, it's very nice.


BBS Signature