[SOLVED] Problem with smooth camera movement

S

Seth Davis

Guest
I am working on a simple way to smoothly move between screens in my game. I want to have an object, that when clicked, will smoothly transition the camera from one side to the other. However, with my current code, it instead jerks the camera to the other side instantly, with no smooth transition. It looks correct to me, if anyone could tell me what is wrong with it, that would be fantastic!

Code:
if (clicked())
{
    if (side == 0)
    {
        side = 2;
        while (obj_camera.x < 1536) obj_camera.x += 0.001;
        side = 1;
    }
    else if (side == 1)
    {
        side = 2;
        while (obj_camera.x > 512) obj_camera.x -= 0.001;
        side = 0;
    }
}
side is created in the create event.
and clicked() is simply testing if it was clicked.

I also tried this code with a form of a for loop, and it did the same.
 
M

maratae

Guest
I am working on a simple way to smoothly move between screens in my game. I want to have an object, that when clicked, will smoothly transition the camera from one side to the other. However, with my current code, it instead jerks the camera to the other side instantly, with no smooth transition. It looks correct to me, if anyone could tell me what is wrong with it, that would be fantastic!

Code:
if (clicked())
{
    if (side == 0)
    {
        side = 2;
        while (obj_camera.x < 1536) obj_camera.x += 0.001;
        side = 1;
    }
    else if (side == 1)
    {
        side = 2;
        while (obj_camera.x > 512) obj_camera.x -= 0.001;
        side = 0;
    }
}
side is created in the create event.
and clicked() is simply testing if it was clicked.

I also tried this code with a form of a for loop, and it did the same.
The 'while' loop runs before the frame ends. Try using an 'if' instead, as while is not meant to be used like this.

Something like this, although i'm not sure what you want your code to do:

Code:
if (clicked())
{
    if (side == 0)
    {
        side = 2;
        if (obj_camera.x < 1536)
        {
            obj_camera.x += 0.001;
        }
        else
        {
            side = 1;
        }
    }
    else if (side == 1)
    {
        side = 2;
        if (obj_camera.x > 512)
        {
            obj_camera.x -= 0.001;
        }
        else
        {
            side = 0;
        }
    }
}
Edit: This code won't work at all because of your 'side' assignments, if you got the idea, now try and sort that part out.
 
S

Seth Davis

Guest
Solved! Thanks for the input! Here is the new code for anyone else with this specific problem:

Code:
if (clicked()) moving = true;

if (moving)
{
    if (side == 0)
    {
        if (obj_camera.x < 1536) obj_camera.x += 128;
        else
        {
            side = 1;
            moving = false;
        }
    }
    else if (side == 1)
    {
        if (obj_camera.x > 512) obj_camera.x -= 128;
        else
        {
            side = 0;
            moving = false;
        }
    }
}
 
Top