Page 4 of 5

Re: IF Hater Club: Replace The Code

PostPosted: Wed Oct 21, 2009 9:29 pm
by jimmynewguy
i really don't understand max and min...just to give me some help on understanding not using "if's", how would if haters get rid of this mess of a code?
Code: Select all
//right move//
if(keyr&&!keyl){
if(xvelocity<3)xvelocity+=.5;}
//left move//
if(keyl&&!keyr){
if(xvelocity>-3)xvelocity-=.5;}
//friction(slow down)
if((keyl&&keyr)||(!keyl&&!keyr))
xvelocity*=.9;

EDIT: Ah i kinda get max and min after messing with them and changed some of the code to
Code: Select all
if(keyr)
{
xvelocity+=.5;
xvelocity=min(xvelocity,3);
}
if(keyl)
{
xvelocity-=.5;
xvelocity=max(xvelocity,-3);
}

Re: IF Hater Club: Replace The Code

PostPosted: Wed Oct 21, 2009 10:16 pm
by Fuzzy
Jimmy thats probably clean enough. Good job.

One thing you should do is use zero before your decimal places though. It makes it easier to read and can avoid bugs.

Re: IF Hater Club: Replace The Code

PostPosted: Thu Oct 22, 2009 1:04 am
by Bee-Ant
jimmynewguy wrote:
Code: Select all
if(keyr)
{
xvelocity+=.5;
xvelocity=min(xvelocity,3);
}
if(keyl)
{
xvelocity-=.5;
xvelocity=max(xvelocity,-3);
}

Use one variable to declare the key state. For example "key", and here's the value means:
1 - right
2 - left
Code: Select all
switch(key)
{
    case 1: xvelocity+=0.5;xvelocity=min(xvelocity,3);break;
    case 2: xvelocity-=0.5;xvelocity=max(xvelocity,-3);break;
}

Re: IF Hater Club: Replace The Code

PostPosted: Thu Oct 22, 2009 1:21 am
by Bee-Ant
Want to make it simpler???
Use this method. Still using "key" variable to declare the key state, and here's the value state:
-1 : left
0 - normal state
1 - right

and use this code
Code: Select all
xvelocity+=key*0.5;
xvelocity=max(-3,min(xvelocity,3);

Make sense? :D

Re: IF Hater Club: Replace The Code

PostPosted: Sat Oct 24, 2009 4:00 pm
by jimmynewguy
thanx beeant! I'm learning alot here, but i don't understand how you combine the two like when you did here
Code: Select all
 xvelocity=max(-3,min(xvelocity,3);


and i was wondering if there was a way to fix this.......this seems like a really messy 2 lines lol
Code: Select all
if((xvelocity>-.5&&xvelocity<0)||(xvelocity<.5&&xvelocity>0))
xvelocity=0;

but i've tried over and over and can't come up with a better way, because of the || or :(

Re: IF Hater Club: Replace The Code

PostPosted: Sat Oct 24, 2009 4:52 pm
by DST
don't mean to step on ur toes bee, but this one is simple...

Code: Select all
if(abs(xvelocity)<0.5){xvelocity=0;}
(you don't need the >0 <0 part, cause it'll be 0 either way, whether the value is -0.1 or if its 0).

Let me dissect the other one:

xvelocity=whichever of these is greater:
-3, or the result of (whichever is smaller, xvelocity or 3).

So the min(xvelocity, 3) caps xvelocity at 3.

So consider the min part to equal 3 here. The line then becomes:

(-3, 3). Xvelocity is now constrained to be between these two numbers.

Re: IF Hater Club: Replace The Code

PostPosted: Sat Oct 24, 2009 5:03 pm
by Bee-Ant
@jimmy: as DST said,
Code: Select all
xvelocity=max(-3,min(xvelocity,3));

Is equal to :
Code: Select all
if(xvelocity<=-3)xvelocity=-3;
if(xvelocity>=3)xvelocity=3;


@DST: wow, can you tell me what abs do?

Re: IF Hater Club: Replace The Code

PostPosted: Sat Oct 24, 2009 5:10 pm
by DST
abs() strips the - from a number.

abs(-3) will return 3. abs(3) will return 3.

To turn something on or off forever on an event:

Code: Select all
light=abs(light-1);

If light is 0, 0-1=-1; abs(-1)=1.
if light is 1, 1-1=0; abs(0)=0.

Re: IF Hater Club: Replace The Code

PostPosted: Sat Oct 24, 2009 5:20 pm
by Bee-Ant
jimmynewguy wrote:
Code: Select all
if((xvelocity>-.5&&xvelocity<0)||(xvelocity<.5&&xvelocity>0))
xvelocity=0;


Huh??thats messy because the messy logic. isnt it equal to
Code: Select all
if(xvelocity>-0.5&&xvelocity<0.5)xvelocity=0;

Then just try DST code :D
Or this code
Code: Select all
xvelocity=(abs(xvelocity)>0.5)*xvelocity;

Re: IF Hater Club: Replace The Code

PostPosted: Sat Oct 24, 2009 5:21 pm
by Bee-Ant
DST wrote:abs() strips the - from a number.

abs(-3) will return 3. abs(3) will return 3.

To turn something on or off forever on an event:

Code: Select all
light=abs(light-1);

If light is 0, 0-1=-1; abs(-1)=1.
if light is 1, 1-1=0; abs(0)=0.

Hmm...just like what i thought... :D

Re: IF Hater Club: Replace The Code

PostPosted: Sat Oct 24, 2009 5:24 pm
by jimmynewguy
Thanx DST and beeant, how did i forget abs()? i've used that alot recently......oh and i thought of a good way to get rid of this code
Code: Select all
if((keyl&&keyr)||(!keyl&&!keyr))

i changed it too this
Code: Select all
if((keyl*-1)+keyr==0)

lol

Re: IF Hater Club: Replace The Code

PostPosted: Sat Oct 24, 2009 5:37 pm
by Bee-Ant
????
What does that mean?
I have never include a variable into an IF without any value condition.

Whats if(keyl) means?
Is it : if keyl variable have a value?or its not null?

Then your code doesnt make sense to me...confuse me :P

Re: IF Hater Club: Replace The Code

PostPosted: Sat Oct 24, 2009 5:46 pm
by jimmynewguy
well i'm pretty sure if(keyl) means if it has a value, i've always used it instead of ==1....but i don't know if it works for negatives or greater than 1 :lol:

THIS IS THE 10thoughsandth post in the support topic (i think)

Re: IF Hater Club: Replace The Code

PostPosted: Sat Oct 24, 2009 6:02 pm
by Bee-Ant
Hmm, just as i thought :D
I think its able for any value greater than zero.
Thanks jim

Re: IF Hater Club: Replace The Code

PostPosted: Sun Nov 15, 2009 3:09 pm
by Fuzzy
How can I optimise this?

Code: Select all
if (BeeAnt == teh.suck)
{
    kickDST();
}