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 = 触摸d起事件 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);总结
以上是内存溢出为你收集整理的cocos2dx自学之 事件监听全部内容,希望文章能够帮你解决cocos2dx自学之 事件监听所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)