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

Author Topic: Rendering an instanced class object  (Read 2226 times)

0 Members and 1 Guest are viewing this topic.

Nick

  • Newbie
  • *
  • Posts: 2
    • View Profile
Rendering an instanced class object
« 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

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Rendering an instanced class object
« Reply #1 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Nick

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Rendering an instanced class object
« Reply #2 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!

 

anything