我有一个 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)
{
}
这是游戏对象 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)
{
}