??
1
编写MyLayer
头文件:MyLayer.h |
#include "cocos2d.h" USING_NS_CC; //继承CCLayer class { public: //没有构造函数和析构函数 CREATE_FUNC(MyLayer); /*static MyLayer *create();*/ static bool }; |
MyLayer.cpp |
#include "MyLayer.h" //MyLayer *MyLayer::create(){ // // // // // // // // // // // //} bool { if (!CCLayer::init()) { return } CCSize //创建一个精灵 CCSprite * spr->setPosition(ccp(winSize.width/2,winSize.height //加入一个精灵 addChild(spr); return } CCScene *MyLayer::scene() { CCScene * MyLayer * scene->addChild(layer); return } |
要想让写的Layer可以执行,须要在AppDelegate.app中加入例如以下代码 |
1、 加入头文件:#include "MyLayer.h" 2、 bool // initialize director CCDirector* CCEGLView* pDirector->setOpenGLView(pEGLView); // turn on display FPS pDirector->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don‘t call this pDirector->setAnimationInterval(1.0 // create a scene. it‘s an autorelease object //CCScene *pScene = HelloWorld::scene(); CCScene * // run pDirector->runWithScene(pScene); return } |
执行结果: |
2
锚点
LayerAnchorPoint.h |
#ifndef __T01LayerAnchorPoint_H__ #define #include USING_NS_CC; class { public: static bool static void void CCSprite *spr; }; #endif |
LayerAnchorPoint.cpp |
#include "LayerAnchorPoint.h" CCScene * { //创建一个场景 CCScene * LayerAnchorPoint * scene->addChild(layer); return } LayerAnchorPoint * LayerAnchorPoint *pRet if (pRet { pRet->autorelease(); } else { delete pRet = } return } bool { CCLayer::init(); CCSize spr = //设置锚点 //spr->setAnchorPoint(ccp(0.656,0.5)); spr->setScale(5.0f); spr->setPosition(ccp(winSize.width/2,winSize.height/2)); addChild(spr); //设置一个定时器,频率是2秒 schedule(schedule_selector(LayerAnchorPoint::mySchedule), return } //通过以下的方法实现画线 void { CCSize //RGB颜色。当中最后一个參数是透明率 ccDrawColor4B(255,0,0,255); ccDrawLine(ccp(0, ccDrawLine(ccp(winSize.width } //通过以下的方式设置旋转 void static ro += 30; spr->setRotation(ro); } |
改动:AppDelegate.cpp中的代码 |
头文件: #include bool // initialize director CCDirector* CCEGLView* pDirector->setOpenGLView(pEGLView); // turn on display FPS pDirector->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don‘t call this pDirector->setAnimationInterval(1.0 // create a scene. it‘s an autorelease object //CCScene *pScene = HelloWorld::scene(); //CCScene * pScene = MyLayer::scene(); CCScene * // run pDirector->runWithScene(pScene); return } |
执行结果: |
3
精灵的创建
1、编写:LayerSprite.h |
#include "cocos2d.h" USING_NS_CC; class { public: static CREATE_FUNC(LayerSprite); bool }; |
编写:表示屏幕大小的头文件AppMacros.h |
#ifndef __APPMACROS_H__ #define #include //通过提前定义获得屏幕大小 #define #endif |
编写:LayerSprite.cpp |
#include "LayerSprite.h" #include CCScene *LayerSprite::scene() { CCScene * scene = CCScene::create(); LayerSprite * layer = LayerSprite::create(); scene->addChild(layer); return scene; } bool T02LayerSprite::init() { CCLayer::init(); CCSprite * bg = CCSprite::create("sprite/background.jpg",CCRectMake(0,0,480,320)); bg->setAnchorPoint(ccp(0,0)); bg->setPosition(CCPointZero); addChild(bg); CCImage * image = image->initWithImageFile("sprite/plant.png"); CCTexture2D * texture = texture->autorelease(); texture->initWithImage(image); CCSprite * plant = CCSprite::createWithTexture(texture); plant->setPosition(ccp(100,winSize.height/2)); addChild(plant); CCTexture2D * texture2 = CCSprite * bullet = bullet->setPosition(ccp(200, addChild(bullet); CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("sprite/zombie.plist"); //CCSpriteFrame *sf = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("z_1_attack_01.png"); //CCSprite *zombie = CCSprite::createWithSpriteFrame(sf); CCSprite *zombie = zombie->setPosition(ccp(400, addChild(zombie); CCJumpBy * by = zombie->runAction(CCRepeatForever::create(by)); CCSpriteFrame * CCSprite * sun = sun->setPosition(ccp(400, addChild(sun); return } |
同上,将LayerSprite.h加到AppDelegate.cpp中,并加入上: CCScene * pScene = |
执行结果: |
4 zorder
LayerZorder.h |
#include "cocos2d.h" USING_NS_CC; class { public: static CREATE_FUNC(LayerZorder); bool }; |
LayerZorder.cpp |
#include "LayerZorder.h" #include CCScene *LayerZorder::scene() { CCScene * LayerZorder * scene->addChild(layer); return } bool { CCLayer::init(); CCSprite * //将图片缩放 man->setScale(0.3f); CCSprite * woman->setScale(0.3f); CCSprite * smallThree->setScale(0.3f); man->setPosition(ccp(winSize.width smallThree->setPosition(ccp(winSize.width woman->setPosition(ccp(winSize.width addChild(man); addChild(smallThree); addChild(woman); //通过setZOrder来设置图片显示顺序 man->setZOrder(100); smallThree->setZOrder(50); woman->setZOrder(0); return /*CCLog("%d", man->getZOrder()); CCLog("%d", smallThree->getZOrder()); CCLog("%d", woman->getZOrder());*/ return } |
改动AppDelegate.cpp 1. 2. 执行结果: |