Search code examples
c++arrayscocos2d-x

How to place custom class object into CCArray?


Currently it seems I need to maintain two arrays. One for the array of sprites of my object "Customer", and one for the array of actual customer objects.

I have no problem using CCArray for the customer sprites (CCSprite).

However I can't seem to add my Customer objects into a CCArray.

Here is some code for my Customer class:

class Customer
{
    int tag;
    double moneyCurrent;
    double moneyBanked;
public:
    Customer();
    void setTag(int);
    void setMoneyCurrent(double);
    void setMoneyBanked(double);
    int getTag();
    double getMoneyCurrent();
    double getMoneyBanked();
};

Here is some code where I attempt to add the customer to the CCArray:

void MainGame::createNewCustomer(int i)
{
    Customer* newCustomer = new Customer();
    newCustomer->setTag(i);
    CCObject* newCustomerObject = (CCObject *)newCustomer;
    _customers->addObject(newCustomerObject);
}

It receives a compiler error. It stems from my attempt to cast the new Customer object into a CCObject.

I'm not entirely sure if my original idea is wrong (maybe I can't use CCArray here), or if I'm doing something else wrong.


Solution

  • CCArray is for Cocos2d objects only. You will either need to change your Customer class to derive from CCObject or just use a std::vector or some other array structure to house your Customer class.