Time for action – making the coins explode

The implementation of checkColliding() looks like this:

QList<QGraphicsItem*> items =  collidingItems(m_player);
for (int i = 0, total = items.count(); i < total; ++i) {
  if (Coin *c = qgraphicsitem_cast<Coin*>(items.at(i)))
    c->explode();
}

What just happened?

First we call the scene's QGraphicsScene::collidingItems() function, which takes the item for which colliding items should be detected as a first argument. With the second, optional argument, you could define how the collision should be detected. The type of that argument is Qt::ItemSelectionMode, which was explained earlier. In our case, a list of all the items that collide with m_player will be returned. So we loop through that list and check whether ...

Get Game Programming Using Qt now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.