Hello,
the title of the post is not very to explicit so I’ll to be precise.I have the following code
// in gameGlobals.h
struct GameGlobals
{
public:
static Vector<cocos2d::Sprite*> enemies;
}
// in gameGlobals.cpp
Vector<Sprite*> GameGlobals::enemies;
Basically I have a vector of pointers to store the enemies that appear in game.
I have a class called Enemy that push to that vector the sprite when an enemy is created. I printed out the size of the vector after the push to check that it increases. So far so good.
Now I want to use that vector (Vector<Sprite*> GameGlobals::enemies;), in the constructor of another class. So if somewhere it has been modifyied, say that a bullet has deleted an enemy from the vector,
the class will know. So I passed a vector paramater to the constructor:
// someClass.cpp
SomeClass::SomeClass(Vector<Sprite*> enemies)
{
_enemies = enemies; // _enemies being a private variable in the header
// cocos2d::Vector<cocos2d::Sprite*> _enemies ;
}
// and I have this method too
void SomeClass::checkSizes()
{
CCLOG("%i <", _enemies.size());
CCLOG("%i <<<<", GameGlobals::enemies.size());
}
okey, so now in the helloWorld file I test it. So I initiallize the class in the init. The init also has a scheduler that creates enemies every 5 seconds. So then in the update method of HelloWorld i run SomeClass::checkSizes() and what I expect is that both sizes _enemies.size() and GameGlobals::enemies.size() are the same since I am passing that same parameter. This is the code
bool HelloWorld::init()
{
//...
SomeClass::SomeClass(GameGlobals::enemies);
// ... then I have the scedule create enemy instance
}
void HellowWorld::update(float dt)
{
SomeClass::checkSizes();
}
But something when wrong because while the size of GameGlobals::enemies keeps increasing as expected, the result is that the size of _enemies is always zero.
Why is that? Since I did this
bool HelloWorld::init()
{
SomeClass::SomeClass(GameGlobals::enemies)
}
and someClass.cpp I defined it as
SomeClass::SomeClass(Vector<Sprite*> enemies)
{
_enemies = enemies;
}
why is it not the same? I am sure it is some silly mistake but I can’t see it.
Thanks,
R