执行setPosition cocos2d-x时,墙不会移动

时间:2015-09-29 15:54:23

标签: c++ cocos2d-x


我正在将以前用Java编写的安卓游戏转换为cocos2d-x。在这个游戏中,我需要在几个随机位置产生墙壁,所以我在我的初始化方法中调用我的精灵上的setPosition,但是x轴上的位置没有改变(它们在y轴上改变了,虽然是Oo)。
当我在onTouchBegun()上调用它并且在那里更改它时,位置会发生变化,它只对初始调用无效。我在谷歌上找不到任何相关的问题,我完全没有想法 我将添加用于在下面生成墙壁的代码:

linestring

WallPair的onTouch方法:

void WallPair::init(cocos2d::Layer *scene, float height, float width, float gapWidth, Vec2 position){
this->position = position;
this->height = height;
this->scene = scene;
this->gapWidth = gapWidth;
//Pointtrigger (obv)
pointWall = Sprite::create();
pointWall->setScale(gapWidth, height);
pointWall->setPosition(position.x, position.y);
auto pointTrigger = PhysicsBody::createBox(Size(gapWidth, height));
pointTrigger->setDynamic(false);
pointTrigger->setGravityEnable(false);
pointTrigger->setCategoryBitmask((int)PhysicsCategory::POINT);
pointTrigger->setCollisionBitmask((int)PhysicsCategory::NONE);
pointTrigger->setContactTestBitmask((int)PhysicsCategory::PLAYER);
pointWall->setPhysicsBody(pointTrigger);
scene->addChild(pointWall, 2);

leftWall = Sprite::create(texture);
leftWall->setScale(width/leftWall->getContentSize().width, height/leftWall->getContentSize().height);
Size wallSize = Size(leftWall->getContentSize().width * leftWall->getScaleX(), leftWall->getContentSize().height * leftWall->getScaleY());
std::cout << "POSRES: " << (position.x - gapWidth - wallSize.width) << " ::: LEFT;" << std::endl;

std::cout << "LPOS: " << leftWall->getPosition().x << ", LSIZE: " << wallSize.width << std::endl;
std::cout << "SCALE: " << leftWall->getScaleX() << ", " << leftWall->getScaleY() << std::endl;
  // std::cout << "contentSizeLW: " << leftWall->getContentSize().width << " pos: " << (position.x - gapWidth - leftWall->getContentSize().width/2) <<", SCALE: " << leftWall->getScaleY()<<std::endl;
//leftWall->setContentSize(Size(leftWall->getContentSize().width, height));
scene->addChild(leftWall, 2);
auto pbl = PhysicsBody::createBox(wallSize, PhysicsMaterial(0.1f, 1.0f, 1.0f));
pbl->setDynamic(false);
pbl->setGravityEnable(false);
pbl->setCategoryBitmask((int)PhysicsCategory::WALL);
pbl->setCollisionBitmask((int)PhysicsCategory::PLAYER);
pbl->setContactTestBitmask((int)PhysicsCategory::PLAYER);
leftWall->setPhysicsBody(pbl);
leftWall->setPosition(position.x - gapWidth - wallSize.width, position.y);


rightWall = Sprite::create(texture);
rightWall->setScale(width/rightWall->getContentSize().width, height/rightWall->getContentSize().height);
wallSize = Size(rightWall->getContentSize().width * rightWall->getScaleX(), rightWall->getContentSize().height * rightWall->getScaleY());
std::cout << "POSRES: " << (position.x + gapWidth + wallSize.width) << ":::RIGHT;" << std::endl;
//rightWall->setContentSize(Size(rightWall->getContentSize().width * rightWall->getScaleX(), height));


std::cout << "RPOS: " << rightWall->getPosition().x << std::endl;
//rightWall->setContentSize(Size(rightWall->getContentSize().width, height));
 //  std::cout << "contentSizeRW: " << rightWall->getContentSize().width << " pos: " << (position.x + gapWidth + rightWall->getContentSize().width/2) << ", SCALE: " << rightWall->getScaleY() <<std::endl;
scene->addChild(rightWall, 2);
pbl = PhysicsBody::createBox(wallSize, PhysicsMaterial(0.1f, 1.0f, 1.0f));
pbl->setDynamic(false);
pbl->setGravityEnable(false);
pbl->setCategoryBitmask((int)PhysicsCategory::WALL);
pbl->setCollisionBitmask((int)PhysicsCategory::PLAYER);
pbl->setContactTestBitmask((int)PhysicsCategory::PLAYER);

rightWall->setPhysicsBody(pbl);
rightWall->setPosition(position.x + gapWidth + wallSize.width, position.y);


}

编辑:
只是为了澄清:
问题不在于触摸方法。这工作正常。问题是WallPair :: init()。即使getPosition告诉我它们应该遍布整个地方(范围从-500到+1500),它也会将精灵放在0的xpos上。但他们仍然在0。 提前谢谢!

1 个答案:

答案 0 :(得分:0)

因此经过大量的时间谷歌搜索,调试,测试和编码我发现了问题。 如果我在替换剧场时使用过渡,我无法用物理体手动设置精灵的位置,但我能够在没有过渡的情况下这样做。

我所做的就是更改replaceScene代码:

from:
    director->replaceScene(TransitionFlipX::create(2, scene));
to:
    director->replaceScene(scene);

它现在有效!