cocos2dx学习笔记2

本文介绍 Cocos2dx 中角色的动作控制,包括移动、缩放、闪烁等效果实现,并详细讲解了单点及多点触摸事件处理方式,以及如何使用 Cocos2dx 进行微信分享。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 运动类

(1)移动,拉伸,闪烁,重复动作 

	MoveTo* moveTo = MoveTo::create(0.9f, Point(250,150));
<span style="white-space:pre">	</span>MoveBy* moveBy = MoveBy::create(0.9f, Point(100,100));
	sprite->runAction(moveTo);
ScaleTo scaleTo = ScaleTo::create(0.9f, 0.4f, 10.f);
ScaleBy scaleBy = ScaleBu::create(0.9f, 0.4f, 1.0f)
	Blink* blink = Blink::create(3.0f, 5);
	JumpBy* jumpBy = JumpBy::create(3.0f, Point(50, 1), 100, 1);
	RepeatForever* RepeatForeverAction = RepeatForever::create(jumpBy);
	//Repeat* RepeatAction = Repeat::create(jumpBy,5);
	sprite->runAction(jumpBy);
	Action* action = Spawn::create(moveTo,jumpBy,scaleTo,NULL);//几个动作同时做
	Action* action = Sequence::create(moveTo,jumpBy,scaleTo,NULL);//一个接一个动作
2.动作监听,请看代码
	MoveTo* moveHome = MoveTo::create(10.0f,Point(100,100));
	auto callbackFunc[&]()
	{
		log("I fine");
	};
	CallFunc* callFunc = CallFunc::create(callbackFunc);
	Action* actions = Sequence::create(moveHome,callFunc, NULL); 
	sprite->runAction(actions);

3.屏幕触摸事件

	auto listener = EventListenerTouchOneByOne::create();
	listener->onTouchBegan = [](Touch* touch, Event* event)//手指按下时
	{
		Point pos1 = touch->getLocation();//基于3D
		Point pos2 = touch->getLocationInView();//基于2D,左上角为原点
		Point pos3 = Director::getInstance()->convertToGL(pos2);//右下角为原点,笛卡尔坐标,常用的就是这种方式!!!!
		log("pos1 x = %f, y = %f,",pos1.x, pos1.y);
		log("pos1 x = %f, y = %f,",pos2.x, pos2.y);
		log("pos1 x = %f, y = %f,",pos3.x, pos3.y);
		return true;
	};
	listener->onTouchMoved = [](Touch* touch, Event* event)//手指在屏幕拖动时会一直调用
	{
		log("moved");
	};
	listener->onTouchEnded = [](Touch* touch, Event* event)//手指松开时
	{
		log("end");
	};
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);//事件管理器注册监听listener
三个事件,另外一个onTouchCancelled 打断触摸事件,是系统级的消息,如手机来电就会被中断。

4.多点触摸

auto listener = EventListenerTouchAllAtOnce  ::create();
listener->setSwallowTouches(true);//吞没事件,当某个触摸事件回调时,截断这个事件,不让他继续往下传
	listener->onTouchBegan = [](const std::vector<Touch*> &touches, Event* event){};
	listener->onTouchMoved = [](const std::vector<Touch*> &touches, Event* event)
	{
		int num = touches.size();
		for(auto &touch : touches)
		{
			auto location = touch->getLocation();
		}
	};
	listener->onTouchEnded = [](const std::vector<Touch*> &touches, Event* event)
	{
		auto target = static_cast<Sprite*>(event->getCurrentTarget());//这个只有在监听事件时绑定对象才有效,将最后一句中的this改为绑定的对象。
		....
	};
	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);
</pre><pre name="code" class="cpp">
<pre name="code" class="cpp">5.Cocos2dx微信分享


5.微信分享博文链接

http://www.cocoachina.com/bbs/read.php?tid-224616-page-1.html
http://www.cocoachina.com/bbs/simple/?t50484.html
<pre name="code" class="cpp">http://blog.sina.com.cn/s/blog_92ac2c5b0101dgru.html





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值