Problem with keys

I’m making a simple pong game so I made a paddle class with a bool b[2]={0,0}; (for up and down) now in the event loop i check wether up and down keys are pressed and set b accordingly, I set it when mPlayer.b[0] =1 it moves the paddle up and when mPlayer.b[1] = 1 then the paddle moves down however it does neither, ive checked to see if the paddle would move by simply setting b[0] = 1 upon init and it works so theres so the problem clearly lies within the event loop, so anybody have any thoughts?

Can you post the code of the event-loop interpreting the keydown- and keyup-events?

main loop

	CPaddle  mPlayer(10, 10, 10, 10, 0xff, 0xff, 0xff);
	

	// loop
	bool Running = 1;
	const int FPS = 30;
	Uint32 Start;

	while(Running)
	{
		Start = SDL_GetTicks();
		while(SDL_PollEvent(&event))
		{
			
			if(event.type == SDL_QUIT) 
				Running = 0;
			switch(event.type)
			{
			case SDL_KEYUP:
				switch(event.key.keysym.sym)
				{
				case SDLK_UP:
					mPlayer.mMove(2);
					break;
				case SDLK_DOWN:
					mPlayer.mMove(-2);
					break;
				}
				break;
			case SDL_KEYDOWN:
				{
					switch(event.key.keysym.sym)
					{
					case SDLK_UP:
						mPlayer.mMove(0);
						break;
					case SDLK_DOWN:
						mPlayer.mMove(0);
						break;
					}
				}
			}
		
		}

		// logic
		mPlayer.mLogic();
		
		// Render
		mPlayer.mRender(Screen);
		// FPS
		SDL_Flip(Screen);
		if(1000/FPS>SDL_GetTicks() - Start)
			SDL_Delay(1000/FPS-(SDL_GetTicks()-Start));
	}

CPaddle.h :

// CPaddle.h
#include <SDL.h>
#include "CSurface.h"

#ifndef _CPADDLE_H_
#define _CPADDLE_H_

class CPaddle: public CSurface
{
public:
	//constructor / destructor
	CPaddle();
	CPaddle(int X, int Y, int W, int H, int R, int G, int B);
	~CPaddle();
	void mMove(int Speed);
	void mLogic();
	void mRender(SDL_Surface* Dest);
	

	
private:
	int mX;
	int mY;
	int mW;
	int mH;
	int mSpeed;
	int mR;
	int mG;
	int mB;
	Uint32 mColor;
};

#endif

CPaddle.cpp :

// CPaddle.cpp
#include "CPaddle.h"

CPaddle::CPaddle()
{
	mX = 30;
	mY = 240;
	mW = 20;
    mH = 40;
	mR = 0xff;
	mG = 0xff;
	mB = 0xff;
}

CPaddle::CPaddle(int X, int Y, int W, int H, int R, int G, int B)
{
	mX = X;
	mY = Y;
	mW = W;
	mH = H;
	mR = R;
	mG = G;
	mB = B;
}

CPaddle::~CPaddle()
{
	mX = NULL;
	mY = NULL;
	mW = NULL;
	mH = NULL;
	mR = NULL;
	mG = NULL;
	mB = NULL;
}

void CPaddle::mMove(int Speed)
{
	mSpeed = Speed;
}

void CPaddle::mLogic()
{
	mY += mSpeed;
}

void CPaddle::mRender(SDL_Surface* Dest)
{
	mColor = SDL_MapRGB(Dest->format, mR, mG, mB);
	CSurface::OnFill(Dest, mX, mY, mX+mW, mY+mH, mColor); 
}

the above code is a little off its this
main loop:

CPaddle  mPlayer(10, 10, 10, 10, 0xff, 0xff, 0xff);
	

	// loop
	bool Running = 1;
	const int FPS = 30;
	Uint32 Start;

	while(Running)
	{
		Start = SDL_GetTicks();
		while(SDL_PollEvent(&event))
		{
			
			if(event.type == SDL_QUIT) 
				Running = 0;
			switch(event.type)
			{
			case SDL_KEYUP:
				switch(event.key.keysym.sym)
				{
				case SDLK_UP:
					mPlayer.b[0] = 1;
					break;
				case SDLK_DOWN:
					mPlayer.b[1] = 1;
					break;
				}
				break;
			case SDL_KEYDOWN:
				{
					switch(event.key.keysym.sym)
					{
					case SDLK_UP:
						mPlayer.b[0] = 0;
						break;
					case SDLK_DOWN:
						mPlayer.b[0] = 0;
						break;
					}
				}
				break;
			}
		
		}

		// logic
		mPlayer.mLogic();
		
		// Render
		mPlayer.mRender(Screen);
		// FPS
		SDL_Flip(Screen);
		if(1000/FPS>SDL_GetTicks() - Start)
			SDL_Delay(1000/FPS-(SDL_GetTicks()-Start));
	}

CPaddle.h :

// CPaddle.h
#include <SDL.h>
#include "CSurface.h"

#ifndef _CPADDLE_H_
#define _CPADDLE_H_

class CPaddle: public CSurface
{
public:
	//constructor / destructor
	CPaddle();
	CPaddle(int X, int Y, int W, int H, int R, int G, int B);
	~CPaddle();
	void mMove(int Speed);
	void mLogic();
	void mRender(SDL_Surface* Dest);
	bool b[2];
	

	
private:
	int mX;
	int mY;
	int mW;
	int mH;
	int mSpeed;
	int mR;
	int mG;
	int mB;
	Uint32 mColor;
};

#endif

CPaddle.cpp :

// CPaddle.h
#include <SDL.h>
#include "CSurface.h"

#ifndef _CPADDLE_H_
#define _CPADDLE_H_

class CPaddle: public CSurface
{
public:
	//constructor / destructor
	CPaddle();
	CPaddle(int X, int Y, int W, int H, int R, int G, int B);
	~CPaddle();
	void mMove(int Speed);
	void mLogic();
	void mRender(SDL_Surface* Dest);
	bool b[2];
	

	
private:
	int mX;
	int mY;
	int mW;
	int mH;
	int mSpeed;
	int mR;
	int mG;
	int mB;
	Uint32 mColor;
};

#endif

Well, your main loop handling keydown and keyup seems correct to me (but I didn’t tested it at all). But I would prefer another approch for moving your CPaddle:

Your CPaddle uses a speed variable. That’s good! But in my opinion it doesn’t make sense to implement “mMove(int speed)” in that way and change the speed all the time. A better solution might be “mMove(int dx, int dy)” for moving in direction <delta x, delta y> relative to your current position (mX and mY seem to the current position). You might store dx and dy in seperate values, such as mdX, mdY or so. Try to use mMove only to set the direction (which is a vector with x and y). Then you can use nLogic to calculate your new position by mX = mX + mdX * mSpeed; and the same way for y. So you just need to set the direction after getting your keydown/keyup.

Actual you are only using y-direction (arrow keys up and down). So you can use mMove(0, 1) for moving down and mMove(0, -1) for moving up. If you need moving in x-direction you can simple extend it by using the first parameter of mMove.

I hope this is helpful. Don’t hesitate to ask furthermore.

Kind regards
Glocke[/code]