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

Author Topic: making an object move help  (Read 9504 times)

0 Members and 1 Guest are viewing this topic.

makeitloud

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
making an object move help
« on: April 15, 2013, 07:28:17 pm »
Hello guys im new to sfml and have a little bit of C++ knowledge, i was wondering if someone could point me in the direction or help me on getting a ball/or circle to just move around on the screen. any help would be appreciated

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Re: making an object move help
« Reply #1 on: April 16, 2013, 12:33:11 am »
#include <SFML/Graphics.hpp>    

int main()
    {
        //Example for making a rectangle move around
        sf::RenderWindow screen(sf::VideoMode(1024, 768) "Rectangle shape test"); //Create a window
        screen.setFramerateLimit(60); //Limit the frame rate to 60

        sf::RectangleShape rectangle; //Instantiate rectangle object
        rectangle.setFillColor(sf::Color::Blue); //Color the rectangle with blue
        rectangle.setPosition(100, 100); //Set the rectangle's position to 100, 100

        while (screen.isOpen()) //loop to update the window
        {
            sf::Event event; //Create an event class

            while (screen.pollEvent(event)) //Poll all possible events
            {
                if (event.type == sf::Event::Closed) //If the user hits the red X button
                    screen.close(); //If true, close the window

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) //If the user is holding down the key "W"
                    rectangle.move(0, 5); //move the rectangle 5 pixels on the y axis
            }

            screen.clear(); //Clear the screen of all elements

            screen.draw(rectangle); //Draw the rectangle

            screen.display(); //Display it
        }
    }

This should work, I haven't tested it so tell me if there's any errors. Probably careless ones on my part. Also, the only reason I wrote this code for you is to start off. Nobody in the community will write much code for you, so you'll have to learn the API. Start reading the documentation here: http://www.sfml-dev.org/documentation/2.0/. There's tutorials here: http://www.sfml-dev.org/tutorials/2.0/. Notice that I gave you documentation and tutorials for SFML 2.0, which is what everybody should be using. Make sure that you're using that version. Anyways, I hope that you stay around and eventually make some badass games ;)
Current Projects:
Technoport

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: making an object move help
« Reply #2 on: April 16, 2013, 07:48:47 am »
Please read the events tutorial carefully. Using real-time input functions inside the event loop makes no sense.
Laurent Gomila - SFML developer

 

anything