Cocos2dx学习笔记
学习cocos2dx将近一个月了,这个强大的引擎令到我差点就放弃了,不过在再次的坚持下,还是选择继续前行。
重新梳理了一下学习的过程,遇到的困难和可以应用的东西。
在开始莫过于创建新项目的头痛了,在没有游戏引擎经验的情况下,我独自走上了这条不归路。
创建新项目:开始在菜单文件中新建项目,让我吃了不少亏。一大堆的错误报告,无法包含各种文件。新手没有按步骤的胡乱创建,耗费了好多天时间。
终于在后来找到了新建项目的方法,右击coco2dx项目新建
创建了不用经常报错的项目后,终于可以专心的学习cocos2dx强大的2d图片处理功能了。
接着,我寻找了HelloCpp中,关于HelloWorldScene.cpp中每句代码的含义,并在每句后面添加自己理解的注解。这是非常基本的,打好最简单的基础,才能看懂以后更复杂的代码。
开始显示第一张图片
CCSprite * righetdoor = CCSprite::create("begin//right.png");
CC_BREAK_IF(!righetdoor);
righetdoor->setAnchorPoint(ccp(0,0));//设置锚点
righetdoor->setPosition(ccp(470,0));//设置位置
this->addChild(righetdoor,1);//将图片显示出来,左为(类纹理),右为显示的层
接下来就是创建动作
CCSprite * logo = CCSprite::create("begin//logo.png");// 显示logo
CC_BREAK_IF(!logo);
logo->setAnchorPoint(ccp(0.5,0.5));
logo->setPosition(ccp(470,325));
CCMoveTo* movelogo = CCMoveTo::create(1.0f,ccp(940,325));//绝对坐标括移?动
//CCMoveBy为相对坐标移动
CCRotateTo* rot = CCRotateTo::create(1.0f,180);//旋转
CCRotateTo* rot1 = CCRotateTo::create(1.0f,0);//旋转
CCMoveTo* movelogo2 = CCMoveTo::create(1.0f,ccp(470,325));//绝对坐标括移动
CCHide* hide1 =CCHide::create();//隐藏
CCCallFunc*cf=CCCallFunc::create(this,callfunc_selector(HelloWorld::admation));/调用一个函数
CCFiniteTimeAction*seq1=CCSequence::create(movelogo,rot,rot1,movelogo2,hide1,cf,NULL);//将一个连串动作连起来
//CCRepeat* repeat = CCRepeat::create(seq1,3);//重复sep1三次
logo->runAction(seq1);
this->addChild(logo,2);
//CCMoveTo* movelogo = CCMoveTo::create(1.0f,ccp(940,325));//绝对坐标移动
//logo->runAction(movelogo);
//CCRotateTo* rot = CCRotateTo::create(1.0f,180);
//CCFiniteTimeAction* seq = CCSequence::create(movelogo,rot,NULL);//在2.04版本与2.1版本在ccsequence的引用有所不同 串联动作
//CCFiniteTimeAction* spa = CCSpawn::create(movelogo,rot,NULL);在2.04版本与2.1版本在CCSpawn的引用有所不同 并联动作
//CCRepeat* repeat = CCRepeat::create(spa,3);重复spa动作3次
//logo->runAction(repeat);
场景方面
创建场景
Menuscence.cpp 文件
#include "Menuscence.h"
USING_NS_CC;
CCScene* Menuscence::scene()
{
CCScene* scene=CCScene::create();
Menuscence* layer=Menuscence::create();
scene->addChild(layer);
return scene;
}
bool Menuscence::init()
{
}
Menuscence::Menuscence(void)
{
}
Menuscence::~Menuscence(void)
{
}
"Menuscence.h"
#pragma once
#include "cocos2d.h"
#include <cstring>
class Menuscence :
public cocos2d::CCLayer
{
private:
cocos2d::CCLabelTTF* output;
public:
static cocos2d::CCScene* scene();
virtual bool init();
CREATE_FUNC(Menuscence);
Menuscence(void);
~Menuscence(void);
};
替换场景
CCScene* scene = Menuscence::scene();//创建场景类(需要定义一个Menuscence在先)
CCTransitionSlideInL* transide = CCTransitionSlideInL::create(1.0f,scene);//左移堆出场景
CCDirector::sharedDirector()->replaceScene(transide);//替换当前场景
菜单控件
添加颜色控件,默认锚点为左下角。单颜色
CCLayerColor* color = CCLayerColor::create(ccc4(255,0,0,125),200,100);//创建颜色控件,ccc4最后一个才是透明度
color->setPosition(ccp(300,300));//值得注意的是,设置color的位置
this->addChild(color,2);
//两种颜色控件支持颜色渐变
CCLayerGradient*grad=CCLayerGradient::create(ccc4(255,0,0,255),ccc4(255,255,0,255),ccp(1,1));
//可以设定颜色, 还可以设定方向
grad->changeWidthAndHeight(100,100);//指定宽度和高度
grad->setPosition(ccp(100,300));//指定位置
this->addChild(grad,2);
//文字显示控件
CCLabelTTF* tupe = CCLabelTTF::create("hello","Arial",50);
tupe->setColor(ccc3(255,0,0));
tupe->setPosition(ccp(300,300));
this->addChild(tupe,2);
//创建文字菜单控件
CCLabelTTF* laber1 = CCLabelTTF::create("Srar","Arial",50);
laber1->setColor(ccc3(255,0,0));
CCLabelTTF* laber2 = CCLabelTTF::create("Next","Arial",50);
laber2->setColor(ccc3(255,0,0));
CCMenuItemLabel*menulabel=CCMenuItemLabel::create(laber1,this,menu_selector(Menuscence::start));//文本菜单项1
//在菜单中添加需要回调的函数名称
menulabel->setPosition(ccp(470,325));
CCMenuItemLabel*menulabe2=CCMenuItemLabel::create(laber2,this,menu_selector(Menuscence::Next));//文本菜单项2
//在菜单中添加需要回调的函数名称
menulabe2->setPosition(ccp(470,275));
CCMenuItemImage*item1=CCMenuItemImage::create("layer/unhit.png","layer/hit.png",this,menu_selector(Menuscence::start));//添加图片按钮
item1->setPosition(ccp(100,200));
CCMenu* menu1 = CCMenu::create(menulabel,menulabe2,item1,NULL);//添加菜单项
menu1->setPosition(ccp(0,0));
this->addChild(menu1,2);
output=CCLabelTTF::create("abc","Arial",50);
output->setPosition(ccp(100,100));
addChild(output);
显示动画
包含在其应用的函数中
this->Moveright();
this->Movehit();
CCAnimation*animation1=CCAnimationCache::sharedAnimationCache()->animationByName("workright");
CCSprite* ivll = CCSprite::create();
ivll->setPosition(ccp(200,300));
this->addChild(ivll);
ivll->runAction(CCRepeatForever::create(CCAnimate::create(animation1)));//动作1
void Menuscence::Moveright()
{
//1.这是包含多张图片的动画方法
char TFileNameIn[64];//需要定义全局变量
strcpy_s(TFileNameIn,("sprite/work/work01.png"));//需要包含#include <cstring>
for (int i=0;i<CMD01_Nm;i++)
{
TFileNameIn[16] = 0x30 + (i/10);
TFileNameIn[17] = 0x31 + (i-((i/10)*10));
frame[i] = CCSpriteFrame::create(TFileNameIn,CCRectMake(0,0,74,62));
}
//2.单张图片加载动画方式
/* CCSpriteFrame * frame[CMD01_Nm] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
for (int i=0;i<CMD01_Nm;i++)
{
frame[i]=CCSpriteFrame::create("sprite/work/work01.png",CCRectMake(74*i,62*0,74,62));
} */
CCArray*frames=CCArray::create(frame[0],frame[1],frame[2],frame[3],frame[4],frame[5],frame[6],NULL);
CCAnimation* animation1 = CCAnimation::createWithSpriteFrames(frames,0.1f);//帧率参数,数据类型为浮点!!!不能用整数
CCAnimationCache::sharedAnimationCache()->addAnimation(animation1,"workright");
显示单个动画
//CCSpriteFrame*frame1=CCSpriteFrame::create("sprite/work/work01.png",CCRectMake(0,0,74,62));
//CCArray*frames=CCArray::create(frame1,frame2,frame3,frame4,frame5,frame6,frame7,frame8,frame9, //显示单帧
// frame10,NULL);
//CCAnimation* animation1 = CCAnimation::createWithSpriteFrames(frames,0.1f);//帧率参数,数据类型为浮点!!!不能用整数
//CCAnimate* animate = CCAnimate::create(animation1);
//CCSprite* work = CCSprite::create();
//work->setAnchorPoint(ccp(0.5,0.5));
//work->setPosition(ccp(200,300));
//this->addChild(work,2);
//work->runAction(CCRepeatForever::create(animate));