# How can I run and jump at the same time?

I'm having some trouble with the game I started. http://testing.fyrastudio.com/lab/tweetOlympics/v0.002/ The thing is that i have an athlete running and he must jump at the same time. A race with obstacles.

I have him running (with pressing the letter Q repeateadly). I also have him jumping (with letter P) But the thing is that when he runs and jumps at the same time, he seems to be jumping at the same place, instead of going forward with the jump... any ideas how can I fix this??

This is the code I'm using for running and jumping on a continuos loop.

//if accelearing and the last time that he accelerated was less than X seconds ago, hes running an accelaring
if (athlete.accelerating && timeCurrent - athlete.last_acceleration > athlete.delay_acceleration) {
athlete.accelerating = false;
athlete.last_acceleration = timeCurrent;
athlete.running = true;
}
if (!athlete.accelerating && timeCurrent - athlete.last_acceleration > athlete.delay_acceleration) {
athlete.decelerating = true;
}
if(athlete.decelerating && timeCurrent - athlete.last_deceleration > athlete.delay_deceleration){
if(athlete.speed >= 1){
//athlete starts to decelarate
athlete.last_deceleration = timeCurrent;
athlete.decelerate();
}else {
athlete.running = false;
}
}
if (athlete.running) {
athlete.position += athlete.speed;
}
if (athlete.jumping) {
if (athlete.jump_height < 1) {
athlete.jump_height = 1;
}else {
if (athlete.jump_height >= athlete.jump_max_height) {
athlete.jump_height = athlete.jump_max_height;
athlete.jumping = false;
}else {
athlete.jump_height = athlete.jump_height * athlete.jump_speed;
}
}

}
if (!athlete.jumping) {

if(athlete.jump_height > 1){
athlete.jump_height = athlete.jump_height * 0.9;
}else {
athlete.jump_height = 1;
}
}

athlete.scaleX = athlete.scaleY = athlete.jump_height;
athlete.x = athlete.position;


Thanks!

Your code appears to work fine, though it's a bit messy. I tested your code and read it, both showed that the sprite continued to move when jumping. I do see that you're setting the scale of the sprite instead of changing the y value when it's jumping. You're changing the X scale at the same time, so this may give the illusion of your sprite not moving forward. Try moving the sprite up instead of changing its scale, or just change the scale in the Y axis. That'll make it easier to see what's happening.

Additionally, this cleaned up code is a bit easier to follow:

if(timeCurrent - athlete.last_acceleration > athlete.delay_acceleration)
{
if (athlete.accelerating) {
athlete.accelerating = false;
athlete.last_acceleration = timeCurrent;
athlete.running = true;
} else {
athlete.decelerating = true;
}
}
if(athlete.decelerating && timeCurrent - athlete.last_deceleration > athlete.delay_deceleration){
if(athlete.speed >= 1){
//athlete starts to decelarate
athlete.last_deceleration = timeCurrent;
athlete.decelerate();
} else {
athlete.running = false;
}
}

if (athlete.running) {
athlete.position += athlete.speed;
}

if (athlete.jumping) {
if (athlete.jump_height < 1) {
athlete.jump_height = 1;
} else {
if (athlete.jump_height >= athlete.jump_max_height) {
athlete.jump_height = athlete.jump_max_height;
athlete.jumping = false;
} else {
athlete.jump_height = athlete.jump_height * athlete.jump_speed;
}
}
} else { //not jumping
if(athlete.jump_height > 1){
athlete.jump_height = athlete.jump_height * 0.9;
} else {
athlete.jump_height = 1;
}
}

athlete.y = athlete.jump_height;
athlete.x = athlete.position;

• Hey Byte, the reason I'm changing the scale is beacuse it's a top-down view... I'll have a look at your code and come back again! Thanks a lot for taking the time to answer! – Jan Jul 7 '12 at 19:01
• OK, top-down makes more sense. I guess with different art that would be clearer. You're doing things right though. Just try to keep your code cleaner and you'll have an easier time finding when you do have a problem. Good luck! – MichaelHouse Jul 7 '12 at 19:26

It looks like I was using a wrong algorithm to calculate the jump and falling down. It was doing it in a linear fashion, and using the scale was not helping to find it out. After looking at some sites and much thought this is what I came up with the following code for a side view and is working great. It would have to be changed a bit for a top-down view, but now it's doing the jump much more fluently.

You can see it working here! http://testing.fyrastudio.com/lab/tweetOlympics/v0.003/ Thanks!

                if(timeCurrent - athlete.last_acceleration > athlete.delay_acceleration)
{
if (athlete.accelerating) {
athlete.accelerating = false;
athlete.last_acceleration = timeCurrent;
athlete.running = true;
} else {
athlete.decelerating = true;
}
}

if(athlete.decelerating && timeCurrent - athlete.last_deceleration > athlete.delay_deceleration){
if(athlete.speed >= 1){
athlete.last_deceleration = timeCurrent;
athlete.decelerate();
}else {
athlete.speed = 0;
athlete.running = false;
}
}

if (athlete.running) {
athlete.position += athlete.speed;
}
if (athlete.jumping) {

athlete.jump_height += athlete.jump_speed;
athlete.jump_speed -= GRAVITY;
if (athlete.jump_height <= 0) {
athlete.jump_height = 0;
athlete.jumping = false;
}
}
athlete.y = -athlete.jump_height + 300;
athlete.x = athlete.position;