下面的代码在 bool Game::init(){ 中。
// on touch ;moved by 10 pix we think it moved //触控设置 //定义: //bool m_startMove;//是否开始移动 //int m_x,m_y;//触摸开始的点 auto event = EventListenertouchOneByOne::create(); event->ontouchBegan = [&](touch*tou,Event *eve){ m_x = tou->getLocation().x; m_y = tou->getLocation().y; m_startMove = true; //cclOG("ontouchBegan m_x = %d,m_y = %d",m_x,m_y); return true; }; event->ontouchmoved = [&](touch*tou,Event *eve){ int x = tou->getLocation().x; int y = tou->getLocation().y; //cclOG("ontouchmoved m_x = %d,x,y); if(m_startMove && (abs(m_x - x) > 10 || abs(m_y - y) > 10)) { //reference OPENGL 坐标 m_startMove = false; E_MOVE_DIR dir; if (abs(m_x - x) > abs(m_y - y)){ //move by X dirction if (m_x < x){ dir = E_MOVE_DIR::RIGHT; cclOG("==E_MOVE_DIR == right"); } else { dir = E_MOVE_DIR::left; cclOG("==E_MOVE_DIR == left"); } } else{ // move by Y direction if (m_y < y){ dir = E_MOVE_DIR::UP; cclOG("==E_MOVE_DIR == up"); } else{ dir = E_MOVE_DIR::DOWN; cclOG("==E_MOVE_DIR == down"); } } moveAllTiled(dir);//移动所有的元素块 } }; Director::getInstance()->getEventdispatcher()->addEventListenerWithSceneGraPHPriority( event,this); return true;// end of Game::init}voID Game::moveAllTiled(E_MOVE_DIR dir){ //移动所有的块,消除 //cclOG("==some of the Tiled have moved!!!"); switch (dir) { case E_MOVE_DIR::UP: moveUp(); break; case E_MOVE_DIR::DOWN: moveDown(); break; case E_MOVE_DIR::left: moveleft(); break; case E_MOVE_DIR::RIGHT: moveRight(); break; default: break; } //music // 判定输赢 //产生新块 newMovedTiled();}
在Game这个层中进行监听触控方向,当移动10个像素的值时,我们认为他移动了,我们就进行相应的动作。
注意函数的写法。
总结以上是内存溢出为你收集整理的【cocos2dx】上下左右简单的触控分析全部内容,希望文章能够帮你解决【cocos2dx】上下左右简单的触控分析所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)