Be a Supporter!

AS: Binary Increasment

  • 2,953 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 2005-06-28 13:54:55 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 2005-06-28 13:56:38 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 2005-06-28 14:07:22 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 2005-06-28 14:10:09 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 2005-06-28 14:25:12 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 2005-06-28 14:26:02 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 2005-06-28 14:34:05 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 2005-07-26 08:34:40 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 2005-11-05 05:24:06 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
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Binary Increasment 2007-08-04 16:33:25 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