cocos 2dx 3.12 学习笔记(七) ActionTutorial

Action

实现精灵的动作,和使用计时器实现基本相同,为cocos2dx自带的方法,使用起来会很方便


ActionTutorial.h


#ifndef __ACTION_TUTORIAL_H__
#define __ACTION_TUTORIAL_H__
#include "cocos2d.h"

class ActionTutorial :public cocos2d::Layer
{
public:
	static cocos2d::Scene* createScene();

	virtual bool init();
	//延时
	void testMoved();//移动
	void testScale();//缩放
	void testRotate();//旋转
	void testSkew();//斜切
	void testJump();//跳
	void testBlink();//闪
	void testFade();//改变透明度
	void testTint();//填色


	//组合
	void testSequence();//顺序执行动作
	void testSpawn();//一起执行动作,什么时候结束看一开始设定的时间
	//缓冲
	void testEase();//缓冲动作
	//祯动画
	void testFrameAnimation();//帧
	//repeat

	CREATE_FUNC(ActionTutorial);
private:
	cocos2d::Sprite * m_pSprite = nullptr;
};
#endif



ActionTutorial.cpp


#include "ActionTutorial.h"
USING_NS_CC;

Scene* ActionTutorial::createScene()
{
	auto scene = Scene::create();

	auto layer = ActionTutorial::create();

	scene->addChild(layer);

	return scene;
}

bool ActionTutorial::init()
{

	if (!Layer::init())
	{
		return false;
	}

	float x = 0;
	float y = 0;
	auto size = this->getContentSize();

	x = size.width / 2;
	y = size.height / 2;
	//m_pSprite->setVisible(false);
	m_pSprite = Sprite::create("HelloWorld.png");
	this->addChild(m_pSprite);
	m_pSprite->setPosition(Vec2(x,y));
	this->testEase();
	return true;
}

void ActionTutorial::testMoved()
{
	//moveTo让节点移动到制定坐标
	//moveBy移动一个相应的坐标
	m_pSprite->runAction(MoveTo::create(1.0f, Vec2(0, 0)));
	//m_pSprite->runAction(Sequence::create(
	//	CallFunc::create([](){
	//	
	//}),
	//	MoveBy::create(1.0f,Vec2(0,0)),
	//	CallFunc::create([](){
	//	
	//}),nullptr
	//	
	//	))

}

void ActionTutorial::testScale()
{
	//to是和原来做对比,by则是在现在基础上改
	//m_pSprite->runAction(ScaleTo::create(1.0f,0.5f));//需要的时间和缩放的倍数。
	m_pSprite->runAction(ScaleBy::create(1.0f, 1.5f));
}
void ActionTutorial::testRotate()
{
	//to在原来基础,by在现有上
	m_pSprite->setRotation(180.0f);
	//m_pSprite->runAction(RotateTo::create(1.0f,180.0f));
	m_pSprite->runAction(RotateBy::create(1.0f, -180.0f));
}
void ActionTutorial::testSkew()
{
	//dt动画时间   s_x,s_y x,y轴的斜切值
	//to  by 类似
	m_pSprite->setSkewX(20.0f);

	m_pSprite->setSkewY(20.0f);
	//m_pSprite-> runAction(SkewTo::create(1.0f,20,20));
	m_pSprite->runAction(SkewBy::create(1.0f, 20, 20));
}

void ActionTutorial::testJump()
{
	//dt 动画时间 //position 目标点 //height 跳跃的高度 //jumps 跳跃的次数
	float x = m_pSprite->getPositionX();
	float y = m_pSprite->getPositionY();
	m_pSprite->runAction(JumpTo::create(1.0f,Vec2(x,y),50.0f,3));
	//m_pSprite->runAction(JumpBy::create(1.0f, Vec2(x , y), 50.0f, 3));


}


void ActionTutorial::testBlink()
{
	//闪动作执行的时间,和闪的次数
	m_pSprite->runAction(Blink::create(1.0f, 5));

}

void ActionTutorial::testFade()
{
	//out  和 in 渐隐  渐现  一个参数为动作执行的时间 
	m_pSprite->runAction(Sequence::create(
		FadeOut::create(5.0f),
		FadeIn::create(5.0f), nullptr
		));
	//auto testNode = Node::create();
	//auto sprite = Sprite::create("HelloWorld.png");
	//testNode->setPosition(m_pSprite->getPosition());
	//
	//testNode->addChild(sprite);
	//this->addChild(testNode);
	//testNode->set
	//Node没有打开透明度的开关,所以无法执行渐隐,需要打开透明度开关才行
}

