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\$

1 Answer 1

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.

\$\endgroup\$
4
  • \$\begingroup\$ No, not the solution. \$\endgroup\$
    – Chris
    Commented Sep 5, 2017 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\$ Commented Sep 5, 2017 at 5:38
  • \$\begingroup\$ There's also a lonely constant in the last if. \$\endgroup\$
    – user35344
    Commented Sep 5, 2017 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
    Commented Sep 5, 2017 at 11:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .