Problem using SFML to move a sprite "missile".

Hi, im using sfml 2.0 on linux to make a space invaders game.

I can see how to move a sfml sprite using a real-time input. For example, to control a ship - I can move it left or right by this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
	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 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// 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?
Your missile moving code is inside the event handler that checks whether LShift is pressed.

Use a bool and set it to true whenever LShift is pressed. At each iteration, check if this bool is true. If this bool is true, call missile.move. Inside this if-statement you can also add a counter and perform simple arithmetic to accelerate the missile.
Hey - thanks!
Topic archived. No new replies allowed.