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

Author Topic: Dynamically allocating Sprite  (Read 1767 times)

0 Members and 1 Guest are viewing this topic.

NRaf

  • Newbie
  • *
  • Posts: 2
    • View Profile
Dynamically allocating Sprite
« on: June 29, 2009, 03:25:47 am »
I've created a Vector of type <sf::Drawable*>. I've just started playing around with SFML so I'm still learning the ropes.

Anyway, the Vector is stored in it's own class (think of it as a sprite [or drawable] manager class). I'm creating the Sprite objects in main and then adding them to the Drawable class.

When I try to access the files in the vector, however, I get segfaults when I try and draw them. I think it may be because the Sprites are going out of scope. This is the actual code I'm using to create the sprites:

Code: [Select]

sf::Sprite sprite;

for (int i = 0; i < size; i++)
{
sprite = sf::Sprite();
sprite.SetImage(images[rand() % 3]); //Choosing between 1 of 3 Image objects
sprite.SetPosition(rand() % 800, rand() % 600);

layer.addToLayer(dynamic_cast<sf::Drawable*>(&sprite));
}


If my understanding is correct, the line sprite = sf::Sprite() is creating the object on the stack, not the heap, which is the root of my problems.

I've tried to remedy this by sprite = new sf::Sprite() but I get the error:
main.cpp:108: error: no match for ‘operator=’ in ‘sprite = (((sf::Sprite*)operator new(196u)), (<statement>, <anonymous>))’
/usr/include/SFML/Graphics/Sprite.hpp:45: note: candidates are: sf::Sprite& sf::Sprite::operator=(const sf::Sprite&)

Any ideas?

NRaf

  • Newbie
  • *
  • Posts: 2
    • View Profile
Dynamically allocating Sprite
« Reply #1 on: June 29, 2009, 03:40:08 am »
Nevermind, I was meant to make sprite a pointer of Sprite...  :lol: