SFML community forums
General => General discussions => Topic started by: Coondog on July 10, 2008, 10:34:07 pm
-
I'm not real good with C++, how do you use the Missile Class?
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
};
would I just use something like Missile("imagefile.png"); ?
I'd like to get bullet firing going with taking the x and y(probably modifying the x and y a bit later) of the character and then creating a bullet(sprite?) with that data
-
Call the Init-method first with the path to the image as the parameter before creating any instances of the missile class. Like this:
if(!Missile::Init("imagefile.png"))
exit(EXIT_FAILURE);
Missile m;
I think that should work, I'm not an expert either :)
-
hmm, I tried your code, but....
/tmp/ccksi0yc.o: In function `Missile::Init(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
main.cpp:(.text._ZN7Missile4InitERKSs[Missile::Init(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)]+0x10): undefined reference to `Missile::Image'
/tmp/ccksi0yc.o: In function `Missile::Missile()':
main.cpp:(.text._ZN7MissileC1Ev[Missile::Missile()]+0x19): undefined reference to `Missile::Image'
collect2: ld returned 1 exit status
-
Try putting this before your main():
sf::Image Missile::Image;
It works for me.
-
here is the code currently:
////////////////////////////////////////////////////////////
// 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)
Here is what I get if I run g++ main.cpp -o main -lsfml-window -lsfml-graphics: main.cpp: In function ‘int main()’:
main.cpp:95: error: ‘ElapsedTime’ was not declared in this scope
main.cpp:96: error: ‘ElapsedTime’ was not declared in this scope
main.cpp: At global scope:
main.cpp:106: error: expected unqualified-id before ‘return’
main.cpp:107: error: expected declaration before ‘}’ token
Like I said, I'm not real good with C++
-
You had some misplaced curly brackets, try this:
////////////////////////////////////////////////////////////
/// 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;
}
-
Thanks, it works :)
-
Remember, this should only be called once:
if(!Missile::Init("megaman.png"))
exit(EXIT_FAILURE);
The image that is set is static (so is the Init-method) so you only have to call it once and every instance of the missile can use that image. I would put it in the beginning of main().
-
I should be able to use App.Draw now, right?
also what does Missile m; do ?
-
It creates an instance of the missile class, you have to add some methods to the missile class to be able to use it properly. You need a way to set its position etc.