Moving My Player Sprite (SFML)

I have a class for my player functions and variables, a class for handling events, drawing sprites etc.

I would like my player sprite to move once the key is pressed. So if W is pressed it will move up. However it is not working at all:

main.cpp
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
#include "Stdafx.h"
#include "Player.h"
Player player;
#include "Game.h"
Game game;

int main()
{

	// Window Variables
	int windowWidth = 1280;
	int windowHeight = 720;
	int newWindowWidth = 1152;
	int newWindowHeight = 648;

	// Render Window
	sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), "Window");
	window.setSize(sf::Vector2u(newWindowWidth, newWindowHeight));

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	player.LoadGame();

	sf::Texture playerTexture;

	game.LoadSpriteIntoStack(player.CreatePlayer(player.GetSprite(), &playerTexture));

	while (window.isOpen())
	{
		// Handle Events
		game.HandleEvents(window, player);

		player.MovePlayer();

		window.clear();

		// Draw Sprites
		game.DrawStackOfSprites(window);

		window.display();

	}

	player.SaveGame();
	return 0;
}


Game.cpp
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
#include "Stdafx.h"
#include "Player.h"
#include "Game.h"

Game::Game()
{
}

void Game::HandleEvents(sf::RenderWindow& window, Player player)
{
	sf::Event event;

	while (window.pollEvent(event))
	{
		switch (event.type)
		{
		case sf::Event::Closed:
			window.close();
			break;
		case sf::Event::Resized:
			windowSize = window.getSize();
			window.setSize(sf::Vector2u(windowSize.x, windowSize.y));

		case sf::Event::KeyPressed:
			switch (event.key.code)
			{
			case sf::Keyboard::Escape:
				window.close();
				break;

			// Player Movement
			case sf::Keyboard::W:
				player.SetMovingUp(true);
			case sf::Keyboard::S:
				player.SetMovingDown(true);
			case sf::Keyboard::A:
				player.SetMovingLeft(true);
			case sf::Keyboard::D:
				player.SetMovingRight(true);
			}
		case sf::Event::KeyReleased:
			switch (event.key.code)
			{
			// Player Movement
			case sf::Keyboard::W:
				player.SetMovingUp(false);
			case sf::Keyboard::S:
				player.SetMovingDown(false);
			case sf::Keyboard::A:
				player.SetMovingLeft(false);
			case sf::Keyboard::D:
				player.SetMovingRight(false);
			}
		}
	}
}

void Game::LoadSpriteIntoStack(sf::Sprite sprite)
{
	stackOfSprites.push_back(sprite);
}

void Game::DrawStackOfSprites(sf::RenderWindow& window)
{
	for (unsigned int i = 0; i < stackOfSprites.size(); i++)
	{
		window.draw(stackOfSprites[i]);
	}
}


Game.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once

class Game
{
public:
	Game();

	// Handle Events
	void HandleEvents(sf::RenderWindow& window, Player player);

	void LoadSpriteIntoStack(sf::Sprite sprite);
	void DrawStackOfSprites(sf::RenderWindow& window);

private:
	sf::Vector2u windowSize;

	std::vector<sf::Sprite> stackOfSprites;
};


Player.cpp (I took out the code with no relevence)
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
#include "Stdafx.h"
#include "Player.h"
#include "Game.h"

Player::Player()
{
}

Player::~Player()
{
}

sf::Sprite Player::CreatePlayer(sf::Sprite sprite, sf::Texture* texture)
{
	texture->loadFromFile("images/player.png");
	sprite.setTexture(*texture);
	return sprite;
}

sf::Sprite Player::GetSprite()
{
	return playerSprite;
}

void Player::MovePlayer()
{
	if (isMovingUp == true)
	{
		playerSprite.move(100, 100);
	}
	else if (isMovingDown == true)
	{
		playerSprite.move(100, 100);
	}
	else if (isMovingLeft == true)
	{
		playerSprite.move(100, 100);
	}
	else if (isMovingRight == true)
	{
		playerSprite.move(100, 100);
	}
}

void Player::SetMovingUp(bool tempMove)
{
	isMovingUp = true;
}

void Player::SetMovingDown(bool tempMove)
{
	isMovingDown = true;
}

