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 the current item is a Coin
object. This is done by trying to cast the pointer to Coin.
If it is successful, we explode the coin by calling explode()
. Calling the explode()
function multiple times is no problem since it...