文章目录
1、ActionsTest:(精灵效果)

MoveTo、MoveBy移动

ScaleTo、ScaleBy 拉伸

Rotate                   旋转

Skew                      偏转

RotationalSkew      旋转偏转

setScaleX(-1.0f);     左右翻转

Jump                跳动

CardinalSpline         根据轨迹移动

Blink                闪 

FadeIn             淡入

TintTo            变色





2、TrasitionsTest

场景切换特效

CCScene pScene  = XXXXX();

3、ActionsProgressTest  进度显示

4、EffectsTest    3D特效

5、ClickAndMove     点击移动

6、ParticleMeteor        点击特效

CCLayer pLayer = XXX();

7、TouchesTest         物体碰撞

8、Parallax           可拖动地图

9、LabelTest  (有很多特效的字符串)

10、scheduler    (可制作生效大小控制的进度条)

setTimeScale改变动作快慢




11、FontTest (可以去看看有什么字体好看的)




12、TextureCache    (精确进度60%。。。)

个人总结,如有遗漏,请自己参照官方testcpp例子

下面较详细地介绍一下:






常用工具:

TiledMap (地图编辑器)ParticleEditor(粒子编辑器)cocosBuilder(可视化编辑-只有苹果用),cocosStudio(可视化编辑-只有windows)Texture Packer(图片组合工具)等




常用方法:

CCMoveTo(持续时间, 目标  ); 

CCMoveBy    持续时间内一个向量运动






// Create the actions,动作

CCAction actionMove = CCMoveTo::create( (float)actualDuration, ccp(0 - target->getContentSize().width/2, actualY) );     //动作移动,(持续时间, 目标  )

CCAction actionMoveDone = CCCallFuncN::create( this,   callfuncN_selector(HelloWorld::spriteMoveFinished));

target->runAction( CCSequence::create(actionMove, actionMoveDone, NULL) ); 第一个动作  ,第二个动作


CCDelayTime::create(3),  等三秒




生成字符串:


CCLabelTTF *label = CCLabelTTF::create("callback 1 called", "Marker Felt", 16);

label->setPosition(ccp(100,200));

addChild(label);





输入框:

CCEditBox




动力感应:

setAccelerometerEnabled(true);  

多点:

setTouchEnabled(true);

虚拟键:

setKeypadEnabled(true);







背景音乐:

CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("background-music-aac.wav", true);


播放一次声效:

CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pew-pew-lei.wav");

图片:


CCSprite pSprite = CCSprite::create(“HelloWorld.png”);

pSprite->setFlipX(true); //可以手动设置图形旋转和镜像

pSprite->setRotation(90);

pSprite->setPosition(ccp(size.width/2, size.height/2));

this->addChild(pSprite, 0)








随机数

int x =  rand() % x;




取得屏幕大小

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


(横屏)宽winSize.width

搞winSize.height




关闭按钮,可以直接引用


CCMenuItemImage pCloseItem = CCMenuItemImage::create(

"CloseNormal.png",

"CloseSelected.png",

this,

menu_selector(HelloWorld::menuCloseCallback));

//CC_BREAK_IF(! pCloseItem);

        

// Place the menu item bottom-right conner.   考虑周到点,关闭按钮的位置

        CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();

        CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

        

pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2,

                                    origin.y + pCloseItem->getContentSize().height/2));




// Create a menu with the "close" menu item, it’s an auto release object.

CCMenu pMenu = CCMenu::create(pCloseItem, NULL);

pMenu->setPosition(CCPointZero);

//CC_BREAK_IF(! pMenu);




// Add the menu to HelloWorld layer as a child layer.

this->addChild(pMenu, 1);


//


void HelloWorld::menuCloseCallback(CCObject pSender)

{

  CCDirector::sharedDirector()->end();

}








切换场景:



CCScene4个基本方法:

runWithScene(运行画面),

replaceScene(替换场景 例如从主菜单进入游戏界面),

pushScene(暂停界面),

popScene(从暂停的界面还原到游戏界面)





 pDirector->runWithScene(pScene);  //启动场景

CCDirector::sharedDirector()->replaceScene( HelloWorld::scene() );




定位问题:

VisibleRect类已封装了几个定位的静态方法




动作:


void XXXX::keyBackClicked(){}        //返回键




void XXXX::keyMenuClicked(){}    //菜单键








暂停  恢复

全局


 if(CCDirector::sharedDirector()->isPaused())

        CCDirector::sharedDirector()->resume(); 

    else

        CCDirector::sharedDirector()->pause(); 



暂时

this->schedule(schedule_selector(PauseResumeActions::pause), 3, false, 0);

this->schedule(scheduleselector(PauseResumeActions::resume), 5, false, 0);









帧动作:

不同图片


 CCAnimation* animation = CCAnimation::create();

        for( int i=0;i<4;i++)

           {

               char szName[100] = {0};

               sprintf(szName, "A2%d.png", i);//转成4张图片的名字了

               animation->addSpriteFrameWithFileName(szName);

           }

           //2秒/14张图

           animation->setDelayPerUnit(2.0f / 4.0f);

           animation->setRestoreOriginalFrame(true);   //回复开始时当结束

           CCAnimate action = CCAnimate::create(animation);

           _mySprite->runAction(CCRepeatForever::create(CCSequence::create(action, action->reverse(), NULL)));






使用纹理:


CCTexture2D texture=CCTextureCache::sharedTextureCache()->addImage(“pic2476.png”);


for (int i = 0; i < 3; i ++)

{

for (int j = 0; j < 4; j ++)

{

CCSpriteFrame frame = CCSpriteFrame::createWithTexture(texture,

CCRectMake(widthj, heighti, width, height));

if(i==0 && j==0)

{

this->setDisplayFrame(frame);

}

moves->addObject(frame);


}



move = new CCAnimation();

move->initWithSpriteFrames(moves, delay);


moveAnimate->CCAnimate::create( move );

CCSprite sprite = CCSprite::spriteWithSpriteFrame(frame0);//设置一个初始frame

sprite->setPosition( ccp( s.width/2, s.height/2) );

addChild(sprite);

sprite->runAction(CCRepeatForever::actionWithAction( moveAnimate ));//重复播放














重复动作:

CCRepeatForever::create(  CCSequence::create(a1, a1->reverse(), NULL)




注册多点触碰事件(貌似可以不需要)


void HelloWorld::registerWithTouchDispatcher()

{

// CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this,0,true);

    CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0);

}











点击事件:


void MainLayer::ccTouchesEnded(CCSet pTouches, CCEvent pEvent)

{

    CCSetIterator it = pTouches->begin();

    CCTouch touch = (CCTouch)(it);

    CCPoint location = touch->getLocation();


    CCNode s = getChildByTag(kTagSprite);

     s->runAction( CCMoveTo::create(1, ccp(location.x, location.y) ) );

}




检测碰撞:

bool HelloWorld::collisionWithCircle(CCPoint circlePoint, float radius,CCPoint circlePointTwo, float radiusTwo) {




屏幕点击事件:(取得点击坐标)


void HelloWorld::ccTouchesBegan(CCSet pTouches, CCEvent pEvent) {

CCSetIterator iter = pTouches->begin();

for (; iter != pTouches->end(); iter++) {

CCTouch pTouch = (CCTouch) (iter);

CCPoint location = pTouch->getLocation();











读取plist


CCArray waveData = CCArray::createWithContentsOfFile("Waves.plist");

if (wave >= waveData->count()) {

return false;

}




CCArray currentWaveData = (CCArray) waveData->objectAtIndex(wave);

CCObject pObject = NULL;

CCARRAY_FOREACH(currentWaveData, pObject) {

CCDictionary enemyData = (CCDictionary) pObject;

Enemy enemy = Enemy::nodeWithTheGame(this);

_enemies->addObject(enemy);

enemy->schedule(schedule_selector(Enemy::doActivate),

((CCString) enemyData->objectForKey("spawnTime"))->floatValue());

}






///纹理图添加

CCSprite castleModel = CCSprite::create();

castleModel->setPosition(ccp(100, VisibleRect::center().y));




CCTexture2D cctexture = CCTextureCache::sharedTextureCache()->addImage(

path_soldier_ainilu);

CCArray castle1 = CCArray::createWithCapacity(8);

//castle1->retain();

for (int i = 0; i < 2; i++) {

for (int j = 0; j < 4; j++) {

CCSpriteFrame frame1 = CCSpriteFrame::createWithTexture(cctexture,

CCRectMake(200j, 446i, 200, 446));

if (i == 0 && j == 0) {

castleModel->setDisplayFrame(frame1);

}

castle1->addObject(frame1);

}

}

CCAnimation castleActions =  CCAnimation::createWithSpriteFrames(castle1,0.2f);

//castleActions->retain();

CCAnimate castleAction = CCAnimate::create(castleActions);

castleModel->runAction(CCRepeatForever::create(castleAction));




this->addChild(castleModel);

//armyList->addObject(castleModel);/





技能冷却





 // 添加阴影模版

CCSprite mStencil = CCSprite::create(skill_black);

mStencil->setPosition(am->getAnchorPoint());

   // mStencil->setVisible(false);

addChild(mStencil);







 // 添加sprite

CCSprite mStencil1 = CCSprite::create(skill_treatment_normal);

CCProgressTimer disable = CCProgressTimer::create(mStencil1);

disable->setType(kCCProgressTimerTypeRadial);

disable->setPosition(am->getAnchorPoint());

//mStencil->setVisible(false);

addChild(disable,100);




  CCActionInterval action_progress_to = CCProgressTo::create(5.0f, 100);

  disable->runAction(CCSequence::create(action_progress_to, NULL));








精灵换图:

CCTexture2D pTexture = CCTextureCache::sharedTextureCache()->addImage(youngPic);

sprite->setTexture(pTexture);






文章目录