SFML community forums
Help => General => Topic started by: ravenheart on September 26, 2008, 07:50:42 pm
-
so, why doesnt this code not work, i think about the movement - Input
int main()
{
// Create main window
RenderWindow App;
App.Create(sf::VideoMode(WINDOW_WIDTH,WINDOW_HEIGHT,32), "SFML Window", Style::Fullscreen);
// Cursor is not being shown
ShowCursor(0);
// create bats
bat left;
bat right;
right.SetPosition(WINDOW_WIDTH - right.width,0);
// INput
const Input& Input = App.GetInput();
bool left_up = Input.IsKeyDown(Key::W);
bool left_down = Input.IsKeyDown(Key::S);
bool right_up = Input.IsKeyDown(Key::Up);
bool right_down = Input.IsKeyDown(Key::Down);
// Geschwindigkeiten
while (App.IsOpened())
{
// Process events
Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == Event.Closed)
App.Close();
// keybord-events
if ((Event.Type == Event.KeyPressed) && (Event.Key.Code == Key::Escape))
{
App.Close();
}
}
// handle bat-movements
if( right_down)
{
right.Move(0, 400* App.GetFrameTime());
}
if(right_down)
{
right.Move(0, - 400 * App.GetFrameTime());
}
if(left_down)
{
left.Move(0, 400* App.GetFrameTime());
}
if(left_up)
{
left.Move(0, -400* App.GetFrameTime());
}
// Draw left paddle;
App.Draw(left);
App.Draw(right);
// Finally, display the rendered frame on screen
App.Display();
}
my bats dont move, when i write my input right above the if-clauses everything works fine, why?
does App.GetInput() needs to be refreshed every frame?
-
Your bats don't move because you're only checking input once at the beginning of the program.
All of this:
const Input& Input = App.GetInput();
bool left_up = Input.IsKeyDown(Key::W);
bool left_down = Input.IsKeyDown(Key::S);
bool right_up = Input.IsKeyDown(Key::Up);
bool right_down = Input.IsKeyDown(Key::Down);
Needs to be put in your main loop somewhere. Also, you have right_down twice in your bat movement part of the loop.
-
so, why doesnt this code not work, i think about the movement - Input
I'm not sure, but just dividing by 0 or something should be a quick fix.
-
Sorry... :p Ok, real answer:
Where you're setting the keypress bools is not in your main loop. You need to move them there. Think about how your program is flowing. Also, did you try using the debugger?
-
ok i found it out by myself what i dont like is that i have to declare my bools every loop from new;
is there any possiblitiy to change that?
-
Declare them once at the beginning and then just re-assign them once each frame.