zoukankan      html  css  js  c++  java
  • cocos2d-x -------之笔记篇 动画的实现

    cocos2d-x 动画的实现

    一、实现原理

    动画的实现其实就是使用一个完整的动作图片集来实现动画,达到动态的效果

      动画动作类(CCAnimate)是加载一个动画类来实现动作。

      动画类(CCAnimation)加载一个精灵帧数组来构成一个动画,

    CCAnimate函数:

      static CCAnimate* create(CCanimation* pAnimation)

    CCAnimation创建函数

    CCAnimation* CCAnimation::createWithSpriteFrames(CCArray *frames, float delay/* = 0.0f*/)

    参数:数组,间隔时间

    二、动作实现步骤

    1、首先需要加载一个纹理图片

    CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage(“纹理图片");

     

    然后能够得到该纹理的宽高信息,

    2、创建一个精灵,一般都是使用该纹理图片中的某个图片来作为显示精灵,这样在执行动画动作的时候能够更加的协调

    首先我们需要加载

    使用上得到的纹理创建一个精灵

    CCSpriteFrame* spriteFrame1 = CCSpriteFrame::createWithTextrue(text,CCRectmake(x,y,宽,高));

    CCSprite* sprite = CCSprite::createWithSpriteFrame(spriteFrame1);

     这样能够将纹理的某个小区域用来创建一个精灵

    3、创建一个精灵帧数组

    CCArray array = CCArray::create();

    然后将要执行动作的图片集存放在该数组中

    4、使用该数组创建一个动画类

    CCAnimation* animation =  CCAnimation::createWithSpriteFrames(array,0.1f);

    5、创建动画动作

    CCAnimate* animate = CCAnimate::create(animation);

    最后精灵执行该动作

    下面是自己测试的代码:

     

    bool MyActiondonghua::init(){
    	if(!CCLayer::init())
    		return false;
    
    	CCSize size = CCDirector::sharedDirector()->getWinSize();
    	//1
    	CCTexture2D * texture = CCTextureCache::sharedTextureCache()->addImage("dongzuo.png");
    	float texturewidht = texture->getContentSize().width;
    	float textureheight = texture->getContentSize().height;
    
    	float pwidth = texturewidht /10;
    	float pheight = textureheight / 4;
    	//2
    	//得到纹理图片的第一个图片
    	CCSpriteFrame* spriteFrame = CCSpriteFrame::createWithTexture(texture,CCRectMake(0,0,pwidth,pheight));
    	//使用这个spriteFrame来创建一个精灵进行显示
    	CCSprite* sprite = CCSprite::createWithSpriteFrame(spriteFrame);
    	sprite->setPosition(ccp(size.width/2,size.height/2) );
    	sprite->retain();
    	this->addChild(sprite);
    	//3
    	//创建动画
    	CCArray* array = CCArray::create();
    	for(int i = 0; i <9 ; i++){
    		CCSpriteFrame* tmpspritefreme = CCSpriteFrame::createWithTexture(texture,CCRectMake(pwidth*i,pheight,pwidth,pheight));
    		array->addObject(tmpspritefreme);
    	}
    	//4
    	CCAnimation *animation = CCAnimation::createWithSpriteFrames(array,0.1f);
    	//5
    	CCAnimate* animate = CCAnimate::create(animation);
    	//6
    	sprite->runAction(CCRepeatForever::create(animate));
    	
    	return true;
    }
    

     

      

     

  • 相关阅读:
    java学习笔记——基于Robot类的屏幕分享
    Java实例——基于jsoup的简单爬虫实现(从智联获取工作信息)
    Java实例练习——基于UDP协议的多客户端通信
    java实例练习——基于TCP/IP协议的多客户端通信
    我个人的Java学习经验(一家之言)
    PHP mac localhost 环境下发送邮件
    php ob_start()、ob_end_flush和ob_end_clean()多级缓冲
    php ob_start()、ob_end_flush和ob_end_clean()多级缓冲
    程序员应该知道的13个设计技巧
    程序员应该知道的13个设计技巧
  • 原文地址:https://www.cnblogs.com/yyroom/p/3856529.html
Copyright © 2011-2022 走看看