Problem with Vector<Vec2>

Hi,
I need resolve a very simple problem.
Why can’t I declare this vector?

Vector<Vec2> test;

The compiler says:
Invalid Type for cocos2d::Vector

I know I can use std::vector < Vec2 > but I want to use cocos2d::Vector < Vec2 >. How can I do this?
Thanks.

I believe a cocos2d::Vector must contain objects that inherit from Ref, e.g. Node. Vec2 does not inherent from Ref, so you should probably use a std::Vector. Not sure about this, but I suspect the main reason to use a cocos2d::Vector over a std::Vector is that it automatically retains Ref objects.

2 Likes

Thanks for the answer @grimfate
My first problem is this :

Vector<Node*> childs = scene->getChildren();
std::vector<Vec2> pos;
for( Vector<Node*>::iterator it = childs.begin(); it != childs.end(); it++)
  pos.push_back((*it)->getPosition());

I’m inserting in std::vector, elements of cocos2d::Vector. This works fine, but I would like use always the same type of data. I’m having problems with specific positions and I would like know if this is the problem.

What exactly is the issue you are having?

Is a very rare problem. I will try summarize it:
In my first Scene, the last line is this:
Director::getInstance()->getEventDispatcher()->removeAllEventListeners();

After this line, I replace the scene and in the new scene I add a new event listener:

auto listenerDisparador = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(GameObject::OnTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(GameObject::OnTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(GameObject::OnTouchEnded, this);

Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, sprite);

At this moment I already have many sprites in my scene. The problem is with some sprites in a specific position. For example:

If after the line:
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, sprite);

I obtain a someSprite->getPosition().x then the value is: 231,078568. This value is correct. But if within the method OnTouchBegan I obtain someSprite->getPosition().x then the value is: 231,078583. The value was changed without me doing anything. I can not understand the source of the problem. Please help me. Thanks.

If you do not move the sprite, then I have no idea what it could be.

I don’t move the sprite. I think is an unexpected error caused by another problem, but I can’t identify the source problem. Maybe a bad handling of the listeners.

I’ve already had unexpected problems with the listeners. On one occasion the sprites were duplicated, and I had to add the method removeAllEventListeners() before replace scene. Maybe this problem is similar. Anyone have any idea?

Thanks.

There may be some floating point precision problems since float has 7± correct digits (231.078568 vs. 231.078583).
One debugging idea is to subclass that sprite and override setPosition methods with some breakpoints.

Thanks for the answer @enrevol, but, how can I solve the problem? I thought take the first 4 decimals, but maybe it can cause future problems…

Finally I solved the problem considering a delta error to compare floats.
Thanks to all.