新手 做flappybird的时候发生问题
<pre class="brush:cpp; toolbar: true; auto-links: false;">
bool _pipe::init()
{
auto bRet = 0;
do {
if (!Node::init())
{
return false;
}
SpriteFrameCache* cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("pip.plist", "pip.png");
// create both pipes and set texture
_pipeup = Sprite::createWithSpriteFrameName("pipedown.png");
_pipeup->setTag(TAG_PIPEUP);
_pipedown = Sprite::createWithSpriteFrameName("pipeup.png");
_pipedown->setTag(TAG_PIPEDOWN);
this->addChild(_pipeup);
this->addChild(_pipedown);
this->setPosition(Point(pipes_x, 0));
this->refresh();
//set pipe's x position
auto half_pipe_width = _pipeup->getContentSize().width / 2;
_pipeup->setPositionX(half_pipe_width);
_pipedown->setPositionX(half_pipe_width);
//set body 加了body后坐标发生问题
auto pipe_body = PhysicsBody::create();
auto shapeBox = PhysicsShapeBox::create(_pipeup->getContentSize());
pipe_body->addShape(shapeBox, false);
pipe_body->setDynamic(false);
_pipeup->setPhysicsBody(pipe_body);
pipe_body = PhysicsBody::create();
shapeBox = PhysicsShapeBox::create(_pipedown->getContentSize());
pipe_body->addShape(shapeBox, false);
pipe_body->setDynamic(false);
_pipedown->setPhysicsBody(pipe_body);
bRet = 1;
} while (0);
return bRet;
}
_pipe继承于node,然后pipeup,pipedown都作为child添加进_pipe内
这样的话如果我_pipeup->setPositionX(half_pipe_width);的话在场景中的坐标应该是(half_pipe_width+pipe_x)了吧
可运行一看管子卡在了(half_pipe_width,0)上,没有相对于父结点偏移
而当我注释掉set body的语句的时候又恢复正常了
这是为什么啊