为了学习 Qt,我正在开发一款小型塔防游戏。我正在使用 QGraphicsScene 来保存游戏的所有对象。为了让它们移动,我没有使用动画框架,而是调用了 advance() 方法和 QTimer。
我想让我的射弹在击中敌人时爆炸。问题是当我试图绘制一个椭圆来模拟爆炸时,它没有被正确绘制。
您可以在 this video 中看到问题.
我尝试使用 z-indexes 但它没有改变任何东西。
这是我用来绘制射弹的代码:
void Projectile::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
if(!isExploding) {
painter->drawPixmap(boundingRect().toRect(), image);
} else {
if(cnt < 50) {
painter->setBrush(QBrush(explosion));
painter->drawEllipse(-cnt, -cnt, 2.0*cnt, 2.0*cnt);
cnt++;
} else {
this->isFinished = 1;
}
}
}
QRectF Projectile::boundingRect() const
{
// Taille de l'image de l'insecte
return QRectF(0, 0, 6, 6);
}
关于如何解决这个问题,你有什么线索吗?
谢谢。
最佳答案
假设 cnt 是 3。 你正在画一个椭圆
painter->drawEllipse(-3,-3,6,6)
这需要一个宽度和高度至少为 9 的 boundingRect。
boundingRect 也是使用内部项目坐标系指定的。您正在从 (-3,-3) 绘制到 (6,6),这是在 boundingRect 之外。
关于c++ - QGraphicsItem绘图问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6047985/