首页 > 代码库 > 编写MyLayer,2 锚点,3 精灵的创建,4 zorder
编写MyLayer,2 锚点,3 精灵的创建,4 zorder
1编写MyLayer
头文件:MyLayer.h |
#include"cocos2d.h" USING_NS_CC; //代表的是: using namespace cocos2d (通过F12进行查看)
//继承CCLayer classMyLayer :publicCCLayer { public: //没有构造函数和析构函数 CREATE_FUNC(MyLayer); /*static MyLayer *create();*/
staticCCScene *scene(); boolinit(); }; |
MyLayer.cpp |
#include"MyLayer.h"
//MyLayer *MyLayer::create(){ // MyLayer * pRet = new MyLayer(); // if (pRet && pRet->init()) // { // pRet->autorelease(); // } // else // { // delete pRet; // pRet = NULL; // } // return pRet; //}
boolMyLayer::init() { if (!CCLayer::init()) { returnfalse; }
CCSizewinSize =CCDirector::sharedDirector()->getWinSize();
//创建一个精灵 CCSprite *spr =CCSprite::create("anchor3.png"); spr->setPosition(ccp(winSize.width/2,winSize.height /2)); //添加一个精灵 addChild(spr);
returntrue; }
CCScene *MyLayer::scene() { CCScene *scene =CCScene::create(); MyLayer *layer =MyLayer::create(); scene->addChild(layer); returnscene; } |
要想让写的Layer能够运行,需要在AppDelegate.app中添加如下代码 |
1、添加头文件:#include"MyLayer.h" 2、在boolAppDelegate::applicationDidFinishLaunching()中修改如下内容:
boolAppDelegate::applicationDidFinishLaunching() { // initialize director CCDirector*pDirector =CCDirector::sharedDirector(); CCEGLView*pEGLView =CCEGLView::sharedOpenGLView();
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 / 60);
// create a scene. it‘s an autorelease object //CCScene *pScene = HelloWorld::scene(); CCScene *pScene =MyLayer::scene();
// run pDirector->runWithScene(pScene);
returntrue; } |
运行结果: |
2锚点
LayerAnchorPoint.h |
#ifndef__T01LayerAnchorPoint_H__ #define__T01LayerAnchorPoint_H__
#include"cocos2d.h" USING_NS_CC;
classLayerAnchorPoint:publicCCLayer { public: staticLayerAnchorPoint *create(); boolinit();
staticCCScene *scene();
voiddraw();
voidmySchedule(floatdt); CCSprite *spr; };
#endif |
LayerAnchorPoint.cpp |
#include"LayerAnchorPoint.h"
CCScene *LayerAnchorPoint::scene() { //创建一个场景 CCScene *scene =CCScene::create(); LayerAnchorPoint *layer =LayerAnchorPoint::create(); scene->addChild(layer); returnscene; }
LayerAnchorPoint *LayerAnchorPoint::create() { LayerAnchorPoint *pRet = newLayerAnchorPoint(); if (pRet && pRet->init()) { pRet->autorelease(); } else { deletepRet; pRet =NULL; } returnpRet; }
boolLayerAnchorPoint::init() { CCLayer::init();
CCSizewinSize =CCDirector::sharedDirector()->getWinSize(); spr =CCSprite::create("anchor1.png"); //设置锚点 //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), 2); returntrue; }
//通过下面的方法实现画线 voidLayerAnchorPoint::draw() { CCSizewinSize =CCDirector::sharedDirector()->getWinSize(); //RGB颜色,其中最后一个参数是透明率 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)); }
//通过下面的方式设置旋转 voidLayerAnchorPoint::mySchedule(floatdt) { staticfloatro = 0; ro += 30; spr->setRotation(ro); } |
修改:AppDelegate.cpp中的代码 |
头文件: #include"LayerAnchorPoint.h"
boolAppDelegate::applicationDidFinishLaunching() { // initialize director CCDirector*pDirector =CCDirector::sharedDirector(); CCEGLView*pEGLView =CCEGLView::sharedOpenGLView();
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 / 60);
// create a scene. it‘s an autorelease object //CCScene *pScene = HelloWorld::scene(); //CCScene * pScene = MyLayer::scene(); CCScene *pScene =LayerAnchorPoint::scene();
// run pDirector->runWithScene(pScene);
returntrue; } |
运行结果: |
3精灵的创建
1、编写:LayerSprite.h |
#include"cocos2d.h"
USING_NS_CC; classLayerSprite:publicCCLayer { public: staticCCScene * scene(); CREATE_FUNC(LayerSprite); boolinit();
}; |
编写:表示屏幕大小的头文件AppMacros.h |
#ifndef__APPMACROS_H__ #define__APPMACROS_H__
#include"cocos2d.h"
//通过预定义获得屏幕大小 #define winSize CCDirector::sharedDirector()->getWinSize()
#endif/* __APPMACROS_H__ */ |
编写:LayerSprite.cpp |
#include"LayerSprite.h" #include"AppMacros.h"
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 =newCCImage(); image->initWithImageFile("sprite/plant.png");
CCTexture2D * texture =newCCTexture2D(); texture->autorelease(); texture->initWithImage(image); CCSprite * plant = CCSprite::createWithTexture(texture);//纹理 plant->setPosition(ccp(100,winSize.height/2)); addChild(plant);
CCTexture2D * texture2 =CCTextureCache::sharedTextureCache()->addImage("sprite/bullet.png"); CCSprite * bullet =CCSprite::createWithTexture(texture2); bullet->setPosition(ccp(200,winSize.height / 2+10)); 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 =CCSprite::createWithSpriteFrameName("z_1_attack_01.png"); zombie->setPosition(ccp(400,winSize.height / 2-50)); addChild(zombie);
CCJumpBy * by =CCJumpBy::create(2,ccp(0,0),40,3); zombie->runAction(CCRepeatForever::create(by));
CCSpriteFrame *frame =CCSpriteFrame::create("sprite/p_1_01.png",CCRectMake(0, 0, 80, 80)); CCSprite * sun =CCSprite::createWithSpriteFrame(frame); sun->setPosition(ccp(400,winSize.height / 2 + 50)); addChild(sun);
returntrue; } |
同上,将LayerSprite.h加到AppDelegate.cpp中,并添加上: CCScene * pScene =LayerSprite::scene(); |
运行结果: |
4 zorder
LayerZorder.h |
#include"cocos2d.h" USING_NS_CC;
classLayerZorder:publicCCLayer { public: staticCCScene *scene(); CREATE_FUNC(LayerZorder); boolinit(); }; |
LayerZorder.cpp |
#include"LayerZorder.h" #include"AppMacros.h"
CCScene *LayerZorder::scene() { CCScene *scene =CCScene::create(); LayerZorder *layer =LayerZorder::create(); scene->addChild(layer); returnscene; }
boolLayerZorder::init() { CCLayer::init();
CCSprite *man =CCSprite::create("zorder/car42.png"); //将图片缩放 man->setScale(0.3f); CCSprite *woman =CCSprite::create("zorder/car43.png"); woman->setScale(0.3f); CCSprite *smallThree =CCSprite::create("zorder/car51.png"); smallThree->setScale(0.3f);
man->setPosition(ccp(winSize.width / 2 + 40, winSize.height / 2 - 30)); smallThree->setPosition(ccp(winSize.width / 2, winSize.height / 2 + 30)); woman->setPosition(ccp(winSize.width / 2 - 40, winSize.height / 2 + 30));
addChild(man); addChild(smallThree); addChild(woman);
//通过setZOrder来设置图片显示顺序 man->setZOrder(100); smallThree->setZOrder(50); woman->setZOrder(0); returntrue;
/*CCLog("%d", man->getZOrder()); CCLog("%d", smallThree->getZOrder()); CCLog("%d", woman->getZOrder());*/ returntrue; } |
修改AppDelegate.cpp 1. 将zorder的头文件进入:#include"LayerZorder.h" 2. CCScene *pScene =LayerZorder::scene(); 运行结果: |
编写MyLayer,2 锚点,3 精灵的创建,4 zorder