我可以在sfml/cpp中为精灵设定时间限制吗



我想在屏幕上显示一个带有时间限制的警告注释(例如在游戏中(。

我的意思是,有可能用SFML只显示10秒的精灵吗?如果是,那么怎么做?

尝试类似的smth:

public:
warning(){
_isVisible = true;
_warn = sf::CircleShape(10.f);
_warn.setFillColor(sf::Color::Red);
}
void checkTime(){
if(_timeForWarn - _timer.getElapsedTime().asSeconds() <= 0){
_isVisible = false;
}
}
void RestartTime(){
_timer.restart();
}
bool GetVisibility(){
return _isVisible;
}

sf::CircleShape getWarnObj() const{
return _warn;
}
private:
sf::CircleShape _warn;
bool _isVisible = false;
sf::Clock _timer;
float _timeForWarn = 3.f;

然后,只需使用

w.checkTime();
if(w.GetVisibility()){
window.draw(w.getWarnObj());
}    

是的,这是可能的。最简单的方法是使用sf::Clock类:

//Do have in mind sf::Clock starts counting from creation of object, use reset function to start it from anytime you want.
class dissapearingOBject
{
public:
sf::RectangleShape rect; // the visual part of the object
sf::Clock timer; //the timer
int dissTime; // the time to dissapear at in milliseconds

dissapearingOBject(int time = 10000) : dissTime(time)
{

}

dissapearingOBject()
{

}
void resetTimer()
{
timer.restart();
}

int getTimer()
{
return timer.getElapsedTime().asMilliseconds();
}

void draw(sf::RenderWindow &window)
{
if (getTimer() < dissTime)
{
window.draw(rect);
}
}
};

最新更新