cocos2dx自学之 事件监听

前端之家收集整理的这篇文章主要介绍了cocos2dx自学之 事件监听前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
auto label = LabelTTF::create("click me!","Courier",30);
	label->setPosition(visibleSize.width/2,visibleSize.height/2);

	addChild(label);
	auto listener = EventListenerTouchOneByOne::create(); //创建监听器
	listener->onTouchBegan = [](Touch *touch,Event *event){  //设置监听函数
		
		if(event->getCurrentTarget()->getBoundingBox().containsPoint(touch->getLocation())){ //判断点击范围是在label上
			log("onTouched!!!!!!!!!!!!");

		}
		return true;  //返回true才能出发事件传递
	};

	//事件传递
	listener->onTouchMoved = [](Touch* touch,Event *event){ //拖动事件   onTouchEnded = 触摸弹起事件
		log("onTouchmoved!");
		return true;//返回 true  才可以继续传递事件
	};

	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,label);  //绑定监听事件 */
	//多点触摸监听器******************************************
	/*auto listener = EventListenerTouchAllAtOnce::create();
	
	listener->onTouchesBegan = [](std::vector<Touch*> t,Event *event){ //单击事件
		log("onTouchesBegan");
	};
	listener->onTouchesMoved = [](std::vector<Touch*> t,Event *event){ //多点同时触摸事件
		log("touches moved,and touch count is%ld",t.size());
	};
	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,this); */


	/****************************  传感器  *****************************************/
	/*Device::setAccelerometerEnabled(true);//打开传感器侦听  传感器=重力感应?
	auto listener = EventListenerAcceleration::create([](Acceleration *a,Event *event){
		log("x:%g,y:%g,z:%g",a->x,a->y,a->z);
	});
	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,this);*/

	/**************************  按键监听  ************************************/
	/*auto listener = EventListenerKeyboard::create();
	listener->onKeyReleased = [](EventKeyboard::KeyCode code,Event *venet){ //监听按键事件
		log("key:%d",code);
		switch(code){  
		case EventKeyboard::KeyCode::KEY_BACKSPACE: //如果是后退键
			Director::getInstance()->end();   //退出当前程序
				break;
		}
	};

	Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,this);
原文链接:https://www.f2er.com/cocos2dx/342111.html

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