Sign in to follow this  
LAURENT*

Having problems with scrolling platform

Recommended Posts

I applied gravity, time, and velocity but my character isn't interacting with a  certain platform how I want him to. Here is my header file and c++ file. Thank god I  don't allow certain variables to be used in certain function otherwise this would be a huger mess.

 

 

Anyways my first idea to stop the character from falling through the platforms was to make velocity = 0 and the y coordinate equal to the  correct spot. Later on I discovered when the platform moves down the character doesn't go with the platform but simply falls to the platform over and over again as it's going down. I don't have a problem going up, left, and right with the platform only down. Oh yeah this game isn't a traditional 2D platformer but I need you to pretend like it is because its logic depends on it. You may have noticed the z variable, well I have only one word to say "isometric" but it logic still depends on the 2D mindset.

 

 

 

 

CPP file

#include <stdlib.h>
#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>
#include <time.h>
#include "tictactoe.h"

void Game::showMovement(int x, int y, SDL_Surface* screen, SDL_Surface* source, SDL_Rect* clip = NULL)
{
	//Holds offsets
    SDL_Rect offset;

    //Get offsets
    offset.x = x;
    offset.y = y;

    //Blit
    SDL_BlitSurface( source, clip, screen, &offset);
}


void Game::showCharacterMovement(int fakeX, int z, SDL_Surface* screen, SDL_Surface* source, SDL_Rect* clip = NULL)
{
	//Holds offsets
    SDL_Rect offset;

    //Get offsets
    offset.x = fakeX;
    offset.y = z;

    //Blit
    SDL_BlitSurface( source, clip, screen, &offset);
}


void Game::hub(SDL_Surface *screen)
{
	bool quit = false;
	//While the user hasn't quit
    
	while( quit == false )
    {
        //While there's events to handle
        while( SDL_PollEvent( &event ) )
        {
            //Handle events for the dot
            buttonInput();

            //If the user has Xed out the window
            if( event.type == SDL_QUIT )
            {
                //Quit the program
                quit = true;
				break;
            }
        }
	
		move( get_ticks() );
		start();
		SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));


		showMovement((int) x, (int)y, screen, LandPad );
		showCharacterMovement((int) fakeX, (int) z, screen, Ysguy1);
		SDL_Flip( screen );
	} 
}


void Game::buttonInput()
{		
	//If a key was pressed
    if( event.type == SDL_KEYDOWN )
    {
        //Adjust the velocity
        switch( event.key.keysym.sym )
        {
            case SDLK_UP: yVel -= YS_VEL_WALK; zVel -= YS_VEL_WALK;  break;
            case SDLK_DOWN: yVel += YS_VEL_WALK; zVel += YS_VEL_WALK;  break;
            case SDLK_LEFT: xVel -= YS_VEL_WALK; break;
            case SDLK_RIGHT: xVel += YS_VEL_WALK; break;
			case SDLK_SPACE: if (grounded == true) {zVel -= YS_VEL_WALK + 20; }  break; 
        }
    }
    //If a key was released
    else if( event.type == SDL_KEYUP )
    {
        //Adjust the velocity
        switch( event.key.keysym.sym )
        {
            case SDLK_UP: yVel += YS_VEL_WALK; zVel += YS_VEL_WALK; break;
            case SDLK_DOWN: yVel -= YS_VEL_WALK; zVel -= YS_VEL_WALK; break;
            case SDLK_LEFT: xVel += YS_VEL_WALK; break;
            case SDLK_RIGHT: xVel -= YS_VEL_WALK; break;	
        }
    }
}


void Game::move( Uint32 deltaTicks )
{	
	landpadCoords.x = x;
	landpadCoords.y = y;
	landpadCoords.w = x + LANDPAD_WIDTH;
	landpadCoords.h = y + LANDPAD_HEIGHT;
	
	
	
	fakeX = x; 
    //Move the dot left or right
    x += xVel * ( deltaTicks / 1000.f );

    //If the dot went too far to the left
    if( x < 0 )
    {
        //Move back
        x = 0;
    }
    //or the right
    else if( x + YS_WIDTH > SCREEN_WIDTH)
    {
        //Move back
        x = SCREEN_WIDTH - YS_WIDTH;
    }

    //Move the dot up or down
    y += yVel * ( deltaTicks / 1000.f );

    //If the dot went too far up
    if( y < 0 )
    {
        //Move back
        y = 0;
    }
    //or down
    else if( y + YS_HEIGHT > SCREEN_HEIGHT)
    {
        //Move back
        y = SCREEN_HEIGHT - YS_HEIGHT;
    }

	zVel += ACCELARATION * ( deltaTicks / 1000.f );
	z += zVel * ( deltaTicks / 1000.f );
	
	
	if( z + YS_HEIGHT > y )
	{
		grounded = true;		
	
		if (grounded == true)
		{	zVel-= zVel; 
		
		}
	}
		
}


