【问题标题】:Cocos2d-x Touch Event coordinate systemCocos2d-x Touch Event 坐标系
【发布时间】:2015-09-05 14:26:09
【问题描述】:

我使用的是 cocos2d-x 3.7.1

我的场景中有一个节点,我正在向该节点添加子节点。 (HexField 是 Node 的子类)

int rhombusSizeX = 1;
int rhombusSizeY = 2;

for (int y = 0; y < rhombusSizeY; ++y){
    for (int x = 0; x < rhombusSizeX; ++x){
        HexField* field = HexField::create();
        field->setPosition(Vec2(x*30 + y*15, y*30));
        field->setName("HexField " + to_string(x) + "," + to_string(y));

        auto listener = EventListenerTouchOneByOne::create();
        listener->setSwallowTouches(true);
        listener->onTouchBegan = CC_CALLBACK_2(HexField::onTouchBegan, field);
        listener->onTouchMoved = CC_CALLBACK_2(HexField::onTouchMoved, field);
        listener->onTouchEnded = CC_CALLBACK_2(HexField::onTouchEnded, field);
        Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, field);

        this->addChild(field, 1);
    }
}

如果只添加一个HexField

int rhombusSizeX = 1;
int rhombusSizeY = 1;

HexField::onTouchBegan 中的 touch-&gt;getLocation() 在世界坐标中按预期报告。

如果添加了多个HexField

int rhombusSizeX = 5;
int rhombusSizeY = 5;

touch-&gt;getLocation() 返回相对于“前一个”添加的 HexField 的坐标,在本例中为 HexField 3,4

为什么会这样?是bug吗?

【问题讨论】:

    标签: c++ c++11 cocos2d-x cocos2d-x-3.0


    【解决方案1】:

    我现在找到答案了。

    这一切都是因为我没有打电话:

    Director::getInstance()->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
    

    之后:

    Director::getInstance()->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
    Director::getInstance()->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, transform);
    

    在我的绘图函数中。

    看起来该错误已升级到我程序的其他部分,导致一些奇怪的行为。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多