Game Development Reference
In-Depth Information
Time for action - handling multitouches
There are three methods we need to implement in this game to handle touches. Each meth-
od receives, as one of its parameters, a vector of Touch objects:
1. So add our onTouchesBegan method:
void GameLayer::onTouchesBegan(const
std::vector<Touch*> &touches, Event* event)
{
for( auto touch : touches) {
if(touch != nullptr) {
auto tap = touch->getLocation();
for (auto player : _players) {
if (player->boundingBox().containsPoint(tap))
{
player->setTouch(touch);
}
}
}
}
}
Each GameSprite , if you recall, has a _touch property.
So we iterate through the touches, grab their location on screen, loop through the
players in the vector, and determine if the touch lands on one of the players. If so,
we store the touch inside the player's _touch property (from the GameSprite
class).
A similar process is repeated for onTouchesMoved and onTouchesEnded , so
you can copy and paste the code and just replace what goes on inside the _play-
ers array for loop.
2. In TouchesMoved , when we loop through the players, we do this:
for (auto player : _players) {
if (player->getTouch() != nullptr &&
player->getTouch() == touch) {
Point nextPosition = tap;
Search WWH ::




Custom Search