LayerAnchorPoint

  • Post author:
  • Post category:其他

LayerAnchorPoint

#include “cocos2d.h”

USING_NS_CC;

 

 

class T01LayerAnchorPoint:public CCLayer

{

public:

static T01LayerAnchorPoint * create();

bool init();

static CCScene *scene();

void draw();

void mySchedule(float dt);

CCSprite *spr;

};

#endif

#include “T01LayerAnchorPoint.h”

 

T01LayerAnchorPoint * T01LayerAnchorPoint::create()

{

创建对象

T01LayerAnchorPoint *pRet = new T01LayerAnchorPoint();

初始化和内存托管

if (pRet && pRet->init())

{

pRet->autorelease();

}

else

{

delete pRet;

pRet = NULL;

}

return pRet;

 

}

 

初始化

bool T01LayerAnchorPoint::init()

{

if (!CCLayer::init())

{

return false;

}

获取位置

//get windows size form director

CCSize winSize = CCDirector::sharedDirector()->getWinSize();

 

创建sprite

CCSprite *spr =CCSprite::create(“anchor3.png”);

//spr = CCSprite::create(“anchor1.png”);

 

设置锚点位置

spr->setAnchorPoint(ccp(0.65, 0.5)); //锚点

忽略锚点

//spr->ignoreAnchorPointForPosition(true); // 忽略锚点 默认的是sprite 锚点至为 (0,0)

设置位置

spr->setPosition(ccp(winSize.width / 2,winSize.height / 2)); //位置

 

放大

spr->setScale(1.0); //放大

定时器

//schedule(schedule_selector(T01LayerAnchorPoint::mySchedule),1); // 定时器 函数回调

 

添加sprite到cclayer

this->addChild(spr);

 

return true;

}

 

回调函数

//定时器

void T01LayerAnchorPoint::mySchedule(float dt)

{

static float ro = 0; //每次调用旋转的度数

ro += 6;

spr->setRotation(ro);

}

 

创建scene

CCScene *T01LayerAnchorPoint::scene()

{

CCScene *scene = CCScene::create();

//scene->setAnchorPoint(ccp(0,0));

创建layer

T01LayerAnchorPoint *layer =T01LayerAnchorPoint::create();

//CCLog(“x = %f ,y = %f “,layer->getPositionX(), layer->getPositionY());

//layer->ignoreAnchorPointForPosition(false);

添加layer到scene

scene->addChild(layer);

return scene;

}

 

 

//over load  画直线

void T01LayerAnchorPoint::draw()

{

CCSize winSize =CCDirector::sharedDirector()->getWinSize();

设置直线的颜色

ccDrawColor4B(255,0,0,255);

两个坐标划线

ccDrawLine(ccp(0,winSize.height/2),ccp(winSize.width,winSize.height/2));

ccDrawLine(ccp(winSize.width/2, 0),ccp(winSize.width/2, winSize.height ));

}


版权声明:本文为chengjing002原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。