如何使用cpp在cocos2dx android中找到Sprite的中心点

时间:2015-05-13 08:07:38

标签: cocos2d-x cocos2d-x-3.0 cocos2d-android

我是cocos2dx安卓游戏开发的新手,我想知道如何在cocos2dx中获得sprite的中心点。我使用的是3.3版。

让我解释一下这个问题我有一个调度程序,每隔5秒调用一次我的函数。它将改变冲刺的位置。现在通过这个精灵,我想把另一个精灵放在这个精灵上面。我想知道如何在cocos2dx中找到运行sprint的中心点。

任何帮助欣赏:)

谢谢

3 个答案:

答案 0 :(得分:0)

以下代码将精灵的视觉中心置于一个点上。

//your sprite
Sprite * spr = Sprite::create(...);

//a point you want to center the sprite on
Point pointToCenterOn;

//sprite should have 0.5,0.5 pivot in order to have it's center in a specific coordinate
//this code will set sprite's position to the desired point
spr->setPosition(pointToCenterOn);

如果你想每帧都这样做 - 在更新中使用此代码或安排回调以便不断运行。

答案 1 :(得分:0)

如果我理解你的问题,那么你想放置精灵 B作为精灵A的孩子。 试试这样的事情,

Sprite* parent = Sprite::create("MyTexture.png");
parent->setposition(Vec2(100,100));
this->addChild(parent,2);

Sprite* child = Sprite::create("MyTexture2.png");
child->setposition(Vec2(parent->getBoundingBox().size.width/2,parent->getBoundingBox().size.height/2));
parent->addChild(child,2);

这会将孩子准确地放在父母的中心,移动父母也会导致孩子移动,

答案 2 :(得分:0)

void yourLayer::setBg() {
    bg1 = CCSprite::create("menu/menu_bg1.jpg");
    bg1->setAnchorPoint(ccp(0, 0.5));
    bg1->setPosition(ccp(0, visibleSize.height / 2 + origin.y));

    lowerPart1 = CCSprite::create("menu/lower.jpg");

    lowerPart1->setAnchorPoint(ccp(0.5,0.5));
    lowerPart1->setPosition(ccp(bg1->getContentSize().width/2,bg1->getContentSize().height/2));
    bg1->addChild(lowerPart1);
    this->addChild(bg1, 0);
}

void yourLayer::scrollBk() {
    bg1->setPosition(ccp(bg1->getPosition().x - 1, bg1->getPosition().y));


    if (bg1->getPosition().x < -bg1->boundingBox().size.width) {
        bg1->setPosition(
                ccp(0, visibleSize.height / 2 + origin.y));
    }


}

void yourLayer::update(float dt) {

    scrollBk();

}

它应该适合你...