0
\$\begingroup\$

I can't quite figure this out, no matter the various modifications I make.

What should happen: I want this to only move the player on left click/left up if i have previously right clicked

What actually happens: Regardless of which mouse button I click/unclick, it activates the direction function.

The offending code:

class Controller_Mouse : public Component
{
public:

    Component_Transform *transform;

    void init() override
    {
        transform = &entity->get_component<Component_Transform>();
    }

    void update() override
    {
        if (Game::event.type == SDL_MOUSEBUTTONDOWN)
        {
            if (SDL_BUTTON_RIGHT)
            {
                transform->active = true;
            }

            if (SDL_BUTTON_LEFT)
            {
                if (transform->active == true)
                {
                    std::cout << "transform vals: " << transform->position.x << ", " << transform->position.y << std::endl;
                    std::cout << "click vals:     " << Game::event.motion.x << ", " << Game::event.motion.y << std::endl;

                    transform->target.x = Game::event.motion.x; transform->target.y = Game::event.motion.y;
                }

                transform->active = false;
            }
        }
        if (Game::event.type == SDL_MOUSEBUTTONUP && SDL_BUTTON_LEFT)
        {
            transform->direction(transform->position.x, transform->position.y, transform->target.x, transform->target.y);
        }
    }
};
\$\endgroup\$
4
\$\begingroup\$

You are not comparing anything to the mouse constants in your if-statements. You need to include the event's mouse button member there.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ No, not the solution. \$\endgroup\$ – Chris Sep 5 '17 at 4:54
  • 3
    \$\begingroup\$ @Chris You are checking if(SDL_BUTTON_RIGHT) and right after if(SDL_BUTTON_LEFT). These will always evaluate to true. \$\endgroup\$ – George Hanna Sep 5 '17 at 5:38
  • \$\begingroup\$ There's also a lonely constant in the last if. \$\endgroup\$ – Tyyppi_77 Sep 5 '17 at 5:50
  • \$\begingroup\$ @Chris: Just saying "no" doesn't give users much information to be able to help you. What specific changes did you try to make, and what outcomes did you observe? This info can help narrow in on what's going wrong. \$\endgroup\$ – DMGregory Sep 5 '17 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.