cocos2dx人物行走例子

大笑这是本人的课堂笔记,刚刚开始学习cocos2dx,所以请各位大神不要在意笔记质量。。。

=====================================.h================================

#include "cocos2d.h"
USING_NS_CC;
class xingzou2:public CCLayer
{
public:
 
    CCSpriteBatchNode *node;

        
    virtual bool init();
    static CCScene *scene();
    void zhen();
    CREATE_FUNC(xingzou2);
};

=====================================.h================================

#include "xingzou2.h"


bool xingzou2::init()
{
    if(CCLayer::init())
    {
         //叫来ccspriteframecache内存池
        CCSpriteFrameCache *cache =CCSpriteFrameCache::sharedSpriteFrameCache();     
        //将plist文件添加进入内存池
        cache->addSpriteFramesWithFile("yaoling1.plist");

        //
        this->node = CCSpriteBatchNode::create("yaoling1.png");
        //将node层放入layer层中
        this->addChild(node);
       
        this->zhen();       
        return true;
    }
    return false;
}


void xingzou2::zhen()
{
    //叫来ccspriteframecache内存池
    CCSpriteFrameCache *cache= CCSpriteFrameCache::sharedSpriteFrameCache();
    //new一个数组
    CCArray *arr= CCArray::create();
    //循环
    for (int i=0; i<6; i++)
    {
        //将名字和图片互相匹配
        sprintf(name, "000%d.png",i);
        //通过名称从内存池中取出数据
        CCSpriteFrame *frame = cache->spriteFrameByName(name);
        //将数据存入数组中
        arr->addObject(frame);
       
    }
    //创建动画序列帧
    CCAnimation *action=CCAnimation::createWithSpriteFrames(arr);
    //设置序列帧的播放间隙
    action->setDelayPerUnit(0.3);
    //设置序列帧的循环次数
    action->setLoops(-1);
   
    //使用序列帧来创建动画播放
    CCAnimate *animate=CCAnimate::create(action);
    //使用图片名称来new一个精灵
    CCSprite *sprite = CCSprite::createWithSpriteFrameName(name);
    //运行动画播放动作
    sprite ->runAction(animate);
    //设置比例缩放
    sprite ->setScale(3);
    //将精灵放入node层中
    this->node->addChild(sprite);
    //设置精灵在屏幕中的坐标位置
    sprite->setPosition(ccp(200, 300));

}


CCScene* xingzou2::scene()
{
   
    CCScene *sc=CCScene::create();
    xingzou2 *xing=xingzou2::create();
    sc->addChild(xing);
    return sc;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值