Cocos2D-X2.2.3学习笔记13(延时动作)

版权声明:本文为博主原创文章。未经博主同意不得转载。

https://blog.csdn.net/q269399361/article/details/28265477

还记得我们上一节讲的瞬时动作吗?有翻转、显示隐藏、位置移动、移除,只是那些都不好玩,今天我们来介绍最经常使用的几种延时动作


什么是延时动作呢?

就是在指定时间内完毕该动作。这样的是能看到效果的


ok,我们略微归类一下。这部分内容不是一般的多

移动:(MoveTo、MoveBy)

旋转(RotateTo、RotateBy)

缩放(ScaleTo、ScaleBy)

倾斜(SkewTo、SkewBy)

跳跃(JumpTo、JumpBy)

闪烁(Blink)

渐隐(FadeIn/FadeOut)

渐变(TintTo、TintBy)

先就这些吧,后面还有非常多非常多。。

。。

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::CCLayer
{
public:
    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();  

    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::CCScene* scene();

    // implement the "static node()" method manually
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

#include "HelloWorldScene.h"

USING_NS_CC;

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	CCSprite* pSprite1= CCSprite::create("Icon.png");
	pSprite1->setPosition(ccp(pSprite1->getContentSize().width/2,visibleSize.height/2));
	this->addChild(pSprite1);
    return true;
}


准备好素材

ok,接下来我们先来看MoveTo


 CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	CCSprite* pSprite1= CCSprite::create("Icon.png");
	CCSize pSpriteSize= pSprite1->getContentSize();
	pSprite1->setPosition(ccp(pSpriteSize.width/2,visibleSize.height/2));
	this->addChild(pSprite1);
	CCActionInterval* pMoveTo=CCMoveTo::create(2.0f,ccp(visibleSize.width-pSpriteSize.width/2,visibleSize.height/2));
	pSprite1->runAction(pMoveTo);

效果图就不贴了,

我们首先创建了精灵,初始位置在最左边,

然后呢。我们穿件了一个CCMoveTo对象

2秒钟之内移动到最右边

最后,精灵运行该动作


使用方法非常easy,大家有没有发现为什么都有个To和By呢??他们之间的差别是什么


OK,我们在来看看MoveBy

CCActionInterval* pMoveBy=CCMoveBy::create(2.0f,ccp(visibleSize.width-pSpriteSize.width/2,visibleSize.height/2));
	pSprite1->runAction(pMoveBy);

我们发现它将移动到了,窗体的右上角


这是为什么呢???

CCMoveTo:   它是以窗体的左下角为原点。还记得坐标系统吗??它移动到(窗体的宽度-精灵大小宽度的一半。窗体高度的一半)。

CCMoveBy:它就不一样的,它以精灵所在坐标的中心点为原点进行移动的,等于就是(窗体的宽度-精灵大小宽度的一半+精灵所在的X坐标,窗体高度的一半+精灵所在的Y坐标)


总之。To和By的差别就在于,

To是实际的坐标,它是正对于窗体的

By是相对于精灵所在的坐标。(事实上,我个人理解就是在正对窗体的坐标中它自己在默认加上精灵所在的坐标而已)

(比方。CCMoveTo 移动到(250,250),那就是窗体所在的250,250的位置)

(CCMoveBy就是(250+精灵的X位置。250+精灵的Y位置)而已)





旋转:RotateTo、RotateBy

//參数1:延时时间
	//參数2:X轴旋转的角度
	//參数3:Y轴旋转的角度
	CCActionInterval* pCCRotateTo= CCRotateTo::create(2.0f,10.0f,50.0f);
	pSprite1->runAction(pCCRotateTo);
By和To的差别就不在说了  刚才已经解释过了

无非就是在当前基础上在加上当前精灵的X或Y轴的角度



缩放(ScaleTo、ScaleBy)

//參数1:延时时间
	//參数2:X轴缩放的倍数	(不缩放为1,0.5为缩小一倍,2为放大一倍)
	//參数3:Y轴缩放的倍数
	CCActionInterval* pCCScaleTo=CCScaleTo::create(2.0f,0.5f,1);

倾斜(SkewTo、SkewBy)

