cocos2d-x中Node中重要的属性

Node还有两个非常重要的属性:position和anchorPoint。

position(位置)属性是Node对象的实际位置。position属性往往还要配合使用anchorPoint属性,为了将一个Node对象(标准矩形图形)精准的放置在屏幕某一个位置上,需要设置该矩形的锚点,anchorPoint是相对于position的比例,默认是(0.5,0.5)。我们看看下面的几种情况:

以anchorPoint为(0.5,0.5)为例,这是默认情况。

 

下面是anchorPoint为(0.0,0.0)情况。

 

下面是anchorPoint为(1.0,1.0)情况。

 

下面是anchorPoint为(0.5,0.66)情况。

 

为了进一步了解anchorPoint使用,我们修改HelloWorld实例,修改HelloWorldScene.cpp的HelloWorld::init()函数如下,其中加粗字体显示的是我们添加的代码。

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <span style="font-size:14px;">bool HelloWorld::init()  
  2. {  
  3.    … …  
  4.    
  5.    auto label = LabelTTF::create("Hello World","Arial", 24);                                                   
  6.    label->setPosition(Point(origin.x + visibleSize.width/2,  
  7.                             origin.y +visibleSize.height - label->getContentSize().height));        
  8.      
  9. label->setAnchorPoint( Point(1.0, 1.0) );  
  10.    
  11.    this->addChild(label, 1);                                                                                                                 
  12.    
  13.    auto sprite = Sprite::create("HelloWorld.png");                                                                            
  14.    sprite->setPosition(Point(visibleSize.width/2 + origin.x,visibleSize.height/2 + origin.y));      
  15.    this->addChild(sprite, 0);                                                                                                               
  16.      
  17.    return true;  
  18. }</span>  

 

Hello World设置了anchorPoint为(1.0,1.0)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值