Can't Get My Bullets to move

the bullet sprite only remain in the area i set it when i run my program... i've tried a few different logics but it stays dre, i'm wondering now if setposition is an immutable entity. can anyone give me and idea of how to go about this... i've already searched in the forum, there's nothing appropriate to my needs... soon as i get my movement to work properly i will be changing my main around n using vectors to delete my bullets.. thanks for your help

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
  void Bullet::createBullet(sf::Texture &bulletTexture, sf::Sprite &bulletImage, sf::Sprite &shipImage)
{
  if(!bulletTexture.loadFromFile("Images/missile.png"))
        cout << "ERROR:bullet could not load" << endl;

    bulletImage.setTexture(bulletTexture);
    sf::Vector2f shipLocation = shipImage.getPosition();

    float locationX = shipLocation.x -83;///gets the X location of the ship places the bullet of  it
    float locationY= shipLocation.y + 25;///gets the Y loction of the ship

    bulletImage.setPosition(locationX, locationY);
}

void Bullet::drawBullet(sf::RenderWindow &myGameGUI, sf::Sprite &bulletImage)
{
    myGameGUI.draw(bulletImage);
}

void Bullet::moveBullet(sf::Sprite &bulletImage){

    sf::Vector2f bulletLocation = bulletImage.getPosition();

    float locationX = bulletLocation.x;///gets the X location of the ship places the bullet of  it
    float locationY= bulletLocation.y;///gets the Y loction of the ship

    sf::Clock clock;
    float speed = 1.f;
    float dt = 1.f/60.f;

                    for(float i = 0.0; i < locationX; i++){
                        bulletImage.move((speed * dt) + i , locationY);


                        dt = clock.restart().asSeconds();
                        bulletImage.setPosition(locationX, locationY);
                    }


    }



Thats how i'm calling it in the main

1
2
3
4
5
6
if(action.key.code == sf::Keyboard::Space){

            bulletObject.drawBullet(myGameGUI,bulletImage);

            bulletObject.moveBullet(bulletImage);
}
you should first update the position of the bullet then update your drawing.
thanks much for the reply... ok i tried what you you said in the main and in the header file with a number of different variations.. but when i press the space bar nothing happens, the sprite doesn't even appear anymore.. do you or any body else have any suggestions please?
Last edited on
you need to update your drawing after you have updated the position of your bullet.

1
2
3
4
5
6
7
8
9
for(float i = 0.0; i < locationX; i++)
{
      bulletImage.move((speed * dt) + i , locationY);
        dt = clock.restart().asSeconds();
         bulletImage.setPosition(locationX, locationY);

         // update drawing here.

                    }
thanks much for the reply.. but stil when i try it like this the bullet just remains in the ships position.. no movement.. i don't kno what to do anymore.. i saw alot of other implementations just like mines can't understand why it won't move.. is my logic correct? can you give me an idea on a better way to do this...
i have this now

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

void Bullet::createBullet(sf::Texture &bulletTexture, sf::Sprite &bulletImage, sf::Sprite &shipImage)
{
  if(!bulletTexture.loadFromFile("Images/missile.png"))
        cout << "ERROR:bullet could not load" << endl;

    bulletImage.setTexture(bulletTexture);
}

void Bullet::drawBullet(sf::RenderWindow &myGameGUI, sf::Sprite &bulletImage)
{
    myGameGUI.draw(bulletImage);
}

void Bullet::moveBullet(sf::Sprite &bulletImage, sf::Sprite &shipImage){

    sf::Vector2f shipLocation = shipImage.getPosition();


    float bullLocX = shipLocation.x;///gets the X location of the ship places the bullet of  it
    float bullLocY = shipLocation.y + 25;///gets the Y location of the ship

    sf::Clock clock;
    float speed = 1.f;
    float dt = 1.f/60.f;

                        for(int i = 0; i < bullLocX; i++)
                            {
                                bulletImage.move((speed * dt ) + i, bullLocY);


                               // bullLocX -= speed * dt;
                                //bullLocY += speed * dt;
                                dt = clock.restart().asSeconds();

                            }
                bulletImage.setPosition(bullLocX , bullLocY);



    }
seems like bullocX and bullocY doesnt change.

 
                bulletImage.setPosition(bullLocX , bullLocY);


so when you set the position it will always be on the same spot.
Last edited on
yh thats what i figured... i'm not even using a const no where in the program thinking about it... i'm getting frustrated.. do you have any alternative way of doing it
i just setup box2d and sfml on my machine.

if you paste your source code i can try to recreate your problem.
ok kool

My main

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

#include<string>
#include<iostream>
#include <SFML/Graphics.hpp>
#include <vector>
#include "ship.h"
#include "bullet.h"



using namespace std;

//declaration of variables
sf::RenderWindow myGameGUI;
sf::Event action;
sf::Texture shipTexture;
sf::Sprite shipImage;
sf::Texture backgroundTexture;
sf::Sprite backgroundImage;
sf:: Texture bulletTexture;
sf:: Sprite bulletImage;

//declaration of classes
void closeFunction();
void Background();



