开发者

QGraphicsItem drawing problem

I am working on a small Tower Defense game in order to learn Qt. I am using a QGraphicsScene to hold all the object of the game. To make them move, I am not using the Animation framework but I'm calling the advance() method along with a QTimer.

I'd like to make my projectiles explode when they hit an enemy. The problem is that when I'm trying to draw an ellipse to simulate the explosion, it doesn't get drawn correctly.

You can see the problem in this video.

I tried to play with the z-indexes but it didn't change anything.

Here's the code I use to draw the projectile :

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'insect开发者_如何学Ce
    return QRectF(0, 0, 6, 6);
}

Do you have any clues on how to solve this problem?

Thank you.


Say cnt is 3. You are drawing an ellipse with

painter->drawEllipse(-3,-3,6,6)

This would require a boundingRect with at least a width and height of 9.
Also the boundingRect is specified using the internal item coordinate system. You are drawing from (-3,-3) to (6,6) which is outside of the boundingRect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