Hi there. I have been trying to get my head around this all week with no success. I have a procedure in an SFML program, that basically bounces a shape around the window.
This works fine. What I want to do is incorporate this code into a function so I can call it with the name of a different shape.
The code part in question is as follows
note: direct and down are bool expressions to control direction.
What I need to do is set it into a function (eg void Bonce('new shape') and input the name of a new shape as a parameter, so, in the case shown circ.setPosition(,x,y) would become 'new shape'.setPosition(x,,y).
Am I just being incredibly stupid or is this really as difficult as it seems?
One way of the solving the problem would be to use polymorphism. In fact, shapes are one of the classic examples -- for polymorphism -- in C++ tutorials.
Have you already come across polymorphism?
Andy
PS I assume that circ is an instant of type Circle (or similar). Does it already have a base class?
Sorry if I did not explain my query very well. 'circ' is a member of a class in sfml which is constructed by using: sf::CircleShape circ(20), where circ is the name of this particular instance and (20) is the radius.
I can create further circles by using different names instead of 'circ'.
What I basically need is a method for passing a new name into a function, as described. The code I have shown above is, at present, inline and it is not a very good idea to have to repeat the whole code for each instance if, for instance, I wished to use 4 circles.
Any advice will be appreciated as I appear to have confused myself even more trying to work out how to do it!
sf::CircleShape circ(20); does, by syntax, construct a variable with name 'circ' and type 'sf::CircleShape'. The '20' is a value passed to the constructor. In the previous posts there are examples that have functions taking a latameter of type as a reference.
void MoveCircle( sf::CircleShape& circ ) // make it a const ref is possible
{
// snip
circ.setPosition(250,350); // circ is whatever cir you passed in
// snip
}
Andy I tried your suggestion . It certainly moves the shape but only once, not for a continuous movement like the inline code does. I printed out the x and y coordinates to check. I will carry on trying, at least you've given me a start.