So, ich konnte mich erst jetzt mit deinem Tutorial beschäftigen da ich die letzten Tage wenig Zeit hatte. Ich bin deinem Tutorial gefolgt und nun habe ich wieder ein Problem: Das Sprite soll sich ja zur Maus bewegen, aber bei mir ist es jetzt so das das Sprite so wie bei setPosition sofort bei der Maus ist(Auch nicht direkt an der Maus sondern ca 100 pixel weiter links).
|
Quellcode
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
void Game::shoot(float frametime)
{
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
sf::Vector2f playerPos;
sf::Vector2i mousePos;
playerPos = player.getPosition();
mousePos = sf::Mouse::getPosition();
int positionX = 0;
int positionY = 0;
positionX = playerPos.x;
positionY = playerPos.y;
int destinationX = 0;
int destinationY = 0;
destinationX = mousePos.x;
destinationY = mousePos.y;
int moveVectorX = destinationX - positionX;
int moveVectorY = destinationY - positionY;
int squareX = moveVectorX*moveVectorX;
int squareY = moveVectorY*moveVectorY;
float distance = std::sqrt(static_cast<float>(squareX + squareY));
moveVectorX / distance;
moveVectorY / distance;
sf::Vector2f move(moveVectorX,moveVectorY);
move * frametime;
bullet.move(move);
}
}
|
Wahrscheinlich hab ich was falsch verstanden oder den Code falsch umgesetzt, ich bin noch nicht so erfahren im Umgang mit Vectoren.