Forum Topic: AS: Binary Increasment

(2,091 views • 9 replies)

This topic is 1 page long.

<< < > >>
None

Inglor

Reply To Post Reply & Quote

Posted at: 6/28/05 01:54 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

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?


None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 6/28/05 01:56 PM

Glaiel-Gamer NEUTRAL LEVEL 27

Sign-Up: 12/28/04

Posts: 8,041

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


None

Inglor

Reply To Post Reply & Quote

Posted at: 6/28/05 02:07 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

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


None

I-Jaf

Reply To Post Reply & Quote

Posted at: 6/28/05 02:10 PM

I-Jaf EVIL LEVEL 09

Sign-Up: 07/02/03

Posts: 4,256

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";
}


None

Inglor

Reply To Post Reply & Quote

Posted at: 6/28/05 02:25 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

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

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


None

I-Jaf

Reply To Post Reply & Quote

Posted at: 6/28/05 02:26 PM

I-Jaf EVIL LEVEL 09

Sign-Up: 07/02/03

Posts: 4,256

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

Inglor wins


None

Inglor

Reply To Post Reply & Quote

Posted at: 6/28/05 02:34 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

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;


None

dELtaluca

Reply To Post Reply & Quote

Posted at: 7/26/05 08:34 AM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,542

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

My social worker says im special!

BBS Signature

None

Claxor

Reply To Post Reply & Quote

Posted at: 11/5/05 05:24 AM

Claxor DARK LEVEL 10

Sign-Up: 10/21/05

Posts: 2,465

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

None

Toast

Reply To Post Reply & Quote

Posted at: 8/4/07 04:33 PM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,914

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.


All times are Eastern Standard Time (GMT -5) | Current Time: 05:01 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!