Diego Diego - 1 month ago 19
C++ Question

How to store Points in an Array Cocos2d-x C++

I'm using Cocos2d-x and I would like to know how to store Points in an array.

myClass.h
Vector<Point*> _pointArray; //Using the cocos class Vector Im getting really confused about how to declare this Points container.

myClass.cpp

//Some trigger in the implementation will populate the array
int count;
int i;
float coinPosX;
float coinPosY;

Point point1 = Vec2(0.8f, 0.2f);
_pointArray.pushBack(point1);

Point point2 = Vec2(15.0f, 10.0f);
_pointArray.pushBack(point2);

count = (int)_pointArray.size();

for (i = 0; i < count; i++){
auto coin = Sprite::create("coin.png");
coin->setPosition(Vec2( _pointArray.at(i).x, _pointArray.at(i).y));
this->addChild(coin);

}


The problem is this error with the pushBack method:

Error: No instance of overloaded function ‘’cocos2d::Vector::pushback[with T=cocos2d::Sprite*]
matches the argument list

arguments types are(cocos2d::Point)
object type is cocos2d::Vector;

The argument is not right, so I¡m getting lost, Whats wrong with this, and whats the right way to store Points in an array that I can iterate and get its data. Thanks for any guideline . Greetings.

Answer

You can't store it like this. Vector is cocos2d-x which requires cocos2d-x objects, which extend Ref class. Instead you can use vector (lower case) like this:

vector<Point> _pointArray;
_pointArray.push_back(point1);
Comments