[quote name='Serapth' timestamp='1311620372' post='4840127']
[quote name='XXChester' timestamp='1311619888' post='4840121']
Try changing your second if to an else if. I have never used SDL but from what I can see if you have a key down and than release it the events will cancel as it is an if statement and not an else if.
That wont have an effect, as the event will only return a result once per call, AKA, it's not going to change during the processing of that code.
However, you are onto what I think the problem is. In the event loop you are increasing the velocity on key down, but then decrementing velocity on key up. That means on a key press ( an down and up event ) you are immediately increasing the velocity, then in the very next event literally milliseconds away, you are descreasing velocity.
So, in the event of a keypress, the following is happening.
velocity set to 0
key down, velocity increased by 5
milliseconds later, next event in loop is keyup event, velocity reduced back to 0.
This is most likely not the logic you actually want. That said, in the event of a key held down, your sprite should move. In this case, without seeing more code ( specifically setVel() and move() ) there is no way to know eactly where the problem is.
[/quote]
Basically what you stated is exactly what I was thinking about the events canceling each other but I am not sure why I thought the else if would fix that.
As for the problem Crazy, you are probably still having the same problem even though they are split into separate functions because the way you are handling the events is probably the same...on key down; do this, on key up; do this. You may want to post how you are calling these functions. Also a little background on the type of game might help, reading the code I am assuming it is a sort of racing game or space game and that is why you have -velocity calculations on key up opposed to just stopping the sprite all together, correct?
[/quote]
Allright. I did some thinking and I figure that the else if is unnecessary so I commented it away to try and see what happened.
RIght now what happens everytime I press a key I see that it changes the xVel and yVel in setVel() and changes are made to the values affected in move() but when the values are taken to the bliting method they are the
of the dafault value.
If that makes any sense.
I'll just post the classes that is involved in the movement:
GameEngine.cpp:
#include "SDL.h"
#include "GameEngine.h"
#include "Image.h"
#include "Sprite.h"
#include <vector>
#include <string>
#include <iostream>
GameEngine::GameEngine(int h, int w)
{
SCREEN_HEIGHT = h;
SCREEN_WIDTH = w;
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(SCREEN_HEIGHT, SCREEN_WIDTH, 32, SDL_SWSURFACE | SDL_DOUBLEBUF);
SDL_ShowCursor(SDL_DISABLE);
}
void GameEngine::log(std:: string log)
{
std::cout << log.c_str() << std::endl;
}
void GameEngine::update()
{
//Update screen and rect
SDL_Flip(screen);
SDL_UpdateRect(screen, 0, 0, 0, 0);
}
void GameEngine::fill()
{
SDL_FillRect(getScreen(), NULL, 0x000000);
}
void GameEngine::blit(SDL_Surface * i, SDL_Rect rect)
{
SDL_BlitSurface(i, NULL, screen, &rect);
}
void GameEngine::exit()
{
SDL_Quit();
}
bool GameEngine::events()
{
std::cout << spriteField.size() << std::endl;
int xVel = 0;
int yVel = 0;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
exit();
return true;
}
if(event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_UP:
yVel -= 5;
break;
case SDLK_DOWN:
yVel += 5;
break;
case SDLK_RIGHT:
xVel -= 5;
break;
case SDLK_LEFT:
xVel += 5;
break;
}
}
/*else if(event.type == SDL_KEYUP)
{
switch(event.key.keysym.sym)
{
case SDLK_UP:
yVel += 5;
break;
case SDLK_DOWN:
yVel -= 5;
break;
case SDLK_RIGHT:
xVel += 5;
break;
case SDLK_LEFT:
xVel -= 5;
break;
}
}*/
for(int i = 0; i < spriteField.size(); i++)
{
spriteField.setVel(xVel, yVel);
spriteField.move();
}
}
return false;
}
int GameEngine::checkFPS()
{
const int tickInterval = 1000/30;
nextTick = SDL_GetTicks() + tickInterval;
delay = nextTick - SDL_GetTicks();
return delay;
}
void GameEngine::delayProgram(int delay)
{
if(delay > 0)
SDL_Delay(delay);
}
SDL_Surface * GameEngine::getScreen()
{
return screen;
}
void GameEngine::addSprite(Sprite s)
{
spriteField.push_back(s);
}
SDL_Rect GameEngine::createTempRect(int x, int y)
{
SDL_Rect rectTemp;
rectTemp.x = x;
rectTemp.y = y;
return rectTemp;
}
Sprite.cpp:
Sprite::Sprite(int xv, int yv, bool b)
{
xVel = 0;
yVel = 0;
player = b;
x = xv;
y = yv;
rect.setRect(x, y);
w = rect.getW();
h = rect.getH();
}
void Sprite::setImage(Image img)
{
i = img;
}
Image Sprite::getImage()
{
return i;
}
void Sprite::tick(std::vector<Sprite> spriteField)
{
for(int i = 0; i < spriteField.size(); i++)
collision(spriteField);
}
void Sprite::setVel(int xv, int yv)
{
xVel += xv;
yVel += yv;
std::cout << "setVel" << x << " " << y << std::endl;
}
void Sprite::move()
{
x += xVel;
y += yVel;
std::cout << "move is" << x << " " << y << std::endl;
}
bool Sprite::collision(Sprite other)
{
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
//Calculate the sides of rect A
leftA = x;
rightA = x + w;
topA = y;
bottomA = y + h;
//Calculate the sides of rect B
leftB = other.x;
rightB = other.x + other.w;
topB = other.y;
bottomB = other.y + other.h;
//If any of the sides from A are outside of B
if(bottomA < topB)
{
return false;
}
if(topA > bottomB)
{
return false;
}
if(rightA < leftB)
{
return false;
}
if(leftA > rightB)
{
return false;
}
//If none of the sides from A are outside B
return true;
}
void Sprite::log(std::string log)
{
std::cout << log << std::endl;
}
Rect Sprite::getRect()
{
return rect;
}
int Sprite::getX()
{
return x;
}
int Sprite::getY()
{
return y;
}
and the main.cpp:
int main(int argc, char * args[])
{
GameEngine g(1000, 800);
bool quit = false;
//Create images.
Image i(true, "pong_player.bmp");
Image background(false, "pong_middle.bmp");
//Sprite
Sprite s1(150, 200, true);
s1.setImage(i);
g.addSprite(s1);
while(quit == false)
{
int delay = g.checkFPS();
//Fill screen with black
g.fill();
g.blit(s1.getImage().getSurface(), s1.getRect().get());
//Blit the background
g.blit(background.getSurface(), g.createTempRect(500, 0));
//Update the screen
g.update();
//Check for events
quit = g.events();
g.delayProgram(delay);
}
g.exit();
return 0;
}
And I'm not trying to make a particularly sofisticated game, I'm trying to make a reversed version of Pong where you try to avoid the ball instead of hitting it.