int main()
{

Ship shipObject;
Bullet bulletObject;
vector <Bullet> shots;

    ///Creates the Window
    myGameGUI.create(sf::VideoMode(1000,680), "WorkingGameProject");
    myGameGUI.setFramerateLimit(60);
    sf::Style::Default;

    Background();///Creates the background

    ///Displays the Background
        myGameGUI.draw(backgroundImage);

    ///calls function ship class and creates the ship
    shipObject.createShip(shipTexture, shipImage);


    while(myGameGUI.isOpen())
    {
     while(myGameGUI.pollEvent(action))
        {
        ///Calls close function and closes the Game
        closeFunction();
        }
///Calls function from ship class and Moves the ship
shipObject.moveShip(action, shipImage);
bulletObject.createBullet(bulletTexture, bulletImage, shipImage);

        myGameGUI.clear();



        ///Calls function from ship class and draws the ship
        shipObject.drawShip(myGameGUI,shipImage);

        if(action.key.code == sf::Keyboard::Space){
            //shots.pop_back(new shot);
            bulletObject.drawBullet(myGameGUI,bulletImage);

            bulletObject.moveBullet(bulletImage, shipImage);




        }

        myGameGUI.display();

    }
return 0;
}



//close fucntion
void closeFunction()
{
   if(action.type == sf::Event::Closed)///close the window when X is clicked
                myGameGUI.close();

   if (action.key.code == sf::Keyboard::Escape) ///close the window when escape key is pressed
                    myGameGUI.close();

}

///creates the Background
void Background()
{
     if(!backgroundTexture.loadFromFile("Images/spaceBackground.jpg"))
        cout << "\n\nERROR:background could not load\n\n" << endl;

    backgroundImage.setTexture(backgroundTexture);
}



My Ship Class n header

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

#ifndef SHIP_H
#define SHIP_H
#include <SFML/Graphics.hpp>



class Ship
{
    public:

        void createShip(sf::Texture &shipTexture,sf::Sprite &shipImage);
        void moveShip(sf::Event &action,sf::Sprite &shipImage);
        void drawShip(sf::RenderWindow &myGameGUI,sf::Sprite &shipImage);


};

#endif // SHIP_H


#include "ship.h"

#include<string>
#include<iostream>
#include <SFML/Graphics.hpp>
#include<SFML/Audio.hpp>

using namespace std;

float shipLocx;
float shipLocY;
float speed = 3.0;


/////////////////////////
    ///create ship
/////////////////////////
void Ship::createShip(sf::Texture &shipTexture, sf::Sprite &shipImage)
{
    if(!shipTexture.loadFromFile("Images/ship.png"))
        cout << "ERROR:Ship could not load" << endl;

    shipImage.setTexture(shipTexture);
    shipImage.setPosition(900,300);

}



///////////
///move ship
///////////
void Ship::moveShip(sf::Event &action, sf::Sprite &shipImage)
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
    shipImage.move(0,-.8 - speed);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
    shipImage.move(0,speed +.8);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
    {
        shipImage.move(-speed - .8,0);
    }
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
    {
        shipImage.move(speed + .8,0);
    }

    if(action.key.code == sf::Keyboard::R)
        shipImage.setPosition(900,300);
}

////////////////////
///Draw ship
////////////////////

void Ship::drawShip(sf::RenderWindow &myGameGUI, sf::Sprite &shipImage)
{
 myGameGUI.draw(shipImage);
}





My Bullet class and header

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

#ifndef BULLET_H
#define BULLET_H
#include <SFML/Graphics.hpp>


class Bullet
{
    public:

        //Bullet();

        void createBullet(sf::Texture &bulletTexture, sf::Sprite &bulletImage, sf::Sprite &shipImage);
        void moveBullet(sf::Sprite &bulletImage, sf::Sprite &shipImage);
        void drawBullet(sf::RenderWindow &myGameGUI, sf::Sprite &bulletImage);

};

#endif // BULLET_H


#include "bullet.h"

#include<string>
#include<iostream>
#include <SFML/Graphics.hpp>
#include<SFML/Audio.hpp>

using namespace std;



void Bullet::createBullet(sf::Texture &bulletTexture, sf::Sprite &bulletImage, sf::Sprite &shipImage)
{
  if(!bulletTexture.loadFromFile("Images/missile.png"))
        cout << "ERROR:bullet could not load" << endl;

    bulletImage.setTexture(bulletTexture);
}

void Bullet::drawBullet(sf::RenderWindow &myGameGUI, sf::Sprite &bulletImage)
{
    myGameGUI.draw(bulletImage);
}

void Bullet::moveBullet(sf::Sprite &bulletImage, sf::Sprite &shipImage){

    sf::Vector2f shipLocation = shipImage.getPosition();


    float bullLocX = shipLocation.x;///gets the X location of the ship places the bullet of  it
    float bullLocY = shipLocation.y + 25;///gets the Y location of the ship


    sf::Clock clock;
    float speed = 1.f;
    float dt = 1.f/60.f;

                        for(int i = 0; i < bullLocX; i++)
                            {
                                bulletImage.move((speed * dt ) + i, bullLocY);


                               // bullLocX -= speed * dt;
                                //bullLocY += speed * dt;
                                dt = clock.restart().asSeconds();
                                bulletImage.setPosition(bullLocX , bullLocY);
                            }




    }
