Collecting Points in Arrays?

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.

You are storing them just fine. Are you having a specific problem?

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.

Sure. You have told the compiler to expect that this Vector holds Sprite objects. But you are putting Point objects in it. A Point is not a Sprite

Yep, so I tried Point instead Sprite like: Vector<Point*>

and there was the same error, but now with Point

cocos2d::Vector<cocos2d::Point*>

And then I tried just with Sprite and Point in the Header and I have the error that Expression must have a Class type

Sprite* _pointArray;
//Then
Point* _pointArray;

So what could it be the right way to create a Points Array?

Why are you even trying Sprite?

just use an std::vector. It can store whatever you want.

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*>

and not Sprite!!

I am not sure why he even trying to store “cocos2d::Point” in Sprite* vector, that doesn’t make sence (unless I missed while he edited something?)

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++ :slight_smile: . 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.

No you were trying to store Point into a Vector that was expecting Sprite i.e you declared this this:

Vector<Sprite*> _pointArray;

and were then trying to stuff a Point into it:

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

You declare an std::vector the same way you do a Cocos2d::Vector.

Please review: http://www.cplusplus.com/reference/vector/vector/

1 Like

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);

        }

you need to specify std::vector that error tells me you are still declaring a Cocos2d::Vector

Also, take a look at cpp-tests for help

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.

your answer is in that link I posted about std::vector.

You are going to have a very hard time if you don’t learn more c++ or at least know how to start solving your problems when they arise.

Thanks for your time. Greetings.