SFML community forums
Help => General => Topic started by: kadajett on December 16, 2009, 04:50:15 am
-
Any Ideas?
#include <SFML/Graphics.hpp>
#include <math.h>
#include <iostream>
using namespace std;
bool checkCollision(int x1, int x2, int y1, int y2);
int main()
{
// Create the main rendering window
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
sf::Image Gah;
sf::Input input;
if(!Gah.LoadFromFile("Arrow.png"))
{return 1;}
sf::Sprite sprite;
sprite.SetImage(Gah);
sprite.SetPosition(100.0, 100.0);
// Start game loop
while (App.IsOpened())
{
sf::Event Event;
// Process events
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
}
sprite.FlipX(true);
}
// Clear the screen (fill it with black color)
App.Clear();
cout<< "drawing sprite"<<endl;
App.Draw(sprite);
// Display window contents on screen
App.Display();
}
return EXIT_SUCCESS;
}
bool checkCollision(int x1, int x2, int y1, int y2)
{
float dis;
dis = sqrt((float)( ((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1))) );
if(dis <= 30)
{
cout << "collide!" << endl;
return true;
}
if(dis >= 31)
{
cout<< "not collide" << endl;
return false;
}
return false;
}
-
And where is it called? With what values?
-
My fault :) I must have deleted it when I was copy pasting it to the forum
#include <SFML/Graphics.hpp>
#include <math.h>
#include <iostream>
#include <string>
using namespace std;
bool checkCollision(int x1, int x2, int y1, int y2);
int main()
{
// Create the main rendering window
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
sf::Image Gah;
if(!Gah.LoadFromFile("Arrow.png"))
return 1;
sf::Sprite sprite;
sprite.SetImage(Gah);
sf::Font MyFont;
sf::String mouseX("Hello World", MyFont, 30.0 );
sprite.SetPosition(100.0, 100.0);
// Start game loop
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
}
if (checkCollision(sprite.GetPosition().x, input.GetMouseX(), sprite.GetPosition().y, input.GetMouseY()))
{
sprite.FlipX(true);
}
// Clear the screen (fill it with black color)
App.Clear();
App.Draw(sprite);
App.Draw(mouseX);
if (checkCollision(sprite.GetPosition().x, input.GetMouseX(), sprite.GetPosition().y, input.GetMouseY()))
{
sprite.FlipX(true);
}
if (!checkCollision(sprite.GetPosition().x, input.GetMouseX(), sprite.GetPosition().y, input.GetMouseY()))
{
sprite.FlipX(false);
}
// Display window contents on screen
App.Display();
}
return EXIT_SUCCESS;
}
bool checkCollision(int x1, int x2, int y1, int y2)
{
float dis = sqrt((float)( ((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1))) );
if (dis <= 150)
{
cout << "collide!" << endl;
return true;
}
else
{
cout<< "not collide" << endl;
return false;
}
return false;
}
-
Hi
I took the liberty to format your code, it was hardly readable.
The problem is simple, you use an empty sf::Input instance instead of retrieving App.GetInput().
-
Sorry about the readability, I was so tired when I wrote that when I was half asleep and I was getting frustrated. Thanks for the help, it worked. Kinda angry cause I didn't see anything about that in my reading :P
I appreciate it.
-
Now that I look at that code, it makes me want to cry a little. I am throwing it all into classes now. Since I have a basic knowledge of SFML. I am renaming all of the variables to go along with my style. And my game is progressing very well.