you can take a look at the source code.

https://github.com/LaurentGomila/SFML-Game-Development-Book

you can copy the projectile class.
i got something working that is crude.
if you want a proper working example look at chapter 7.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "Game.h"
#include "StringHelpers.h"

const float Game::PlayerSpeed = 100.f;
const float Game::BulletSpeed = 1.0f;
const sf::Time Game::TimePerFrame = sf::seconds(1.f/60.f);

Game::Game()
: mWindow(sf::VideoMode(640, 480), "SFML Application", sf::Style::Close)
, mTexture()
, mTexture_bullet()
, mBullet() 
, mPlayer()
, mFont()
, mStatisticsText()
, mStatisticsUpdateTime()
, mStatisticsNumFrames(0)
, mIsMovingUp(false)
, mIsMovingDown(false)
, mIsMovingRight(false)
, mIsMovingLeft(false)
, mIsfiring(false)
{
	if (!mTexture.loadFromFile("Images/Eagle.png"))
	{
		// Handle loading error
	}
	
	mPlayer.setTexture(mTexture);
	mPlayer.setPosition(100.f, 100.f);

	if (!mTexture_bullet.loadFromFile("Images/raptor.png"))
	{
			// Handle loading error
	}

	mBullet.setTexture(mTexture_bullet);
	mBullet.scale(.5,.5);
	mBullet.setPosition(124.f, 124.f);
	
	mFont.loadFromFile("Images/Sansation.ttf");
	mStatisticsText.setFont(mFont);
	mStatisticsText.setPosition(5.f, 5.f);
	mStatisticsText.setCharacterSize(10);
}

void Game::run()
{
	sf::Clock clock;
	sf::Time timeSinceLastUpdate = sf::Time::Zero;
	while (mWindow.isOpen())
	{
		sf::Time elapsedTime = clock.restart();
		timeSinceLastUpdate += elapsedTime;
		while (timeSinceLastUpdate > TimePerFrame)
		{
			timeSinceLastUpdate -= TimePerFrame;

			processEvents();
			update(TimePerFrame);
		}

		updateStatistics(elapsedTime);
		render();
	}
}

void Game::processEvents()
{
	sf::Event event;
	while (mWindow.pollEvent(event))
	{
		switch (event.type)
		{
			case sf::Event::KeyPressed:
				handlePlayerInput(event.key.code, true);
				break;

			case sf::Event::KeyReleased:
				handlePlayerInput(event.key.code, false);
				break;

			case sf::Event::Closed:
				mWindow.close();
				break;
		}
	}
}

void Game::update(sf::Time elapsedTime)
{
	sf::Vector2f movement(0.f, 0.f);
	sf::Vector2f bulletmove(0.f, 0.f);

	if (mIsMovingUp)
		movement.y -= PlayerSpeed;
	if (mIsMovingDown)
		movement.y += PlayerSpeed;
	if (mIsMovingLeft)
		movement.x -= PlayerSpeed;
	if (mIsMovingRight)
		movement.x += PlayerSpeed;
	
	mBullet.move(bulletmove.x + (.000001 * elapsedTime.asSeconds()) ,mPlayer.getPosition().y);	
	mPlayer.move(movement * elapsedTime.asSeconds());
}

void Game::render()
{
	mWindow.clear();
	mWindow.draw(mBullet);
	mWindow.draw(mPlayer);
	mWindow.draw(mStatisticsText);
	mWindow.display();
}

void Game::updateStatistics(sf::Time elapsedTime)
{
	mStatisticsUpdateTime += elapsedTime;
	mStatisticsNumFrames += 1;

	if (mStatisticsUpdateTime >= sf::seconds(1.0f))
	{
		mStatisticsText.setString(
			"Frames / Second = " + toString(mStatisticsNumFrames) + "\n" +
			"Time / Update = " + toString(mStatisticsUpdateTime.asMicroseconds() / mStatisticsNumFrames) + "us");
							 
		mStatisticsUpdateTime -= sf::seconds(1.0f);
		mStatisticsNumFrames = 0;
	}
}

void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed)
{	
	if (key == sf::Keyboard::W)
		mIsMovingUp = isPressed;
	else if (key == sf::Keyboard::S)
		mIsMovingDown = isPressed;
	else if (key == sf::Keyboard::A)
		mIsMovingLeft = isPressed;
	else if (key == sf::Keyboard::D)
		mIsMovingRight = isPressed;
	else if (key == sf::Keyboard::Space)
		mIsfiring = isPressed;
	
	if(mIsfiring)
	{
		mBullet.setPosition(mPlayer.getPosition().x ,mPlayer.getPosition().y -25);
		mIsfiring = false;
	}

}
thanks much.. really appreciated
Topic archived. No new replies allowed.