cocos2dx学习笔记2

前端之家收集整理的这篇文章主要介绍了cocos2dx学习笔记2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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,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,NULL);//一个接一个动作
2.动作监听,请看代码
	MoveTo* moveHome = MoveTo::create(10.0f,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,pos2.x,pos2.y);
		log("pos1 x = %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

猜你在找的Cocos2d-x相关文章