Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Moving springs separately  (Read 2292 times)

0 Members and 1 Guest are viewing this topic.

jakub

  • Newbie
  • *
  • Posts: 8
    • View Profile
Moving springs separately
« on: January 19, 2023, 12:33:45 pm »
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);
         }
 
« Last Edit: January 19, 2023, 12:46:30 pm by eXpl0it3r »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Moving springs separately
« Reply #1 on: January 19, 2023, 02:42:58 pm »
well, its unclear what you mean...
you can move each sprite using
sprite_var.setPosition(x, y)
//or
sprite_var.move(x, y)
Visit my game site (and hopefully help funding it? )
Website | IndieDB

jakub

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Moving springs separately
« Reply #2 on: January 19, 2023, 04:41:12 pm »
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.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Moving springs separately
« Reply #3 on: January 21, 2023, 06:43:01 pm »
not really...  :-\
can you post some more code? ir a minimal working example?
Visit my game site (and hopefully help funding it? )
Website | IndieDB

jakub

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Moving springs separately
« Reply #4 on: January 22, 2023, 12:07:45 pm »
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
« Last Edit: January 23, 2023, 08:40:19 am by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Moving springs separately
« Reply #5 on: January 23, 2023, 02:16:19 pm »
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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

jakub

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Moving springs separately
« Reply #6 on: January 24, 2023, 11:06:43 am »
Could you explain it in more details. My problem is that when I release either key or arrow both of sprites stops.

jakub

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Moving springs separately
« Reply #7 on: January 24, 2023, 11:15:50 am »
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);
}

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Moving springs separately
« Reply #8 on: January 24, 2023, 11:49:59 am »
look, we can't really understand what is happening ir this small piece of code. you'll need to provide a minimal working 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);
         }
Visit my game site (and hopefully help funding it? )
Website | IndieDB

jakub

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Moving springs separately
« Reply #9 on: January 24, 2023, 12:45:24 pm »
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.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Moving springs separately
« Reply #10 on: January 24, 2023, 01:24:28 pm »
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));
    }
}
Visit my game site (and hopefully help funding it? )
Website | IndieDB

jakub

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Moving springs separately
« Reply #11 on: January 24, 2023, 02:47:38 pm »
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;
}

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Moving springs separately
« Reply #12 on: January 24, 2023, 03:31:05 pm »
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))
« Last Edit: January 24, 2023, 03:32:44 pm by Stauricus »
Visit my game site (and hopefully help funding it? )
Website | IndieDB

jakub

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Moving springs separately
« Reply #13 on: January 24, 2023, 04:10:19 pm »
It works  ;D

 

anything