Skip to main content
gamedev.net
🔒 Locked

return object

Started by beginnerfurqan 2 replies 600+ views
Original Post
beginnerfurqan
beginnerfurqan
float d = +2.0f, e = -10.0f, f; .... void myIdle() { ..... e += 0.2; } Using the above code I have made an object move upwards. How do I make the object go back down the way it came up(i dont mean wrapping)? I have tried the following but doesnt work. if (e >= 10.0f) { e = 10.0f; e -= 2.0; } Can anyone help?
6
6
EDIT: The poster above just got in while I was writing, the same code though

The if statement block code won't work because e -= 2 will only happen when e>=10, which i guess isn't what you want it to do.

For simplicity, you could do this

Set up a global bool called movingUp and set it to true.

Write a an if else to test to see your object is at the bottom or top of the screen

if(e >= 10) //at the top
movingUp = FALSE
if(e <= 0) //at the bottom
movingUp = TRUE

Then write an if else to move your object such as:

if(movingup)
e += 0.2;
else
e -=2;
----------------------------------------------------
Check out my casual TBS game blog
----------------------------------------------------
Rob Loach
Rob Loach
float e = 0.0f; // The bodyfloat velocity = 4f; // The speed of the body.//...void myIdle(){    // Move the body    e += velocity;    // Add resistance    velocity -= 0.1f;        // Bounce off the ground    if(e < 0f)        e *= -1f;}


Implementing the bodies velocity will greatly make your life easier. If you run the above example, you'll see that e is thrown up in the air, and then peaks and slowly comes back down, much like gravity would act on a flying ball.
Rob Loach [Website] [Projects] [

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.