- Game Programming using Qt 5 Beginner's Guide
- Pavel Strakhov Witold Wysota Lorenz Haas
- 122字
- 2021-08-27 18:31:11
Time for action - Moving the background
The scene will create a graphics item for each part of the background and store pointers to them in the m_sky, m_grass, and m_trees private fields. Now the question is how to move them at different speeds. The solution is quite simple—the slowest one, the sky, is the smallest image. The fastest background, the ground and the grass, are the largest images. Now when we take a look at the end of the movePlayer() function's slot, we see this:
qreal ratio = qreal(m_worldShift) / maxWorldShift; applyParallax(ratio, m_sky); applyParallax(ratio, m_grass); applyParallax(ratio, m_trees);
The applyParallax() helper method contains the following code:
void MyScene::applyParallax(qreal ratio, QGraphicsItem* item) { item->setX(-ratio * (item->boundingRect().width() - width())); }