//參数1:延时时间
	//參数2:X轴倾斜的角度	(0为不倾斜。倾斜的角度越大看到的效果越明显)
	//參数3:Y轴倾斜的角度
	CCActionInterval* pCCSkewTo= CCSkewTo::create(2.0f,20.0f,0);

跳跃(JumpTo、JumpBy)

//參数1:延时时间
	//參数2:须要跳跃到什么位置(即跳跃的终点)
	//參数3:跳跃的高度
	//參数4:从開始到跳跃结束的过程中须要跳跃几次
	CCActionInterval* pCCJumpTo= CCJumpTo::create(2.0f,ccp(visibleSize.width-pSpriteSize.width/2,visibleSize.height/2),100,3);


闪烁(Blink)

//參数1:延时时间
	//參数2:须要闪烁的次数
	 CCActionInterval* pCCBlink= CCBlink::create(2.0f,5);

渐隐(FadeIn/FadeOut)

//參数:延时时间(多少秒后全然隐藏)
	CCActionInterval* pCCFadeOut=  CCFadeOut::create(2);

渐变(TintTo、TintBy)

//參数1:延时时间
	//參数2,3,4:分别相应颜色的RGB值
	CCActionInterval* pCCTintTo=CCTintTo::create(2.0f,255,0,0);



OK,搞定,有意思吧??

后面还有非常多呢。比方,组合动作,持续性动作。顺序动作。回调动作等等.....

 
总结一下:
今天学习了
移动(CCMoveTo)
翻转(CCRotateTo)
倾斜(CCSkewTo)
缩放(CCScaleTo)
跳跃(CCJumpTo)
闪烁(CCBlink)
渐隐(CCFadeIn)
渐变(CCTintTo)
还有最重要的一点,To和By的差别

附上所写的代码:
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	CCSprite* pSprite1= CCSprite::create("Icon.png");
	CCSize pSpriteSize= pSprite1->getContentSize();
	pSprite1->setPosition(ccp(pSpriteSize.width/2,visibleSize.height/2));
	this->addChild(pSprite1);
	//CCActionInterval* pMoveTo=CCMoveTo::create(2.0f,ccp(visibleSize.width-pSpriteSize.width/2,visibleSize.height/2));
	//CCActionInterval* pMoveBy=CCMoveBy::create(2.0f,ccp(visibleSize.width-pSpriteSize.width/2,visibleSize.height/2));

	////參数1:延时时间
	////參数2:X轴旋转的角度
	////參数3:Y轴旋转的角度
	//CCActionInterval* pCCRotateTo= CCRotateBy::create(2.0f,10.0f,50.0f);

	//參数1:延时时间
	//參数2:X轴缩放的倍数	(不缩放为1,0.5为缩小一倍,2为放大一倍)
	//參数3:Y轴缩放的倍数
	//CCActionInterval* pCCScaleTo=CCScaleTo::create(2.0f,0.5f,1);

	//參数1:延时时间
	//參数2:X轴倾斜的角度	(0为不倾斜。倾斜的角度越大看到的效果越明显)
	//參数3:Y轴倾斜的角度
	//CCActionInterval* pCCSkewTo= CCSkewTo::create(2.0f,20.0f,0);


	//參数1:延时时间
	//參数2:须要跳跃到什么位置(即跳跃的终点)
	//參数3:跳跃的高度
	//參数4:从開始到跳跃结束的过程中须要跳跃几次
	//CCActionInterval* pCCJumpTo= CCJumpTo::create(2.0f,ccp(visibleSize.width-pSpriteSize.width/2,visibleSize.height/2),100,3);

	//參数1:延时时间
	//參数2:须要闪烁的次数
	//CCActionInterval* pCCBlink= CCBlink::create(2.0f,5);

	//參数:延时时间(多少秒后全然隐藏)
	//CCActionInterval* pCCFadeOut=  CCFadeOut::create(2);

    //參数1:延时时间
	//參数2,3,4:分别相应颜色的RGB值
	CCActionInterval* pCCTintTo=CCTintTo::create(2.0f,255,0,0);
	pSprite1->runAction(pCCTintTo);
    return true;
}

posted on 2019-04-18 19:19  xfgnongmin  阅读(221)  评论(0编辑  收藏  举报

导航