Help: Why aren't I jumping?

Non-platform specific questions.

Help: Why aren't I jumping?

Postby Unspaceman » Sun Jan 20, 2013 5:22 am

Hi all, I'm doing my first work in Game Editor with really no coding experience, and I'm pretty much using extensive references to piece together something marginally functional. So here's the code that I'm working with, but I'm just not jumping and I have no earthly idea why.

Player > Draw Actor
Code: Select all
char * key = GetKeyState();
float tspeed=0;
char anim[32];

if(vdir==0){vdir=1;}

if(key[KEY_RIGHT]==1){
hdir=0;
if(vdir>0)
 tspeed=speed;
 }

 
if(key[KEY_LEFT]==1){
hdir=1;
if(vdir>0)
 tspeed=-speed;
 }
 
if(key[KEY_SPACE]==1 && vdir!=1){
yvelocity=jump*canjump;
canjump=0;
 }

if(key[KEY_LSHIFT]==1){
dash=2;
tspeed*=dash;
 }

if(key[KEY_DOWN]==1){
if(vdir<2)
vdir=0;
tspeed=0;
}
else if(vdir==2){
vdir=0;
tspeed=speed;
}
 
x+=tspeed;
yvelocity+=1;

if(yvelocity>2 && canjump==1){
canjump=0;
vdir=2;
}


Player > Collision
Code: Select all
PhysicalResponse(MOVE_EVENT_ACTOR_ONLY, USE_CALCULATED_MASS, 1,1,0,1);
canjump=1;
if(vdir==2){
vdir=1;
}


Player > Create Actor
Code: Select all
speed=4;
hdir=0;
vdir=1;
jump=-12;
canjump=0;
dash=1;


If anyone could provide a little insight to why my Actor isn't jumping, I'd be very grateful.
Unspaceman
 
Posts: 4
Joined: Sat Jan 19, 2013 8:41 am
Score: 0 Give a positive score

Re: Help: Why aren't I jumping?

Postby skydereign » Sun Jan 20, 2013 5:51 am

It is because vdir is equal to 1, and your if statement requires vdir to not equal one when jumping. Why do you have vdir? Another problem that comes up, is that you have the jump code in the draw actor, so if the conditions are correct for jumping (vdir!=1 and space pressed), if the player doesn't release jump in one frame, the jump code will trigger again, and stop the player's jump (as yvelocity=jump*canjump will equal 0).
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Help: Why aren't I jumping?

Postby Unspaceman » Sun Jan 20, 2013 6:06 am

So would your recommendation be to put the jump code in its own KeyDown event?

I'll be honest, I don't see the entire point of of vdir, and it's not my decision. I'm trying to adapt upon other code that I've seen so I can learn through experience. It's certainly a rough trail though.
Unspaceman
 
Posts: 4
Joined: Sat Jan 19, 2013 8:41 am
Score: 0 Give a positive score

Re: Help: Why aren't I jumping?

Postby skydereign » Sun Jan 20, 2013 6:14 am

Unspaceman wrote:I'll be honest, I don't see the entire point of of vdir, and it's not my decision.

Don't do this. This is how bad habits start, as well as you run into all sorts of problems. You should know why code works before using it. Or at least, understand at some level what it is trying to do.

Unspaceman wrote: I'm trying to adapt upon other code that I've seen so I can learn through experience. It's certainly a rough trail though.

The problem with this is that some code on this site, is pretty bad. What you have currently implemented could be easily done in cleaner ways, and without the hidr/vdir/canjump and other variables. Some of them are useful, but you should know what they do, and what their values stand for, otherwise you won't be able to use it.

Unspaceman wrote:So would your recommendation be to put the jump code in its own KeyDown event?

If you want the key event only to trigger once per press, yes I recommend using the keydown event.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Help: Why aren't I jumping?

Postby Unspaceman » Sun Jan 20, 2013 6:19 am

I do have ever intention to learn and understand the code and how it works. But, I'm also trying to get groundwork for, if nothing else, test stages for a personal project, so I'm flying by the seat of my pants to get a semi-functional pre-alpha finished. It's unfortunate that there are poor code examples on this site, but, gotta start somewhere.

Thanks a lot for your help!
Unspaceman
 
Posts: 4
Joined: Sat Jan 19, 2013 8:41 am
Score: 0 Give a positive score

Re: Help: Why aren't I jumping?

Postby skydereign » Sun Jan 20, 2013 6:22 am

Unspaceman wrote: It's unfortunate that there are poor code examples on this site,

That is what happens when new users try to help. We don't filter posts like that. Not to mention, even well written code can be bad if not used properly.

You might look here. It is my preferred way of setting up player interaction in gE. http://game-editor.com/State_Method
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Help: Why aren't I jumping?

Postby Unspaceman » Sun Jan 20, 2013 6:52 am

I've had that tab open for a while, seemed far too complicated to jump into immediately, but I'm certainly going to go over it and apply it as best I can.
Unspaceman
 
Posts: 4
Joined: Sat Jan 19, 2013 8:41 am
Score: 0 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron