SFML community forums

Help => Graphics => Topic started by: Nick on April 02, 2013, 11:23:53 pm

Title: Rendering an instanced class object
Post by: Nick on April 02, 2013, 11:23:53 pm
Hi there,

I'm trying to create an instanced class object (I don't know if it even works) when the mouse is clicked, so therefore I'll be able to shoot multiple bullets. I'm new to classes and hardly understand it, so please don't be too harsh! ;)

This is the bullet class:
        class Bullet
        {
        public:

                sf::Sprite sprite;
                float xPosition;
                float yPosition;
                float xVelocity;
                float yVelocity;

                void Bullet::Shape()
                {
                        sf::RectangleShape bullet;
                        bullet.setSize(sf::Vector2f(1,1));
                        bullet.setFillColor(sf::Color::Black);
                        bullet.setPosition(xPosition, yPosition);
                        bullet.move(xVelocity, yVelocity);
                }
        };
        Bullet bullet[10];

Then this actually shoots it (well, it should do but I haven't got to testing that yet...)
//Shooting
                if (leftClick == true)
                {                      
                        //Get bullet position  
                        bulletOpposite = sin(gunRotationAngle * PI/180) * 5;
                        bulletAdjacent = cos(gunRotationAngle * PI/180) * 5;

                        //Set position
                        bullet[bulletNumber].xPosition = gun.getPosition().x + bulletAdjacent;
                        bullet[bulletNumber].yPosition = gun.getPosition().y + bulletOpposite;

                        //Set velocity
                        bullet[bulletNumber].xVelocity = bulletAdjacent;
                        bullet[bulletNumber].yVelocity = bulletOpposite;

                        bulletNumber += 1;

                        leftClick = false;
                }

I've calculated the location of where the bullet is and the velocity from the angle of the gun (which is a 5 x 1 pixel 'object').

Then I try to render it...
window.draw(bullet[bulletNumber]);

But get an error: Error   5   error C2664: 'void sf::RenderTarget::draw(const sf::Drawable &,const sf::RenderStates &)' : cannot convert parameter 1 from 'Bullet [10]' to 'const sf::Drawable &'

Again, I don't totally understand classes, but I appreciate anybody's help!

Thanks! :D
Title: Re: Rendering an instanced class object
Post by: Nexus on April 02, 2013, 11:36:10 pm
You should really read a good C++ book and learn more about the basics. Your method definition is wrong, and you create useless local variables. Instead of the global array, you should use an STL container as a local variable. Then you can't just pass arbitrary objects to draw(), please read SFML's tutorials and documentation in order to see which classes are drawable.
Title: Re: Rendering an instanced class object
Post by: Nick on April 02, 2013, 11:41:37 pm
Right, I'll leave out this until I get a bit better then...

Thanks for the reply!