void ActionTutorial::testTint()
{
	//m_pSprite->runAction(TintTo::create(5.0f,Color3B::RED));
	
	//GLshort  OPENGL定义
	//改变颜色,指针的话,要先打开颜色开关,否则无法达到目的
	m_pSprite->runAction(TintBy::create(5.0f,255,0,0));

	
	auto testNode = Node::create();
	auto sprite = Sprite::create("HelloWorld.png");
	testNode->setPosition(m_pSprite->getPosition()+Vec2(50.0f,0.0f));
	//testNode->runAction(TintTo::create(5.0f, Color3B::RED));
	testNode->runAction(TintBy::create(5.0f, 255, 0, 0));
	testNode->setCascadeColorEnabled(true);
	testNode->addChild(sprite);
	this->addChild(testNode);
}

void ActionTutorial::testSequence()
{
	Vec2 pos = m_pSprite->getPosition();
	auto delay = Sequence::create(
		Show::create(),nullptr
		);

	auto callbak_0 = [this](){
		log("callback_0");
		
	};

	auto callback_1 = [](Ref * pSpender){
		Sprite * pSprite = dynamic_cast<Sprite*>(pSpender);
		pSprite->setColor(Color3B::WHITE);
	};
	

	auto seq = Sequence::create(
		MoveBy::create(1.0f, Vec2(-100, 0)),
		Hide::create(),
		DelayTime::create(0.5f),
		MoveBy::create(1.0f, Vec2(0, -100)),
		DelayTime::create(0.5f),
		delay->clone(),
		//Place::create(pos),//瞬时动作
		MoveBy::create(1.0f, Vec2(100, 0)),
		DelayTime::create(0.5f),
		MoveBy::create(1.0f, Vec2(0, 100)),
		nullptr
		);
	m_pSprite->runAction(seq);

}

void ActionTutorial::testSpawn()
{
	auto sp = Spawn::create(
		//同时开始,动作执行时间长短与定义的执行时间有关。
		MoveBy::create(10.0f, Vec2(-200.0f, 0)),
		RotateBy::create(5.0f, 360.0f*2.0f),
		TintBy::create(8.0f,255,0,255),
		nullptr
		
		);

	auto sp_1 = sp->reverse();//reverse 为sp的反动作
	auto seq = Sequence::create(sp,sp_1,nullptr);
	m_pSprite->runAction(RepeatForever::create(seq));

}

void ActionTutorial::testEase()
{
	//指数变化
	//EaseExponential
	auto ease_0_in = EaseExponentialIn::create(MoveBy::create(2.0f, Vec2(-300.0f, 0)));
	auto ease_0_out = EaseExponentialOut::create(MoveBy::create(2.0f, Vec2(-300.0f, 0)));
	auto ease_0_in_out = EaseExponentialInOut::create(MoveBy::create(2.0f, Vec2(-300.0f, 0)));

	//EaseSine  sine缓冲

	auto ease_1_in = EaseSineIn::create(MoveBy::create(2.0f, Vec2(-300.0f, 0)));
	auto ease_1_out = EaseSineOut::create(MoveBy::create(2.0f, Vec2(-300.0f, 0)));
	auto ease_1_in_out = EaseSineInOut::create(MoveBy::create(2.0f, Vec2(-300.0f, 0)));
	//EaseElastic 
	//弹性变化
	auto ease_2_in = EaseElasticIn::create(MoveBy::create(2.0f, Vec2(-300.0f, 0)));
	auto ease_2_out = EaseElasticOut::create(MoveBy::create(2.0f, Vec2(-300.0f, 0)));
	auto ease_2_in_out = EaseElasticInOut::create(MoveBy::create(2.0f, Vec2(-300.0f, 0)));
	
	//跳跃

	auto ease_3_in = EaseBounceIn::create(MoveBy::create(2.0f, Vec2(-300.0f, 0)));
	auto ease_3_out = EaseBounceOut::create(MoveBy::create(2.0f, Vec2(-300.0f, 0)));
	auto ease_3_in_out = EaseBounceInOut::create(MoveBy::create(2.0f, Vec2(-300.0f, 0)));
	//回震
	auto ease_4_in = EaseBackIn::create(MoveBy::create(2.0f, Vec2(-300.0f, 0)));
	auto ease_4_out = EaseBackOut::create(MoveBy::create(2.0f, Vec2(-300.0f, 0)));
	auto ease_4_in_out = EaseBackInOut::create(MoveBy::create(2.0f, Vec2(-300.0f, 0)));

	auto seq = Sequence::create(
		ease_4_in_out, TintTo::create(0.1f,Color3B::RED), ease_0_in_out->reverse(), nullptr
		);

	
	m_pSprite->runAction(RepeatForever::create(seq));
}

void ActionTutorial::testFrameAnimation()
{
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值