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

Author Topic: Is there a way of getting SFML to move a sprite to the mouse cursor position?  (Read 5725 times)

0 Members and 1 Guest are viewing this topic.

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
I've had a lot of success and found it surprisingly easy to get keys set up in SFML but I'm currently struggling with the mouse commands, is it possible to get a basic point and click function going? I'm talking the type you see on RPGs like Diablo 2, click to move the sprite across the screen. All the drawing commands work fine for me and I can get what I want on the screen no problem. I realise the localPosition is the wrong command for what I want but I've hit a wall now with it and I'd appreciate some tips, I can get stuff to happen if I specify exact coordinates and such but I want the sprite to follow where the cursor goes.

Quote


    while (window.isOpen())
    {

    sf::Event event;
    while (window.pollEvent(event))

        sf::Vector2i localPosition = sf::Mouse::getPosition( window );

        if ( sf::Mouse::isButtonPressed( sf::Mouse::Left ) );

        {
            spritename1.move ( localPosition );
        }


        {
            if (event.type == sf::Event::Closed)
                window.close();
            }

        window.clear();
        window.draw( spritewasteland1 );
        window.draw ( spritename1 );
        window.display();

    }

    return 0;


Note: Oops! I think I may have found the solution already! Let me know if you can think of anything better though! I think I'm going to have to use some of this: http://www.sfml-dev.org/documentation/2.0/structsf_1_1Event_1_1MouseMoveEvent.php

Extra Note: Okay found that events are the things I need to look at carefully so I can understand the syntax better: http://sfml-dev.org/tutorials/2.0/window-events.php
« Last Edit: June 03, 2013, 10:29:47 am by Lethn »

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
I think I know what the problem is properly, I don't know the command needed for move to recognise where my mouse cursor coordinates are, I tried GetPosition but that just seemed to cause even more problems, I'd appreciate help if someone could post, in the mean time I'll keep digging around.

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
I FOUND IT OUT WOOT! WOOT! :D

http://en.sfml-dev.org/forums/index.php?topic=8539.0

Quote
To be a bit more precise, it's now sf::Mouse::getPosition().y, if it should be relative to your screen resolution and sf::Mouse::getPosition(window).y, if it should be relative to your window, were window is of type sf::RenderWindow.

Now I just need to find out why the bugger is warping all over the place and then I'll be able to do whatever I want with this.

 

anything