O'Reilly logo

Game Programming Using Qt by Lorenz Haas, Witold Wysota

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required