1
General discussions / How do you use the Missile class?
« on: July 11, 2008, 12:03:35 am »
I should be able to use App.Draw now, right?
also what does Missile m; do ?
also what does Missile m; do ?
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int direction =0;
class Missile
{
public :
static bool Init(const std::string& ImageFile)
{
return Image.LoadFromFile(ImageFile);
}
Missile()
{
Sprite.SetImage(Image); // every sprite uses the same unique image
}
private :
static sf::Image Image; // shared by every instance
sf::Sprite Sprite; // one per instance
};
sf::Image Missile::Image;
int main()
{
// Create the main rendering window
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
App.SetBackgroundColor(sf::Color(255, 255, 255));
// Load the sprite image from a file
sf::Image Image;
if (!Image.LoadFromFile("megaman.png"))
return EXIT_FAILURE;
// sf::Image bullet;
// if (!Image.LoadFromFile("megaman.png"))
// return EXIT_FAILURE;
// Create the sprite
sf::Sprite Sprite(Image);
// Change its properties
Sprite.SetColor(sf::Color(0, 255, 255, 200));
Sprite.SetPosition(500.f, 500.f);
Sprite.SetScale(2.f, 2.f);
// 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();
}
// Get elapsed time
float ElapsedTime = App.GetFrameTime();
// Move the sprite
if (App.GetInput().IsKeyDown(sf::Key::Left)) {Sprite.Move(-100 * ElapsedTime, 0);
if (direction == 1){
direction =0; Sprite.FlipX(0);
}}
if (App.GetInput().IsKeyDown(sf::Key::Right)) {Sprite.Move( 100 * ElapsedTime, 0);
if (direction == 0){
direction =1; Sprite.FlipX(1);
}}
if (App.GetInput().IsKeyDown(sf::Key::Up)) Sprite.Move(0, -100 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::Down)) Sprite.Move(0, 100 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::V))
if(!Missile::Init("megaman.png")){
exit(EXIT_FAILURE);
Missile m;}
}
// Rotate the sprite
if (App.GetInput().IsKeyDown(sf::Key::Z)) Sprite.Rotate(- 100 * ElapsedTime);
if (App.GetInput().IsKeyDown(sf::Key::Subtract)) Sprite.Rotate(+ 100 * ElapsedTime);
// Display sprite in our window
App.Draw(Sprite);
// Display window contents on screen
App.Display();
}
return EXIT_SUCCESS;
}
(NOTE: 1 or more comments may be wrong)class Missile
{
public :
static bool Init(const std::string& ImageFile)
{
return Image.LoadFromFile(ImageFile);
}
Missile()
{
Sprite.SetImage(Image); // every sprite uses the same unique image
}
private :
static sf::Image Image; // shared by every instance
sf::Sprite Sprite; // one per instance
};