Sprite not moving when using a function from another class SFML c++
Posted
by
user2892932
on Stack Overflow
See other posts from Stack Overflow
or by user2892932
Published on 2013-10-30T22:18:09Z
Indexed on
2013/11/01
9:54 UTC
Read the original article
Hit count: 231
I have a Game.cpp, and I am calling a update function in my Player class. In my player update Function I have it to check for keyboard input, and it seems to work, but whenever I try to call the .move() function, it seems to not work. I get no errors either. I am new to sfml, and decent with c++. Help is appreciated!
#include "Player.h"
Player::Player(void):
vel(0),
maxvel(100)
{
Load("Assets/sss.png",true);
}
void Player::Update(sf::Sprite& p)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
moveObject(-3,0, p);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
moveObject(-3,0, p);
}
}
Player::~Player(void)
{
}
This is the GameObject cpp
#include "GameObject.h"
#include <iostream>
GameObject::GameObject(void)
{
isLoaded = false;
}
void GameObject::Load(std::string flname, bool isPlayer)
{
if(!tex.loadFromFile(flname))
{
EXIT_FAILURE;
}
else
{
if(isPlayer)
{
if(!tex.loadFromFile(flname, sf::IntRect(0,0,33,33)))
{
EXIT_FAILURE;
}
else
{
std::cout << "Loading image" << "\n";
filename = flname;
spr.setTexture(tex);
isLoaded = true;
}
}
else
{
std::cout << "Loading image" << "\n";
filename = flname;
spr.setTexture(tex);
isLoaded = true;
}
}
}
void GameObject::Draw(sf::RenderWindow & window)
{
if(isLoaded)
{
window.draw(spr);
window.display();
std::cout << "Sprite drew" << "\n";
}
}
void GameObject::setPos(float x, float y)
{
if(isLoaded)
{
spr.setPosition(x,y);
}
}
sf::Vector2f GameObject::GetObjPos()
{
return spr.getPosition();
}
sf::Sprite& GameObject::getSprite()
{
return spr;
}
void GameObject::moveObject(float x, float y, sf::Sprite& sp)
{
sp.move(x, y);
}
GameObject::~GameObject(void)
{
}
© Stack Overflow or respective owner