Hi there, I’m getting use to the Classes in Cocos and the C++ syntax and I’m a little confused about how can I store Points in an array, or use the Vector Class to do something like -seudo code-: PointArray* pointArray = {}, and then be able to iterate the array and get the stored info.
myClass.h
// Create a Pointer to the Sprite Class in my Class head
`Vector<Sprite*> _pointArray;`
myClass.cpp
//Some trigger in the implementation will populate the array
int count;
int i;
float coinPosX;
float coinPosY;
Point point = Point(Vec2(0.8f, 0.2f));
_pointArray.pushBack(point);
Point point = Point(Vec2(0.5f, 0.5f));
_pointArray.pushBack(point);
Point point = Point(Vec2(0.2f, 0.8f));
_pointArray.pushBack(point);
count = (int)_pointArray.size();
for (i = 1; i < count; i++){
auto coinPos = _pointArray.at(i);
coinPosX = coinPos.x;
coinPosY = coinPos.y;
auto coin = Sprite::create("coin.png");
coin->setPosition(Vec2(coinPosX, coinPosY));
this->addChild(coin);
}
Thanks for any guideline on this topic. Greetings.
Just for you to know I declared the point just once, and then I just changed it values because there is an error of multiple instantiation of the Point point.
Point point = Point(Vec2(0.8f, 0.2f));
_pointArray.pushBack(point);
point = Point(Vec2(0.5f, 0.5f));
_pointArray.pushBack(point);
point = Point(Vec2(0.2f, 0.8f));
_pointArray.pushBack(point);
This is the error. I’m using Visual Studio and It’s marking the error exactly at the point before the method pushback
_pointArray.pushBack(point);
Error: No instance of overloaded function ‘’cocos2d::Vector<t>::pushback[with T=cocos2d::Sprite*]
matches the argument list
arguments types are(cocos2d::Point)
object type is cocos2d::Vector<cocos2d::Sprite*>;
What could it be the problem? Thanks for your time.
You are storing cocos2d::Point “pointers” not the values.
You are creating Point which is not a pointer (without the *).
Solution:
use cocos2d::Vector<cocos2d::Point>
instead of cocos2d::Vector<cocos2d::Point*>
No, I’m not trying to store Points into Sprites, I just don’t know how to store points and I started with Sprites. I have not a strong background on C++, and this <> is kind weird to me and I’m getting used to the whole thing Cocos2d + C++ . So std:vector is like an all terrain container, that’s great to know, How can I declare this in my head? Greetings
Seudo code: PointsContainer or Points Array* _pointsArray; So I can store points or vectors.
I’ve overview the reading and I can see that Vectors can work as an array, thanks for the guideline. In this case this vector contains an array that expects Points:
Vector<Point> _pointArray;
The implementation is still the same but I got this error:
Invalid Type for cocos2d::Vector<T>!
int count;
int i;
float coinPosX;
float coinPosY;
Point point = Point(Vec2(0.8f, 0.2f));
_pointArray.pushBack(point);
point = Point(Vec2(0.5f, 0.5f));
_pointArray.pushBack(point);
point = Point(Vec2(0.2f, 0.8f));
_pointArray.pushBack(point);
count = (int)_pointArray.size();
for (i = 1; i < count; i++){
auto coinPos = _pointArray.at(i);
coinPosX = coinPos.x;
coinPosY = coinPos.y;
auto coin = Sprite::create("coin.png");
coin->setPosition(Vec2(coinPosX, coinPosY));
this->addChild(coin);
}
Oh yeah, I’ve seen that the Cocos Vector Class works slightly different than the Std class. So I declared std::vector<Point> _pointArray in my head; but pushBack is not a member of the std::vector class so there is an error telling me so. Thanks for any guideline.