【问题标题】:How to find the center point of Sprite in cocos2dx android using cpp如何使用cpp在cocos2dx android中找到Sprite的中心点
【发布时间】:2015-05-13 08:07:38
【问题描述】:

我是 cocos2dx android 游戏开发的新手,我想知道如何在 cocos2dx 中获得精灵的中心点。我使用的是 3.3 版本。

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

任何帮助表示赞赏:)

谢谢

【问题讨论】:

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


    【解决方案1】:

    以下代码会将精灵的视觉中心放在一个点上。

    //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);
    

    如果您想在每一帧都执行此操作 - 在更新中使用此代码或安排回调以持续运行。

    【讨论】:

    • 默认为 0.5,是的。我的意思是建议更改它以防您的枢轴不是默认值:)
    • Point centerPoint 这是什么?
    • 我在cocos2dx中使用cpp
    • 我没看懂centerPoint
    • Size size = moveSprite->getContentSize(); sprite_tickingBombing->setPosition(Point(size.width * 0.5, size.height * 0.5));
    【解决方案2】:

    如果我理解你的问题,那么你想放置精灵 B 作为 Sprite 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);
    

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

    【讨论】:

    • 是孩子必须要加“parent->addChild(child,2);”我可以将 Sprite* 子添加为 this->addChild(child,2);
    • 如果你添加使用 this->addChild(child,2);然后这个孩子将被添加到添加父级的同一层中,您必须分别移动子级和父级,这对于大量对象来说有点混乱,如果你想这样做,那么你可以像 child->setPosition 那样做(parent->getPosition));
    【解决方案3】:
    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();
    
    }
    

    它应该适合你...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-03
      相关资源
      最近更新 更多