• Advertisement
Sign in to follow this  

Second entity moves really slow!

This topic is 1843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everybody.

I'm trying to learn how to make a simple game with SDL using mainly LazyFoo's tutorials, and I have a problem I have know idea how to solve.

 

When I move the player around with nothing else on the screen except the tiles, the player moves fine.

But when I add another entity ( an enemy ), the ntity that calls its moving function last moves really slowly.

So, if I call the player's moving function first, the enemy moves slowly, and if I call the enemy's moving function first, the player moves slowly.

 

This is the code:

 

Player movement:

 

void player::control()
{
    if( event.type == SDL_KEYDOWN )
    {
        switch( event.key.keysym.sym )
        {
            case SDLK_d:
            xvel += VELOCITY;
            break;

            case SDLK_a:
            xvel -= VELOCITY;
            break;

            default: {}
        }
    }

    if( event.type == SDL_KEYUP )
    {
        switch( event.key.keysym.sym )
        {
            case SDLK_d:
            xvel -= VELOCITY;
            break;

            case SDLK_a:
            xvel += VELOCITY;
            break;

            default: {}
        }
    }
}

void player::jump()
{
    Uint8 * keystate = SDL_GetKeyState( NULL );

    if( keystate[ SDLK_SPACE ] && jumping == false )
    {
        jumping = true;
        yvel = -( VELOCITY * 3 );
    }
}

void player::move( Uint32 delta_ticks , tile * tiles[] )
{
    double lastx = box.x;
    double lasty = box.y;

    box.x += xvel * ( delta_ticks / 1000.f );

    if( box.x < 0 ) box.x = 0;
    if( box.x + PLAYER_WIDTH > LEVEL_WIDTH ) box.x = LEVEL_WIDTH - PLAYER_WIDTH;

    if( collision( box , tiles ) ) box.x = lastx;

    box.y += yvel * ( delta_ticks / 1000.f );

    if( collision( box , tiles ) )
    {
        if( yvel >= 0 )
        {
            box.y = lasty;
            jumping = false;
        }
        else
        {
            box.y = lasty;
            jumping = true;
        }

        yvel = 0;
    }
    else jumping = true;

    yvel += gravity;

    if( yvel >= VELOCITY * 8 ) yvel = VELOCITY * 8;

    if( box.y > LEVEL_HEIGHT + 50 )
    {
        box.y = 0;
        box.x = 50;
        status = FACING_RIGHT;
        yvel = 0;
    }

    x = box.x;
    y = box.y;
}

 

Enemy movement ( the enemy is a completely different class than the player.):

 

void enemy::move_enemy( Uint32 delta_ticks , tile * tiles[] )
{
    double lastx = box.x;
    double lasty = box.y;

    xvel = -VELOCITY;

    box.x += xvel * ( delta_ticks / 1000.f );

    if( collision( box , tiles ) )
    {
        box.x = lastx;
        xvel = -xvel;
    }

    box.y += yvel * ( delta_ticks / 1000.f );

    if( collision( box , tiles ) )
    {
        box.y = lasty;
        yvel = 0;
    }

    yvel += gravity;

    if( yvel >= VELOCITY * 8 ) yvel = VELOCITY * 8;

    x = box.x;
    y = box.y;
}

 

The main function:

 

bool quit = false;
    bool tileedit = false;

    if( init() == false ) return 1;
    if( load_files() == false ) return 2;

    timer delta;
    player player1( 50 , 50 );
    enemy enemy1( 600 , 100 , BUBBLE );
    tile * tiles[ TOTAL_TILES ];

    if( set_tiles( tiles ) == false ) return 4;

    delta.start();

    while( quit == false )
    {
        scrolling_background();
        if( tileedit ) editor_show( current_tile );

        while( SDL_PollEvent( &event ) )
        {
            if( event.type == SDL_KEYDOWN )
            {
                if( event.key.keysym.sym == SDLK_t ) tileedit = !tileedit;
            }

            if( event.type == SDL_QUIT ) quit = true;

            if( tileedit == false ) player1.control();
            else if( tileedit ) editor_control( tiles );
        }

        if( tileedit == false )
        {
            player1.jump();
            scrolling_background();
            player1.move( delta.get_ticks() , tiles );
            enemy1.move_enemy( delta.get_ticks() , tiles );
            player1.player_camera();
        }
        else editor_camera();

        for( int t = 0 ; t < TOTAL_TILES ; t++ )
        {
            tiles[t]->show();
        }

        if( tileedit == false ) player1.show();

        enemy1.show();

        if( SDL_Flip( screen ) == -1 ) return 5;
    }

    editor_save( tiles );
    cleanup( tiles );

    return 0;

 

Could it be that I don't use frame independent movement correctly? I have no idea, and I'm very new to programming in general, so if someone more experienced could help me understand where is the problem, I would really appreciate it.

 

Thank you in advance.

Edited by Kain5056

Share this post


Link to post
Share on other sites
Advertisement

It works perfectly! Thank you so much for the help! smile.png

 

Here is the new game loop:

 

while( quit == false )
    {
        Uint32 ticks = delta.get_ticks();

        scrolling_background();
        if( tileedit ) editor_show( current_tile );

        while( SDL_PollEvent( &event ) )
        {
            if( event.type == SDL_KEYDOWN )
            {
                if( event.key.keysym.sym == SDLK_t ) tileedit = !tileedit;
            }

            if( event.type == SDL_QUIT ) quit = true;

            if( tileedit == false ) player1.control();
            else if( tileedit ) editor_control( tiles );
        }

        if( tileedit == false )
        {
            player1.jump();
            scrolling_background();
            player1.move( ticks , tiles );
            enemy1.move_enemy( ticks , tiles );
            player1.player_camera();
        }
        else editor_camera();

        for( int t = 0 ; t < TOTAL_TILES ; t++ )
        {
            tiles[t]->show();
        }

        if( tileedit == false ) player1.show();

        enemy1.show();

        if( SDL_Flip( screen ) == -1 ) return 5;
    }

 

 

Thanks again. smile.png

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement