bool Stick::onTouchBegan(Point pos) { if(_isWorking) return false; if(_bIsLocked == false) { if ( pos.x <= VisibleRect::width()/2 ) { _stick->stopAllActions(); _stick->runAction(FadeIn::create(0.1f)); _frame->stopAllActions(); _frame->runAction(FadeIn::create(0.1f)); if( pos.x - _frame->getContentSize().width/2 <= 0 ) // 考虑边界情况 { _frame->setPositionX(_frame->getContentSize().width/2); _stick->setPositionX(pos.x); } else // 正常情况 { _frame->setPositionX(pos.x); } if(pos.y - _frame->getContentSize().height/2 <= 0) // 考虑边界情况 { _frame->setPositionY(_frame->getContentSize().height/2); _stick->setPositionY(pos.y); } else if(pos.y + _frame->getContentSize().height/2 >= VisibleRect::height()) // 考虑边界情况 { _frame->setPositionY(VisibleRect::height() - _frame->getContentSize().height/2); _stick->setPositionY(pos.y); } else // 正常情况 { _frame->setPositionY(pos.y); } handleTouchChange(pos); return true; } } else { Rect rect = CCRectMake(_frame->getPositionX() - _frame->getContentSize().width/2, _frame->getPositionY() - _frame->getContentSize().height/2, _frame->getContentSize().width, _frame->getContentSize().height); if ( rect.containsPoint(pos) ) { _stick->stopAllActions(); _stick->runAction(FadeTo::create(0.1f,255)); _frame->stopAllActions(); _frame->runAction(FadeTo::create(0.1f,255)); handleTouchChange(pos); return true; } } return false; }
bool Joystick::onTouchBegan(Touch* touch, Event* event) { this->setPosition(touch->getLocation()); //if (this->getBoundingBox().containsPoint(touch->getLocation())) { handleTouchChange(touch); return true; } return false; }
void Stick::onTouchMoved(Point pos) { handleTouchChange(pos); if(_bIsLocked == false) { auto bigR = _frame->getContentSize().width / 2; auto distance = _frame->getPosition().getDistance(pos); if (distance > bigR) { auto posFrame = _frame->getPosition(); if(posFrame.x + 30 <= pos.x) { _frame->setPositionX(posFrame.x + (distance - bigR)); if(_frame->getPositionX() + bigR >= VisibleRect::width()) { _frame->setPositionX(VisibleRect::width() - bigR); } } else if(posFrame.x - 30 >= pos.x) { _frame->setPositionX(posFrame.x - (distance - bigR)); if(_frame->getPositionX() - bigR <= 0) { _frame->setPositionX(bigR); } } if(posFrame.y + 30 <= pos.y) { _frame->setPositionY(posFrame.y + (distance - bigR)); if(_frame->getPositionY() + bigR >= VisibleRect::height()) { _frame->setPositionY(VisibleRect::height() - bigR); } } else if(posFrame.y - 30 >= pos.y) { _frame->setPositionY(posFrame.y - (distance - bigR)); if(_frame->getPositionY() - bigR <= 0) { _frame->setPositionY(bigR); } } } } }
void Joystick::onTouchMoved(Touch* touch, Event* event) { //if (this->getBoundingBox().containsPoint(touch->getLocation())) handleTouchChange(touch); }