Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How do you use the Missile class?  (Read 6020 times)

0 Members and 1 Guest are viewing this topic.

Coondog

  • Newbie
  • *
  • Posts: 5
    • View Profile
How do you use the Missile class?
« on: July 10, 2008, 10:34:07 pm »
I'm not real good with C++, how do you use the Missile Class?
Code: [Select]
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

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
How do you use the Missile class?
« Reply #1 on: July 10, 2008, 11:07:30 pm »
Call the Init-method first with the path to the image as the parameter before creating any instances of the missile class. Like this:

Code: [Select]
if(!Missile::Init("imagefile.png"))
    exit(EXIT_FAILURE);

Missile m;


I think that should work, I'm not an expert either :)

Coondog

  • Newbie
  • *
  • Posts: 5
    • View Profile
How do you use the Missile class?
« Reply #2 on: July 10, 2008, 11:20:11 pm »
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

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
How do you use the Missile class?
« Reply #3 on: July 10, 2008, 11:37:25 pm »
Try putting this before your main():

Code: [Select]
sf::Image Missile::Image;

It works for me.

Coondog

  • Newbie
  • *
  • Posts: 5
    • View Profile
How do you use the Missile class?
« Reply #4 on: July 10, 2008, 11:43:44 pm »
here is the code currently:
Code: [Select]

////////////////////////////////////////////////////////////
// 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++

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
How do you use the Missile class?
« Reply #5 on: July 10, 2008, 11:54:38 pm »
You had some misplaced curly brackets, try this:

Code: [Select]
////////////////////////////////////////////////////////////
/// 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;
}

Coondog

  • Newbie
  • *
  • Posts: 5
    • View Profile
How do you use the Missile class?
« Reply #6 on: July 10, 2008, 11:57:13 pm »
Thanks, it works  :)

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
How do you use the Missile class?
« Reply #7 on: July 11, 2008, 12:00:35 am »
Remember, this should only be called once:

Code: [Select]
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().

Coondog

  • Newbie
  • *
  • Posts: 5
    • View Profile
How do you use the Missile class?
« Reply #8 on: July 11, 2008, 12:03:35 am »
I should be able to use App.Draw now, right?
also what does Missile m; do ?

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
How do you use the Missile class?
« Reply #9 on: July 11, 2008, 12:08:16 am »
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.