码迷,mamicode.com
首页 > 其他好文 > 详细

1.04 锚点

时间:2016-06-15 09:11:08      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

1.04 锚点

技术分享
技术分享

一、锚点影响了什么

    1、锚点影响 放置位置
    2、锚点影响 缩放 --- 缩放是围绕锚点旋转的
    3、锚点影响 旋转 --- 通常情况下,旋转是围绕锚点进行

二、忽略锚点

    1、忽略锚点 --- 将锚点置成 0,0
    2、通常情况下,Scene 是忽略锚点的


三、源代码

1、头文件
  1. #ifndef _T01LayerAnchorPoint_H_
  2. #define _T01LayerAnchorPoint_H_
  3. #include "cocos2d.h"
  4. USING_NS_CC;
  5. //锚点
  6. class T01LayerAnchorPoint:public CCLayer
  7. {
  8. public:
  9. static T01LayerAnchorPoint* create( );
  10. static CCScene* scene( );
  11. bool init( );
  12. virtual void draw( );
  13. virtual void mySchedule( float dt );
  14. private:
  15. CCSprite* spr;//定时器需要
  16. };
  17. #endif

2、源文件
  1. #include "T01LayerAnchorPoint.h"
  2. CCScene* T01LayerAnchorPoint::scene( )
  3. {
  4. CCScene* scene = CCScene::create( );
  5. T01LayerAnchorPoint* layer = T01LayerAnchorPoint::create( );
  6. //通常情况下,Scene 是忽略锚点的
  7. //layer->ignoreAnchorPointForPosition( false ); //不忽略锚点
  8. //CCLog( "x = %f, y = %f", layer->getPositionX( ), layer->getPositionY( ) );
  9. scene->addChild( layer );
  10. return scene;
  11. }
  12. T01LayerAnchorPoint* T01LayerAnchorPoint::create( )
  13. {
  14. T01LayerAnchorPoint* pRet = new T01LayerAnchorPoint;
  15. if (pRet && pRet->init())
  16. {
  17. pRet->autorelease( );
  18. }
  19. else
  20. {
  21. delete pRet;
  22. pRet = NULL;
  23. }
  24. return pRet;
  25. }
  26. bool T01LayerAnchorPoint::init( )
  27. {
  28. CCLayer::init( ); //父类初始化
  29. //主战场
  30. CCSize winSize = CCDirector::sharedDirector( )->getWinSize( );
  31. spr = CCSprite::create( "anchor3.png" );
  32. //锚点影响 放置位置
  33. //spr->setAnchorPoint( ccp( 0, 0 ) );
  34. spr->setAnchorPoint( ccp( 0, 1 ) );
  35. //spr->setAnchorPoint( ccp( 1, 0 ) );
  36. //spr->setAnchorPoint( ccp( 1, 1 ) );
  37. //锚点影响 缩放 --- 缩放是围绕锚点旋转的
  38. //spr->setAnchorPoint( ccp( 0.5, 0.5 ) );
  39. //spr->setAnchorPoint( ccp( 0.656, 0.5 ) );
  40. //spr->setScale( 5.0f ); //缩放
  41. //忽略锚点 --- 将锚点置成 0,0
  42. spr->ignoreAnchorPointForPosition( false );
  43. spr->setPosition( ccp( winSize.width / 2, winSize.height / 2 ) );
  44. this->addChild( spr );
  45. //锚点影响 旋转 --- 通常情况下,旋转是围绕锚点进行
  46. //定时器
  47. //typedef void (CCObject::*SEL_SCHEDULE)(float);
  48. //schedule( schedule_selector(T01LayerAnchorPoint::mySchedule ), 2 ); //2秒钟触发
  49. return true;
  50. }
  51. void T01LayerAnchorPoint::draw( )
  52. {
  53. //draw 绘图
  54. CCSize winSize = CCDirector::sharedDirector( )->getWinSize( );
  55. ccDrawColor4B( 255, 0, 0, 255 );
  56. ccDrawLine( ccp( 0, winSize.height / 2 ), ccp( winSize.width, winSize.height/2) );
  57. ccDrawLine( ccp( winSize.width / 2, 0 ), ccp( winSize.width/2 , winSize.height) );
  58. }
  59. void T01LayerAnchorPoint::mySchedule( float dt )
  60. {
  61. static float ro = 0;
  62. ro += 30;
  63. spr->setRotation( ro );
  64. }







1.04 锚点

标签:

原文地址:http://www.cnblogs.com/nfking/p/5586141.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!