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

Author Topic: move function doesnt work with variables?  (Read 1857 times)

0 Members and 1 Guest are viewing this topic.

Daora

  • Newbie
  • *
  • Posts: 2
    • View Profile
move function doesnt work with variables?
« on: April 23, 2017, 04:36:47 pm »
I have a weird problem. I Just started with SFML yesterday and wanted to make a simple game for the beginning, now i want to move a rectangle object 0.5pixel in x and y with every loop, so when i write
ball.move(0.5, 0.5);
everything works as expected, but if i put x and y into variables it stops working, anyone know what causes this behavior?

Here is the full code for reference:

#include <SFML/Graphics.hpp>
#include <stdio.h>
#include <iostream>

int main()
{
        sf::RenderWindow window(sf::VideoMode(1280, 718), "", sf::Style::Close);
        sf::RectangleShape ball(sf::Vector2f(15, 15));

        ball.setPosition(640, 359);

        int p1score = 0;
        int p2score = 0;

        int xspeed = 0.5;
        int yspeed = 0.5;

        int x = ball.getPosition().x;
        int y = ball.getPosition().y;

       

        //mainloop
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        //close
                        if (event.type == sf::Event::Closed)
                        {
                                window.close();
                        }

                }

                window.clear(sf::Color::Black);



                ball.move(xspeed, yspeed);

                window.draw(ball);
               
                window.display();
        }


        return 0;
}




Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: move function doesnt work with variables?
« Reply #1 on: April 23, 2017, 04:45:59 pm »
Quote
int xspeed = 0.5;
0.5 stored in an integer is 0.
Laurent Gomila - SFML developer

Daora

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: move function doesnt work with variables?
« Reply #2 on: April 23, 2017, 05:20:05 pm »
omg im fucking stupid haha