创建一个层T1LayerAnchorPoint
- AppDelegate.cpp
1 bool AppDelegate::applicationDidFinishLaunching() { 2 // initialize director 3 auto director = Director::getInstance(); 4 auto glview = director->getOpenGLView(); 5 if(!glview) { 6 glview = GLViewImpl::createWithRect("MyLayer", Rect(0, 0, 960, 640)); 7 director->setOpenGLView(glview); 8 } 9 10 director->getOpenGLView()->setDesignResolutionSize(960, 640, ResolutionPolicy::SHOW_ALL); 11 12 // turn on display FPS 13 director->setDisplayStats(true); 14 15 // set FPS. the default value is 1.0/60 if you don't call this 16 director->setAnimationInterval(1.0 / 60); 17 18 FileUtils::getInstance()->addSearchPath("res"); 19 20 // create a scene. it's an autorelease object 21 //auto scene = HelloWorld::createScene(); 22 23 //CCScene *pScene = MyScene::create(); 24 CCScene *pScene = T1LayerAnchorPoint::scene(); 25 26 // run 27 director->runWithScene(pScene); 28 29 return true; 30 }
- T1LayerAnchorPoint.h
1 #pragma once 2 #include "cocos2d.h" 3 USING_NS_CC; 4 5 class T1LayerAnchorPoint:public CCLayer 6 { 7 public: 8 //create->init 9 static T1LayerAnchorPoint*create(); 10 bool init(); 11 static CCScene *scene(); 12 13 //画线 14 virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4& transform, uint32_t flags); 15 };
- T1LayerAnchorPoint.cpp
1 #include "T1LayerAnchorPoint.h" 2 3 //创建层 4 T1LayerAnchorPoint*T1LayerAnchorPoint::create() 5 { 6 T1LayerAnchorPoint *pRet = new T1LayerAnchorPoint(); 7 if (pRet && pRet->init()) 8 { 9 pRet->autorelease(); 10 } 11 else 12 { 13 delete pRet; 14 pRet = NULL; 15 } 16 return pRet; 17 18 } 19 20 //初始化层 21 bool T1LayerAnchorPoint::init() 22 { 23 CCLayer::init(); 24 25 CCSize winSize = CCDirector::sharedDirector()->getWinSize(); 26 CCSprite *spr = CCSprite::create("anchor3.png"); 27 //设置锚点 28 spr->setAnchorPoint(ccp(0, 0)); 29 spr->setPosition(ccp(winSize.width / 2, winSize.height / 2)); 30 addChild(spr); 31 return true; 32 } 33 34 CCScene *T1LayerAnchorPoint::scene() 35 { 36 CCScene *scene = CCScene::create(); 37 T1LayerAnchorPoint *layer = T1LayerAnchorPoint::create(); 38 scene->addChild(layer); 39 return scene; 40 } 41 42 void T1LayerAnchorPoint::draw(cocos2d::Renderer *renderer, const cocos2d::Mat4& transform, uint32_t flags) 43 { 44 CCSize winSize = CCDirector::sharedDirector()->getWinSize(); 45 ccDrawColor4B(255, 0, 0, 255); 46 47 ccDrawLine(ccp(0, winSize.height / 2), ccp(winSize.width, winSize.height / 2)); 48 ccDrawLine(ccp(winSize.width / 2, 0), ccp(winSize.width / 2, winSize.height)); 49 }