标签:
1.04 锚点
#ifndef _T01LayerAnchorPoint_H_
#define _T01LayerAnchorPoint_H_
#include "cocos2d.h"
USING_NS_CC;
//锚点
class T01LayerAnchorPoint:public CCLayer
{
public:
static T01LayerAnchorPoint* create( );
static CCScene* scene( );
bool init( );
virtual void draw( );
virtual void mySchedule( float dt );
private:
CCSprite* spr;//定时器需要
};
#endif
#include "T01LayerAnchorPoint.h"
CCScene* T01LayerAnchorPoint::scene( )
{
CCScene* scene = CCScene::create( );
T01LayerAnchorPoint* layer = T01LayerAnchorPoint::create( );
//通常情况下,Scene 是忽略锚点的
//layer->ignoreAnchorPointForPosition( false ); //不忽略锚点
//CCLog( "x = %f, y = %f", layer->getPositionX( ), layer->getPositionY( ) );
scene->addChild( layer );
return scene;
}
T01LayerAnchorPoint* T01LayerAnchorPoint::create( )
{
T01LayerAnchorPoint* pRet = new T01LayerAnchorPoint;
if (pRet && pRet->init())
{
pRet->autorelease( );
}
else
{
delete pRet;
pRet = NULL;
}
return pRet;
}
bool T01LayerAnchorPoint::init( )
{
CCLayer::init( ); //父类初始化
//主战场
CCSize winSize = CCDirector::sharedDirector( )->getWinSize( );
spr = CCSprite::create( "anchor3.png" );
//锚点影响 放置位置
//spr->setAnchorPoint( ccp( 0, 0 ) );
spr->setAnchorPoint( ccp( 0, 1 ) );
//spr->setAnchorPoint( ccp( 1, 0 ) );
//spr->setAnchorPoint( ccp( 1, 1 ) );
//锚点影响 缩放 --- 缩放是围绕锚点旋转的
//spr->setAnchorPoint( ccp( 0.5, 0.5 ) );
//spr->setAnchorPoint( ccp( 0.656, 0.5 ) );
//spr->setScale( 5.0f ); //缩放
//忽略锚点 --- 将锚点置成 0,0
spr->ignoreAnchorPointForPosition( false );
spr->setPosition( ccp( winSize.width / 2, winSize.height / 2 ) );
this->addChild( spr );
//锚点影响 旋转 --- 通常情况下,旋转是围绕锚点进行
//定时器
//typedef void (CCObject::*SEL_SCHEDULE)(float);
//schedule( schedule_selector(T01LayerAnchorPoint::mySchedule ), 2 ); //2秒钟触发
return true;
}
void T01LayerAnchorPoint::draw( )
{
//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) );
}
void T01LayerAnchorPoint::mySchedule( float dt )
{
static float ro = 0;
ro += 30;
spr->setRotation( ro );
}
标签:
原文地址:http://www.cnblogs.com/nfking/p/5586141.html