- A lot of initial event handling code is in your Ship class, whereas all of it should be in the pollEvent() loop. It's common for that event handling to involve calling methods on Ship, or set variables that are later result in calls to Ship methods, but Ship itself should not be the one checking which mouse buttons or keys are being pressed. That's coupling your Ship entity far too tightly to your game loop/input logic. Specifically, your Ship really doesn't need direct access to your Window and Event objects (except in the draw() method where it needs a window to draw itself on).
- You were mixing event-based input and real-time input, which is never good because those represent two fundamentally different ways of handling input that don't make sense together. You'll see a lot of other threads on this forum where we tell newbies not to do that. If you don't know what that means, I'm specifically referring to one of the Ship methods doing this:
if( (event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::M) ) { // event-based input
}
if( sf::Mouse::isButtonPressed(sf::Mouse::Left) ) { // real-time input
}
What you want to do is have a pollEvent() loop that deals with all of the pending events (remember there may be several), and then move on to any real-time tests you need afterward.
// event-based input
while (window.pollEvent(event)) {
if( (event.type == sf::Event::KeyPressed) {
} else // other types of events
}
// then real-time input, if needed
if( sf::Mouse::isButtonPressed(sf::Mouse::Left) ) { ... }
- You were using real-time input (isButtonPressed) to determine if the player's Ship was selected or not. You almost certainly want to use event-based input if the goal is to select something in reaction to a click. With real-time input you may miss clicks (if one frame takes too long), react to a single click multiple times (if it takes multiple frames), or any number of other strange issues.
- Arguably the most serious: Your movement logic doesn't appear to contain any attempt at calculating how much the Ship should move
during this one frame (it just calls setPosition() on the final destination) and yet you were expressing surprise at the fact that "it just teleports there instantly", which indicates a serious conceptual issue of some kind. I figured showing you an update() method that handled this properly would be the fastest way to clear up the confusion.
Those are the really big ones anyway.