void Player::SetMovingLeft(bool tempMove)
{
	isMovingLeft = true;
}

void Player::SetMovingRight(bool tempMove)
{
	isMovingRight = true;
}


Player.h
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
#pragma once

class Player
{
public:
	Player();
	~Player();

	sf::Sprite CreatePlayer(sf::Sprite sprite, sf::Texture* texture);
	sf::Sprite GetSprite();

	void Shoot();

	// Is sprite moving
	bool GetMovingUp();
	bool GetMovingDown();
	bool GetMovingLeft();
	bool GetMovingRight();

	// Set sprite moving
	void SetMovingUp(bool tempMove);
	void SetMovingDown(bool tempMove);
	void SetMovingLeft(bool tempMove);
	void SetMovingRight(bool tempMove);

	// Move sprite
	void MovePlayer();

	// Ammo
	int GetAmmo();
	void SetAmmo(int tempAmmo);

	// Lives
	int GetLives();
	void SetLives(int tempLives);

	// Save
	void LoadGame();
	void SaveGame();

private:
	int ammo;
	int lives;

	bool isMovingUp;
	bool isMovingDown;
	bool isMovingLeft;
	bool isMovingRight;

	sf::Sprite playerSprite;
};


Any help will be appreciated :)
Last edited on
Your game object holds sprite objects. No sprite held by the game object is in any way connected or linked to the playerSprite in the player object. The only sprites that are drawn in this code are those contained in the game object, so updating the playerSprite in the player object has no effect.

If you want the a Game object to refer to an existing sprite, it should not store a copy of the sprite. It should store some sort of reference (in a general sense - a pointer would work) to a sprite.
So instead of std::vector<sf::Sprite> stackOfSprites; I should have std::vector<sf::Sprite*> stackOfSprites;

However i now get errors here:
1
2
3
4
5
6
7
8
9
10
11
12
void Game::LoadSpriteIntoStack(sf::Sprite* sprite)
{
	stackOfSprites->push_back(sprite);
}

void Game::DrawStackOfSprites(sf::RenderWindow& window)
{
	for (unsigned int i = 0; i < stackOfSprites.size(); i++)
	{
		window->draw(stackOfSprites[i]);
	}
}



Error 1 error C2819: type 'std::vector<sf::Sprite *,std::allocator<_Ty>>' does not have an overloaded member 'operator ->' c:\users\dylan\documents\visual studio 2013\projects\sfml game\helpwitharrow\trunk\game.cpp 60 1 SFML Game
Error 2 error C2232: '->std::vector<sf::Sprite *,std::allocator<_Ty>>::push_back' : left operand has 'class' type, use '.' c:\users\dylan\documents\visual studio 2013\projects\sfml game\helpwitharrow\trunk\game.cpp 60 1 SFML Game
Error 3 error C2819: type 'sf::RenderWindow' does not have an overloaded member 'operator ->' c:\users\dylan\documents\visual studio 2013\projects\sfml game\helpwitharrow\trunk\game.cpp 67 1 SFML Game
Error 4 error C2232: '->sf::RenderTarget::draw' : left operand has 'class' type, use '.' c:\users\dylan\documents\visual studio 2013\projects\sfml game\helpwitharrow\trunk\game.cpp 67 1 SFML Game
5 IntelliSense: expression must have pointer type c:\Users\Dylan\Documents\Visual Studio 2013\Projects\SFML Game\helpwitharrow\trunk\Game.cpp 60 2 SFML Game
6 IntelliSense: expression must have pointer type c:\Users\Dylan\Documents\Visual Studio 2013\Projects\SFML Game\helpwitharrow\trunk\Game.cpp 67 3 SFML Game


Not sure if that is all I would have to change. But I would like to fix these errors before I continue :)
Last edited on
stackOfSprites.push_back(sprite);
What about the:

1
2
3
4
5
6
7
void Game::DrawStackOfSprites(sf::RenderWindow& window)
{
	for (unsigned int i = 0; i < stackOfSprites.size(); i++)
	{
		window->draw(stackOfSprites[i]);
	}
}


and is that all I would have to change?
Last edited on
window->draw(*stackOfSprites[i]);

and is that all I would have to change?


In the code that you've shown, yes.
Thank you for your help, I solved my problem :)
Topic archived. No new replies allowed.