IF Hater Club: Replace The Code

Non-platform specific questions.

Re: IF Hater Club: Replace The Code

Postby jimmynewguy » Wed Oct 21, 2009 9:29 pm

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);
}
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: IF Hater Club: Replace The Code

Postby Fuzzy » Wed Oct 21, 2009 10:16 pm

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.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: IF Hater Club: Replace The Code

Postby Bee-Ant » Thu Oct 22, 2009 1:04 am

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;
}
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: IF Hater Club: Replace The Code

Postby Bee-Ant » Thu Oct 22, 2009 1:21 am

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
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: IF Hater Club: Replace The Code

Postby jimmynewguy » Sat Oct 24, 2009 4:00 pm

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 :(
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: IF Hater Club: Replace The Code

Postby DST » Sat Oct 24, 2009 4:52 pm

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.
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: IF Hater Club: Replace The Code

Postby Bee-Ant » Sat Oct 24, 2009 5:03 pm

@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?
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: IF Hater Club: Replace The Code

Postby DST » Sat Oct 24, 2009 5:10 pm

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.
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: IF Hater Club: Replace The Code

Postby Bee-Ant » Sat Oct 24, 2009 5:20 pm

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;
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: IF Hater Club: Replace The Code

Postby Bee-Ant » Sat Oct 24, 2009 5:21 pm

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
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: IF Hater Club: Replace The Code

Postby jimmynewguy » Sat Oct 24, 2009 5:24 pm

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
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: IF Hater Club: Replace The Code

Postby Bee-Ant » Sat Oct 24, 2009 5:37 pm

????
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
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: IF Hater Club: Replace The Code

Postby jimmynewguy » Sat Oct 24, 2009 5:46 pm

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)
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: IF Hater Club: Replace The Code

Postby Bee-Ant » Sat Oct 24, 2009 6:02 pm

Hmm, just as i thought :D
I think its able for any value greater than zero.
Thanks jim
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: IF Hater Club: Replace The Code

Postby Fuzzy » Sun Nov 15, 2009 3:09 pm

How can I optimise this?

Code: Select all
if (BeeAnt == teh.suck)
{
    kickDST();
}
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

PreviousNext

Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron