当前位置:编程学习 > wap >>

Cocos2d-x初入学堂(4)-->CCSprite动画

针动画是我们见得最多的,电视、电影等,如果印象深刻的话,小时候的那种老款照相机的胶卷...大小相同的一张一张的底片....哈哈!其实如果对游戏要求不高,游戏的图片也不多,咋们就可以采用这种方式来制作动画,不过好游戏一般都不是这样做的....那是什么呢?...动作编辑器,先讲讲最基础的制作动画方式(其实利用动作编辑器,其实也是切割图片,如果还没有接触过动作编辑器的,可以学着用下SpriteX)...好了,就此开始吧!

1、先用texturePacker制作我们的素材

找一组动画图片,我直接test里面那个大叔的一组图片...

由于用直接用test里面的图片有点问题,所以我直接把组图,用ps切出每帧然后导出,然后用texturePacker打包,导出role.plist和role.png

2、上传代码:...老样子(我没有新建工程,直接用的原来的工程)

看一下我的目录:

 

红色框出来的就是我基于上讲工程新添加的文件:(因为我特别怕乱,所以单独创建和场景和层)

ActionScene.h

[cpp]
#pragma once 
#include "cocos2d.h" 
using namespace cocos2d; 
 
class ActionScene :public CCScene 

public: 
    ActionScene(void); 
    ~ActionScene(void); 
    static CCScene* scene(); 
}; 

ActionScen.cpp

[cpp] 
#include "ActionScene.h" 
#include "ActionLayer.h" 
 
ActionScene::ActionScene(void) 


 
 
ActionScene::~ActionScene(void) 


 
CCScene* ActionScene::scene() 

    CCScene* scene=CCScene::create(); 
    CCLayer* layer=ActionLayer::layer(0); 
    scene->addChild(layer); 
    scene->scheduleUpdate(); 
    return scene; 

ActionLayer.h

[cpp] 
#pragma once 
#include "cocos2d.h" 
using namespace cocos2d; 
enum 

    ACTION_ANIMATION_LAYER=0 
}; 
 
class ActionLayer :public CCLayer 

public: 
    ActionLayer(void); 
    ~ActionLayer(void); 
    static CCLayer* layer(int id); 
protected: 
    CCSprite* grossini; 
}; 

ActionLayer.cpp

[cpp] 
#include "ActionLayer.h" 
#include "ActionAnimationLayer.h" 
ActionLayer::ActionLayer(void) 

 

 
ActionLayer::~ActionLayer(void) 

 

 
CCLayer* ActionLayer::layer(int id) 

    CCLayer* pLayer=NULL; 
    switch(id) 
    { 
    case ACTION_ANIMATION_LAYER: 
        pLayer=new ActionAnimationLayer(); 
        break; 
    } 
    return pLayer; 

ActionAnimationLayer.h

[cpp] 
#pragma once 
#include "actionlayer.h" 
class ActionAnimationLayer :public ActionLayer 

public: 
    ActionAnimationLayer(void); 
    ~ActionAnimationLayer(void); 
    virtual void onEnter(); 
    void frameAnimation(CCSpriteFrameCache *cache); 
    void jumpAnimation(); 
    void fadeAnimation(); 
    void sequenceAnimation(CCSize s); 
    void followAnimation(CCSize s); 
}; 
ActionAnimationLayer.cpp

[cpp]
#include "ActionAnimationLayer.h" 
 
 
ActionAnimationLayer::ActionAnimationLayer(void) 

 

 
 
ActionAnimationLayer::~ActionAnimationLayer(void) 

 

 
 
void ActionAnimationLayer::onEnter() 

    //【注意:】此句话千万不要漏了,漏了是不会有动画效果的,底层包含了动画的刷新, 
    //我就是这个地方啊!搞得我多搞了几个小时,还好这个工程熟悉了一下底层的代码 
    CCLayer::onEnter(); 
 
    CCSize s=CCDirector::sharedDirector()->getWinSize(); 
     
    CCSpriteFrameCache *cache=CCSpriteFrameCache::sharedSpriteFrameCache(); 
    cache->addSpriteFramesWithFile("role.plist","role.png"); 
 
    grossini=CCSprite::spriteWithSpriteFrameName("role2.png"); 
    grossini->setPosition(ccp(s.width/2,s.height/2)); 
 
    this->addChild(grossini); 
     
    //播放帧动画 
    //this->frameAnimation(cache); 
    //播放跳动画 
    //this->jumpAnimation(); 
    //浅入浅出 
    //this->fadeAnimation(); 
    //组动画:move+rotate 
    //this->sequenceAnimation(s); 
    //拉屏幕效果 
    this->followAnimation(s); 

 
void ActionAnimationLayer::frameAnimation(CCSpriteFrameCache *cache) 

    //第一种方式: 
    CCAnimation* animation = CCAnimation::create(); 
    for( int i=1;i<5;i++) 
    { 
        char szName[100] = {0}; 
        sprintf(szName, "role%1d.png", i); 
        //animation->addSpriteFrameWithFileName(szName); 
 
        animation->addSpriteFrame(cache->spriteFrameByName(szName)); 
    } 
    // 每针停留2.8/14f秒 
    animation->setDelayPerUnit(2.8f / 14.0f); 
    //设置恢复初始针 
    animation->setRestoreOriginalFrame(true); 
    //设置循环次数 
    animation->setLoops(4); 
    //创建动画 
    CCAnimate* action = CCAnimate::create(animation); 
    //运行动画 
    grossini->runAction(CCSequence::create(action, action->reverse(), NULL)); 
    //第二种方式: 
    /*CCArray* animFrames=CCArray::create(4);
    char str[100]={0};
    for(int i=1;i<5;i++)
    {
        sprintf(str, "role%1d.png", i);
        CCSpriteFrame* frame=cache2->spriteFrameByName(str);
        animFrames->addObject(frame);
    }

补充:移动开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,