Header file

class Game
{	 
	public:	
		//Game();

		Game::Game()
		{
			circle = IMG_Load("circle.jpg");  
			croix = IMG_Load("croix.jpg"); 
			board = IMG_Load("board.jpg"); 
			horizontale = IMG_Load("horizontale.jpg");
			verticale = IMG_Load("verticale.jpg");
			diagonale1 = IMG_Load("diagonale1.png");
			diagonale2 = IMG_Load("diagonale2.png");
			button = IMG_Load("button.png");
			
			// Characters
			Ysguy1= IMG_Load("Ysguy1.png");
			LandPad = IMG_Load("LandPad.png");
			//Timer variables and functionality
			startTicks = 0;
			pausedTicks = 0;
			paused = false;
			started = false;

			//Const numbers that should never change
			 YS_VEL_WALK = 100;
			 YS_HEIGHT = 25;
			 YS_WIDTH = 19;
		     SCREEN_WIDTH = 589;
			 SCREEN_HEIGHT = 404;
			 ACCELARATION = 300;
			 LANDPAD_WIDTH = 19;
			 LANDPAD_HEIGHT = 7;
		};

		void meun (SDL_Surface* screen);
		void characters(SDL_Surface* screen);
		void play(SDL_Surface* screen); //F1
		int check_victory(SDL_Surface* screen); //F2	
		/***************************************************************************************/
		/////////////////////////////////////////////////////////////////////////////////////////
		////////////////////////////////MOVE PUBLIC FUNCTIONS///////////////////////////////////
		/////////////////////////////////////////////////////////////////////////////////////////
		/***************************************************************************************/
		bool collision(SDL_Rect A);
		void buttonInput();
		void hub(SDL_Surface* screen);
		void move (Uint32 deltaTicks);
		void showMovement(int x, int y, SDL_Surface* screen, SDL_Surface* source, SDL_Rect* clip);
		void showCharacterMovement(int x, int z, SDL_Surface* screen, SDL_Surface* source, SDL_Rect* clip);
		/***************************************************************************************/
		/////////////////////////////////////////////////////////////////////////////////////////
		////////////////////////////////TIMER PUBLIC FUNCTIONS///////////////////////////////////
		/////////////////////////////////////////////////////////////////////////////////////////
		/***************************************************************************************/
		void start();
		void stop();
		void pause();
		void unpause();

		//Gets the timer's time
		int get_ticks();

		//Checks the status of the timer
		bool is_started();
		bool is_paused();
			
		Game::~Game()
		{
			SDL_FreeSurface(horizontale);
			SDL_FreeSurface(verticale);
			SDL_FreeSurface(diagonale1);
			SDL_FreeSurface(diagonale2);
			SDL_FreeSurface(croix);
			SDL_FreeSurface(circle);
			SDL_FreeSurface(button);
			SDL_FreeSurface(board);	
		};

		//~Game();
		


	private:
		SDL_Event event;
		SDL_Surface *board, *circle, *croix, *horizontale, *verticale, *diagonale1, *diagonale2, *button, *Ysguy1, *LandPad;
		enum {EMPTY, CIRCLE, CROSS};
		int meunSelect;
		int continuer,  turn,  end, nowinner, xCoordinate, yCoordinate;
		int space1, space2, space3, space4, space5, space6, space7, space8, space9;  
		SDL_Rect boardPosition, positionforme, positionwon;
		SDL_Rect button1, button2, button3, button4, button5, button6, button7, button8, button9;
		bool quit;
		/***************************************************************************************/
		/////////////////////////////////////////////////////////////////////////////////////////
		////////////////////////////////TIMER PRIVATE FUNCTIONS//////////////////////////////////
		/////////////////////////////////////////////////////////////////////////////////////////
		/***************************************************************************************/
		//The clock time when the timer started
		int startTicks;

		//The ticks stored when the timer was paused
		int pausedTicks;

	    //The timer status
		bool paused;
		bool started;

		/***************************************************************************************/
		/////////////////////////////////////////////////////////////////////////////////////////
		////////////////////////////////MOVER PRIVATE FUNCTIONS//////////////////////////////////
		/////////////////////////////////////////////////////////////////////////////////////////
		/***************************************************************************************/
		float x, y, z;
		float fakeX;
		//The velocity of the dot
		float xVel, yVel, zVel;
		float ACCELARATION;
		int YS_VEL_WALK;
		int YS_HEIGHT;
		int YS_WIDTH;
		int SCREEN_WIDTH;
		int SCREEN_HEIGHT;
		int LANDPAD_WIDTH;
		int LANDPAD_HEIGHT;
		int leftA, leftB;
		int rightA, rightB;
		int topA, topB;
		int bottomA, bottomB;

		bool grounded;
		
		SDL_Rect landpadCoords;
		
};


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this