-
Hi, I want to have options to move them separately, how can I do this?
if (app.isKeyPressedEvent());
{
dino.processEvents(app.e.key.code, true);
player.procesEvents(app.e.key.code, true);
}
if (app.isKeyReleased())
{
dino.processEvents(app.e.key.code, false);
player.procesEvents(app.e.key.code, false);
}
-
well, its unclear what you mean...
you can move each sprite using
sprite_var.setPosition(x, y)
//or
sprite_var.move(x, y)
-
Sorry for unclear asked question. I am asking how can I make both of control at the same tiem, It means "processEvents" for dino object is method which allow me to control him by "AWSD" for player allow me control him by arrows, but when I relessed one of the key both of them stops. I hope now it's more clear.
-
not really... :-\
can you post some more code? ir a minimal working example?
-
void VirtualSpriteClass::ProcessEvents(sf::Keyboard::Key key, bool checkPressed)
{
if (checkPressed == true)
{
if (key == sf::Keyboard::W)
m_up = true;
if (key == sf::Keyboard::S)
m_down = true;
if (key == sf::Keyboard::A)
m_left = true;
if (key == sf::Keyboard::D)
m_right = true;
}
if (checkPressed == false)
{
m_up = false;
m_down = false;
m_left = false;
m_right = false;
}
}
That's the processEvents function and what I mean is that when I move sprites one of them by "AWSD" and another by "arrows" the move how I expected, but problem is when I relesse one of the key for example I move my dino object by using "D" and player object by using left arrow and the indeed move but when I relesse eaither of them they stops. What I try to achive is that to stop only this sprite which key was relased
-
You want to track the key state separately, so you can then later check whether e.g. W and A are pressed at the same time.
Alternatively or in addition, you can track the direction itself with a direction vector.
-
Could you explain it in more details. My problem is that when I release either key or arrow both of sprites stops.
-
void dinosaur::update()
{
sf::Vector2f movement;
if (up)
movement.y -= 2.0f;
if (down)
movement.y += 2.0f;
if (left)
movement.x -= 2.0f;
if (right)
movement.x += 2.0f;
sprite.move(movement);
}
-
look, we can't really understand what is happening ir this small piece of code. you'll need to provide a minimal working example (https://stackoverflow.com/help/minimal-reproducible-example).
we don't know what this does. maybe you could try simply removing it?
if (app.isKeyReleased())
{
dino.processEvents(app.e.key.code, false);
player.procesEvents(app.e.key.code, false);
}
-
int main()
{
App app;
dinosaur dino;
godzilla god;//Głodzilla
god.sprite.setScale(0.3, 0.3);
while (app.window->isOpen())
{
while (app.window->pollEvent(app.e))
{
if (app.isClosedEvent())
{
app.window->close();
break;
}
if (app.isKeyPressedEvent())
{
dino.processEvents(app.e.key.code, true);
}
if (app.isKeyPressedEvent())
{
god.processEvent(app.e.key.code, true);
}
if (app.isKeyReleased())
{
dino.processEvents(app.e.key.code, false);
}
if (app.isKeyReleased())
{
god.processEvent(app.e.key.code, false);
}
}
app.update();
dino.isTextureLoaded();
dino.isSpriteLoaded();
god.isTextureLoaded();
god.isSpriteLoaded();
god.model();
dino.model();
dino.update();
god.update();
god.colision(god.sprite);
dino.colision(dino.sprite);
app.window->draw(dino.sprite);
app.window->draw(god.sprite);
app.window->display();
}
return 0;
}
[code=cpp]
void dinosaur::processEvents(sf::Keyboard::Key key, bool checkPressed)
{
if (checkPressed == true)
{
if (key == sf::Keyboard::W)
up = true;
if (key == sf::Keyboard::S)
down = true;
if (key == sf::Keyboard::A)
left = true;
if (key == sf::Keyboard::D)
right = true;
}
if (checkPressed == false)
{
up = false;
down = false;
left = false;
right = false;
}
}
[code=cpp]
void dinosaur::update()
{
sf::Vector2f movement;
if (up)
movement.y -= 2.0f;
if (down)
movement.y += 2.0f;
if (left)
movement.x -= 2.0f;
if (right)
movement.x += 2.0f;
sprite.move(movement);
}
[code=cpp]
void godzilla::processEvent(sf::Keyboard::Key key, bool checkPressed)
{
if (checkPressed == true)
{
if (key == sf::Keyboard::Up)
m_up = true;
if (key == sf::Keyboard::Down)
m_down = true;
if (key == sf::Keyboard::Left)
m_left = true;
if (key == sf::Keyboard::Right)
m_right = true;
}
if (checkPressed == false)
{
m_up = false;
m_down = false;
m_left = false;
m_right = false;
}
}
The problem is when I hold e.g D and rigt arrow and when I release D both of them stops not only the one witch key was realessed.
-
sorry, your example is still not complete nor working. I don't know what is inside App class, and what class godzilla, dino, etc belongs to.
check this example, it does what you want to. maybe you can use it in your code.
#include <SFML/Graphics.hpp>
int main(){
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML");
sf::CircleShape circle1(50.f);
sf::CircleShape circle2(50.f);
circle1.setFillColor(sf::Color::Blue);
circle2.setFillColor(sf::Color::Red);
circle1.setPosition(100, 100);
circle2.setPosition(400, 400);
while (window.isOpen()){
sf::Event event;
while (window.pollEvent(event)){
if (event.type == sf::Event::Closed){
window.close();
}
}
//Moving Circle 1
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
circle1.move(-1.f, 0.f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
circle1.move(1.f, 0.f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
circle1.move(0.f, -1.f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
circle1.move(0.f, +1.f);
}
//Moving Circle 2
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
circle2.move(-1.f, 0.f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
circle2.move(1.f, 0.f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
circle2.move(0.f, -1.f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
circle2.move(0.f, +1.f);
}
window.clear(sf::Color::White);
window.draw(circle1);
window.draw(circle2);
window.display();
sf::sleep(sf::milliseconds(10));
}
}
-
With shapes it works good but not with sprites.
dinosaur::dinosaur()
{
isTextureLoaded();
isSpriteLoaded();
up = false;
down = false;
left = false;
right = false;
}
bool dinosaur::isTextureLoaded()
{
if (!texture.loadFromFile("download.png"));
return EXIT_FAILURE;
}
void dinosaur::isSpriteLoaded()
{
sprite.setTexture(texture);
}
int main()
{
dinosaur dino;
godzilla god;//
god.sprite.setScale(0.3, 0.3);
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
god.sprite.move(-2.f, 0.f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
god.sprite.move(2.f, 0.f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
god.sprite.move(0.f, -2.f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
god.sprite.move(0.f, +2.f);
}
//Moving Sprite 2
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
dino.sprite.move(-2.f, 0.f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
dino.sprite.move(2.f, 0.f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
dino.sprite.move(0.f, -2.f);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
dino.sprite.move(0.f, +2.f);
}
}
window.clear(sf::Color::White);
window.draw(dino.sprite);
window.draw(god.sprite);
window.display();
}
return 0;
}
-
I'm still unable to test your code, its incomplete
EDIT: put if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
god.sprite.move(-2.f, 0.f);
and such outside of this loop while (window.pollEvent(event))
-
It works ;D