Recently I have been making a test "game" in C++ (to get used with SFML). I have made a Super Mario sprite as shown below
sf::Texture SuperMarioAnim;
if (!SuperMarioAnim.loadFromFile("img/image.png"))
{
std::wcout << L"Συγγνώμη, η εικόνα SuperMarioAnim δεν υπάρχει ή διαγράφηκε";
}
sf::Sprite SuperMario;
SuperMario.setTexture(SuperMarioAnim);
SuperMario.setPosition(200, 2900);
I also made a player_speed integer, with the value of the speed of the player, obviously, and I also created a time object equal to the value we get from clock.getElapsedTime()
:
int player_spd = 30;
while (window.isOpen())
{
//...
sf::Time time;
time = clock.getElapsedTime();
//...
}
However, when I tell the sprite to move in the X axis with this:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
SuperMario.move(sf::Vector2f((player_spd * time), 0));
I get this error: 1>c:\\[directory]\\visual studio 2017\\projects\\test_game\\test_game\\test_game.cpp(180): error C2593: 'operator *' is ambiguous
(line 180 is the SuperMario.move command right above)
Could someone specify what I did wrong? I am using Visual Studio 2017 and SFML 2.4
sf::Time
defines these overloads of operator*
1 :
Time operator* (Time left, float right)
//Overload of binary * operator to scale a time value.
Time operator* (Time left, Int64 right)
//Overload of binary * operator to scale a time value.
Time operator* (float left, Time right)
//Overload of binary * operator to scale a time value.
Time operator* (Int64 left, Time right)
//Overload of binary * operator to scale a time value.
The error indicates that overload resolution failed because it can't choose among them. player_spd
is an int
. And calling operator*
will require converting it to either float
or Int64
. Neither is better than the other (according to the error message). So it's ambiguous.
You can cast, or just define player_spd
as one of the types that sf::Time
can be multiplied by.
'clock.getElapsedTime()' returns a 'Time' object. You have the option of returning a float from a 'Time' object as 'asSeconds()', 'asMilliseconds()', or 'asMicroseconds()'. I believe this will solve your error, but am unable to test on my end. I would also recommend changing 'player_spd' to a float.
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
SuperMario.move(player_spd * time.asSeconds()), 0));
Good luck!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.