00:00
00:00
Newgrounds Background Image Theme

Chan99 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS: Binary Increasment

4,190 Views | 9 Replies
New Topic Respond to this Topic

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?

Response to AS: Binary Increasment 2005-06-28 13:56:38


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

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

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

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 !

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

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

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);
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

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);
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

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);
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