Thanks. But even if I use shape, it will disappear. And I tried to lower down the move(50,0) to move(10,0).
Since I really need to make the getcircle function to work (otherwise it will draw a copy of ball), can you tell me how I can return an address? I thought I would have needed to use pointers but I really don't know how I could do that. I want to do it with pointers that are more familiar with me. I recognize its my first time trying to use libraries and classes altogether, I already did functions that worked on pointers but never functions that returns pointers. For example how can I call the draw function with the pointer ?
Here is my new code:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
sf::Event event;
class Ball
{
int CircleR, Redd, Greenn, Bluee;
sf::CircleShape * shape;
public:
Ball(int CircleR,int Redd,int Greenn,int Bluee);
Ball();
sf::CircleShape * getCircle();
void moveCircle(int x, int y);
};
int main()
{
Ball ball(100.f, 250, 0, 0);
sf::RenderWindow window(sf::VideoMode(800,600), "SFML Test");
while(window.isOpen())
{
while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
window.close();
break;
default:
break;
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
ball.moveCircle(-10,0);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
ball.moveCircle(10,0);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
ball.moveCircle(0,10);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
ball.moveCircle(0,-10);
}
window.clear(sf::Color::Black);
window.draw(&(ball.getCircle()));
window.display();
}
return 0;
}
Ball::Ball(int CircleR, int Redd, int Greenn, int Bluee)
{
CircleR = CircleR;
Redd = Redd;
Greenn = Greenn;
Bluee = Bluee;
shape->setRadius(CircleR);
shape->setFillColor(sf::Color(Redd,Greenn,Bluee));
shape->setPosition(100,100);
}
Ball::Ball()
{
shape->setRadius(50);
shape->setFillColor(sf::Color(100,100,100));
shape->setPosition(100,100);
}
sf::CircleShape * Ball::getCircle()
{
return shape;
}
void Ball::moveCircle(int x, int y)
{
getCircle()->move(x, y);
}
Also forget my last code for the key events, I just wanted to replace the if with a switch statement but it doesn't look like you can replace it with a switch. Thanks for your help.