Cocos2dx如何把三个Node节点或者控件放在某个节点/控件中间位置?

来源:互联网 发布:linux poll 编辑:程序博客网 时间:2024/06/05 04:11

1、先求出三个Node节点或者控件所占的宽度

2、求出三个Node节点或者控件addchild的父类的中间位置

3、设第一个节点左对齐,坐标为父类中间的位置-三个Node节点或者控件所占的宽度/2

4、设置第二个位置左对齐,坐标为第一个节点的坐标加上第一个节点坐标的宽度

5、设置第三个位置左对齐,坐标为第二个节点的坐标加上第二个节点坐标的宽度

例如

三个控件

    --宝石图标

    local gemIcon = cc.Sprite:createWithSpriteFrameName("ac_everyday_gem.png")

    gemIcon:setAnchorPoint(cc.p(0,0.5))

    gemIcon:setScale(0.85)

    bgSprite:addChild(gemIcon)

    --已充值

    local recharge_text = createLabel(getLocalString("have_recharge"),appFont,22)

    recharge_text:setAnchorPoint(cc.p(0,0.5))

    bgSprite:addChild(recharge_text)

    --充值数量

    local recharge_count = createLabel("",appFont,22)

    recharge_count:setString(string.format("  %d/60", 30))

    recharge_count:setAnchorPoint(cc.p(0,0.5))

    recharge_count:setTextColor(cc.c4b(0xff, 0xc6, 0x00, 0xff))

    bgSprite:addChild(recharge_count)

三个节点/控件坐标

    local contentSize = gemIcon:getContentSize().width*gemIcon:getScale() + recharge_text:getContentSize().width + recharge_count:getContentSize().width + 5

    gemIcon:setPosition(bgSprite:getContentSize().width/2 - contentSize/2 - 5,135)

    recharge_text:setPosition(gemIcon:getPositionX() + gemIcon:getContentSize().width*gemIcon:getScale() + 5,gemIcon:getPositionY())

    recharge_count:setPosition(recharge_text:getPositionX() + recharge_text:getContentSize().width,gemIcon:getPositionY())

阅读全文
0 0
原创粉丝点击