下面的代码在 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个像素的值时,我们认为他移动了,我们就进行相应的动作。
注意函数的写法。
原文链接:https://www.f2er.com/cocos2dx/345236.html