sfml player doesnt seem to move

Started by
7 comments, last by jpetrie 9 years ago

Player.h


#pragma once
#include "Game.h"
#include "AnimatedSprite.h"

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


	void OnKeyDown();
	void Draw(sf::RenderWindow &window);
	
private:
	sf::Texture playerTexture;
	sf::Sprite playerImage;
};

Player.cpp


#include "Player.h"

Player::Player()
{
	if (!playerTexture.loadFromFile("images/wizardT.png"))
	{
		std::cout << "Can't load the player image";
	}


	playerImage.setTexture(playerTexture);
	playerImage.setPosition(200, 200);
	playerImage.setTextureRect(sf::IntRect(48, 14, 73, 73));
}


Player::~Player()
{
}

void Player::OnKeyDown(){
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
		playerImage.move(0, -5.0);
	}
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
		playerImage.move(0, 5.0);
	}
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
		playerImage.move(-5.0, 0);
	}
	if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
		playerImage.move(5.0, 0);
	}
}

void Player::Draw(sf::RenderWindow &window){
	window.draw(playerImage);
}

Game.cpp


#include "Game.h"
#include "AnimatedSprite.h"

Game::Game()
{
}


Game::~Game()
{
}

void Game::Setup(){
}

void Game::OnKeyDown(){
	Player player;
	player.OnKeyDown();
}

void Game::Update(sf::RenderWindow &window){
	Setup();
	OnKeyDown();
}

void Game::Draw(sf::RenderWindow &window){
	Player player;
	player.Draw(window);
}

Main.cpp


#include <SFML/Graphics.hpp>
#include "Game.h"

int main()
{
	sf::RenderWindow window(sf::VideoMode(800,600), "SFML works!");

	Game game;

	while (window.isOpen())
	{
		sf::Event event;
		while (window.pollEvent(event))
		{
			if (event.type == sf::Event::Closed)
				window.close();
		}

		game.Update(window);

		window.clear();
		game.Draw(window);
		window.display();
	}

	return 0;
}

I cant spot anything wrong with it, any help would be great

Advertisement

You create a new player every time you enter the OnKeyDown function.

Your Game class needs to contain an instance/reference/pointer to a Player.

Currently, you create a new player in both Game::OnKeyDown and Game::Draw, and in both cases your player instance is destroyed upon leaving the function (out of scope).

Hello to all my stalkers.

You create a new player every time you enter the OnKeyDown function.

Your Game class needs to contain an instance/reference/pointer to a Player.

Currently, you create a new player in both Game::OnKeyDown and Game::Draw, and in both cases your player instance is destroyed upon leaving the function (out of scope).

I was using Player player inside the game.h file. It runs once and then i get a weird error which doesn't make sence:

dbfa958606e45d5acc1a9fb36a76cb36.png

I was using Player player inside the game.h file. It runs once and then i get a weird error which doesn't make sence:

dbfa958606e45d5acc1a9fb36a76cb36.png

Image doesn't load, but you are incorrect.


void Game::OnKeyDown(){
	Player player; //This creates a new instance of the player
	player.OnKeyDown(); //This uses the newly created instance of the player
} //The new instance of the player goes out of scope and is destroyed.


void Game::Draw(sf::RenderWindow &window){
	Player player; //Same as above
	player.Draw(window);
}

Hello to all my stalkers.

I was using Player player inside the game.h file. It runs once and then i get a weird error which doesn't make sence:

dbfa958606e45d5acc1a9fb36a76cb36.png

Image doesn't load, but you are incorrect.


void Game::OnKeyDown(){
	Player player; //This creates a new instance of the player
	player.OnKeyDown(); //This uses the newly created instance of the player
} //The new instance of the player goes out of scope and is destroyed.


void Game::Draw(sf::RenderWindow &window){
	Player player; //Same as above
	player.Draw(window);
}

http://gyazo.com/dbfa958606e45d5acc1a9fb36a76cb36

I put it like that because I wasnt able to load the game with it in the .h file


I put it like that because I wasnt able to load the game with it in the .h file

As I said, you need to use a player that's inside Game somewhere (since we don't want to use filthy globals). This can be either a pointer/reference, or an instance.

If you tried doing that, but got errors with it, you need to post that code, so we can see what you did wrong.

Hello to all my stalkers.

Just wondering but why are you including game in the player? That logically doesn't make sense. You would include a player in a game but not the other way around. Then in the Game you could create a public variable that is Player player.

Just wondering but why are you including game in the player? That logically doesn't make sense. You would include a player in a game but not the other way around. Then in the Game you could create a public variable that is Player player.

Just wondering but why are you including game in the player? That logically doesn't make sense. You would include a player in a game but not the other way around. Then in the Game you could create a public variable that is Player player.

seems like it fixed the error and now my movement works! Thanks all!

In the future, please do not mark threads in the For Beginners forum with "solved" prefixes or similar; it's against the rules.

This topic is closed to new replies.

Advertisement