我有一个Game.cpp,我在我的Player类中调用了一个更新函数。在我的播放器更新功能我有它检查键盘输入,它似乎工作,但每当我尝试调用.move()函数,它似乎不起作用。我也没有错。我是sfml的新手,并且使用c ++很不错。感谢帮助!
#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)
{
}
这是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)
{
}
答案 0 :(得分:1)
从显示的代码中,您应该将moveObject重写为
void GameObject::moveObject(float x, float y)
{
spr.move(x, y);
}
其中spr是类
中其他地方使用的GameObject的成员变量答案 1 :(得分:0)
将moveObject函数更改为此
void GameObject::moveObject(float x, floaty)
{
spr.setPosition(x, y);
}
如果您需要获取x和y位置,请为spr创建get方法或将其公开