Wednesday, June 29, 2022
HomeGame DevelopmentAttempting to reference a vector of sprites - C++

Attempting to reference a vector of sprites – C++


Whats up,
the title of the publish shouldn’t be very to express so I’ll to be exact.I’ve the next code

// in gameGlobals.h
struct GameGlobals
{
public:
    static Vector<cocos2d::Sprite*> enemies;
}

// in gameGlobals.cpp
Vector<Sprite*> GameGlobals::enemies;

Principally I’ve a vector of tips that could retailer the enemies that seem in sport.
I’ve a category referred to as Enemy that push to that vector the sprite when an enemy is created. I printed out the dimensions of the vector after the push to examine that it will increase. Up to now so good.

Now I need to use that vector (Vector<Sprite*> GameGlobals::enemies;), within the constructor of one other class. So if someplace it has been modifyied, say {that a} bullet has deleted an enemy from the vector,
the category will know. So I handed a vector paramater to the constructor:

// someClass.cpp
SomeClass::SomeClass(Vector<Sprite*> enemies)
{
    _enemies = enemies;  // _enemies being a personal variable within the header   
                         // cocos2d::Vector<cocos2d::Sprite*> _enemies ;
}

// and I've this technique too
void SomeClass::checkSizes()
{
        CCLOG("%i <", _enemies.dimension());
		CCLOG("%i <<<<", GameGlobals::enemies.dimension());
}

okey, so now within the helloWorld file I check it. So I initiallize the category within the init. The init additionally has a scheduler that creates enemies each 5 seconds. So then within the replace technique of HelloWorld i run SomeClass::checkSizes() and what I anticipate is that each sizes _enemies.dimension() and GameGlobals::enemies.dimension() are the identical since I’m passing that very same parameter. That is the code

bool HelloWorld::init()
{
    //...        
    SomeClass::SomeClass(GameGlobals::enemies);
   // ... then I've the scedule create enemy occasion
}

void HellowWorld::replace(float dt)
{
    SomeClass::checkSizes();
}

However one thing when fallacious as a result of whereas the dimensions of GameGlobals::enemies retains rising as anticipated, the result’s that the dimensions of _enemies is at all times zero.
Why is that? Since I did this

bool HelloWorld::init()
{
    SomeClass::SomeClass(GameGlobals::enemies)
}

and someClass.cpp I outlined it as

SomeClass::SomeClass(Vector<Sprite*> enemies)
{
    _enemies = enemies; 
}

why is it not the identical? I’m certain it’s some foolish mistake however I can’t see it.
Thanks,
R


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments