I am having problems learning to move a missile up the y axis.
Now to move a sprite representing a space ship I used this:
int keycode(0);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
keycode = 1;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
keycode = 2;
switch(keycode)
{
case 1:// move left
{
position = ship.getPosition();
if(position.x <= 0.0)
ship.setPosition(0.0,500);
else
ship.move(-0.130 * elapsed.asMilliseconds(),0);
break;
}
case 2: // move right
{
position = ship.getPosition();
if(position.x >= 700.0)
ship.setPosition(800-shipsize.width,500);
else
ship.move(0.130 * elapsed.asMilliseconds(),0);
break;
}
}
By trial & error I got it moving at the type of speed I wanted. The trouble is that I now want to give my ship a function to fire a missile. So say the window i use is 800x600 and the ship moves along the x axis only, but is set at position 500 on the y axis. - The y axis position of my ship does not change. A missile will always start at position 500 on the y axis and go up towards the top of the window. (Note that the xy axis in sfml is flipped so 0,0 is the top left corner of window.)
I have been trying to build a test program that moves a missile up the y axis once the left-shift button is pressed. It should work without a ship.
This code does make missile move - but it does not do what I want it to. Note that once i have working code i will turn this into a class & remove magic numbers etc. For simplicity, i'm not worried about doing that at the moment :
// an implementation of a missile.
#include <iostream>
#include<SFML/Graphics.hpp>
#include<SFML/System.hpp>
int main()
{
sf::RenderWindow mywindow(sf::VideoMode(800, 600, 32), "missile testing");
mywindow.setVerticalSyncEnabled(true);
// create sprite
sf::Texture missilepic;
missilepic.loadFromFile("/home/game/images/missile.png");
sf::Sprite missile;
missile.setTexture(missilepic);
missile.setPosition(400,500);
sf::Time elapsed;
sf::Clock clock;
bool running = true;
while(mywindow.isOpen())
{
elapsed = clock.getElapsedTime();
clock.restart();
sf::Event event;
while (mywindow.pollEvent(event))
{
if((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
mywindow.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))
{ // code to move missile
missile.move(0, -2 * elapsed.asSeconds());
}
mywindow.clear();
mywindow.draw(missile);
mywindow.display();
}
return EXIT_SUCCESS;
}//main
problem 1. Unless I keep the l-shift key pressed, the missile will only move a short distance. I need it to move from it's origin on y-axis at 500 right up to 0 - with 1 key press. I think a loop will be needed but with what parameters?
problem 2. Adjusting speed. at first i experimented with the code that i used to move the ship left/right. But this does not work for the missile as that code depends on the left or right key remaining pressed. If i use the same settings, the missile moves too fast. So how can i make it move at a slower rate? What is the code to use?
I asked for help on another forum, but the answer i had was wrong as the person thought that the missile movement code was part of the event - polling code - it isn't as it appears outside of that loop. Anyway, i am mixed up about this as i cannot see how to move the missile up the y axis at a controlled speed (that can be changed easily).