#include "pch.h"
#include <iostream>
#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>
#include <SFML\System.hpp>
using namespace std;
using namespace sf;
class Flower {
public:
Flower() {
}
void create(float posx, float posy, bool tr, RenderWindow &window, float dt) {
sprite.setPosition(posx, posy);
texture.loadFromFile("data/img/red.png");
l += 2 * dt;
sprite.setTexture(texture);
if (l >= 2 && l < 4)
texture.loadFromFile("data/img/blue.png");
if (l >= 4 && l < 6)
texture.loadFromFile("data/img/green.png");
if (l >= 6)
texture.loadFromFile("data/img/yellow.png");
window.draw(sprite);
}
Sprite sprite;
Texture texture;
float l = 0;
};
int main()
{
Flower *flower;
RenderWindow window(VideoMode(640, 640, 32), "Gra");
window.setFramerateLimit(60);
Clock clock;
float dt = 0;
Event e;
while (window.isOpen())
{
dt = clock.restart().asSeconds();
while (window.pollEvent(e))
{
switch (e.type) {
case Event::Closed:
window.close();
break;
}
if (e.type == Event::KeyPressed && e.key.code == Keyboard::A) {
flower = new Flower;
flower->create(200, 200, 1, window, dt);
}
}
window.display();
window.clear();
}
}
Chciałem zrobić dodatek do prostej gry 2d taki, że po naciśnięciu danego klawisza w miejscu gracza pojawia się nowy obiekt drzewa, jak próbuje dać if(Keyboard::isKeyPressed(Keyboard::Space)) to wyświetla się, ale gdy puszczam to znika. Ma ktoś jakieś rozwiązanie?