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

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


什么是延时动作呢?

就是在指定时间内完成该动作,这种是能看到效果的


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秒钟之内移动到最右边

最后,精灵执行该动作


用法很简单,大家有没有发现为什么都有个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);
B y和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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值