使用自定义的QGraphicsItem类进行QT碰撞检测。

4
我正在尝试创建一个子弹类,一旦检测到与敌人类发生碰撞,就会删除该类。我想做的事情是这样的:
void bullet::DoCollision()
{
    if(collidesWithItem(enemy))
    {
        QList<enemy> collisions = collidingItems(enemy);
    }
    //sudo code
    //foreach collision
    //delete enemy
}
//delete myself

我这样做是正确的吗?它们都是QGraphicsItems

1个回答

3
你可以使用QGraphicsItem::collidingItems来返回与该项发生碰撞的所有项的列表。获取列表后,您可以检测碰撞项是否为敌人类型,并在是敌人时将其移除:
QList<QGraphicsItem *> list = collidingItems() ;

foreach(QGraphicsItem * i , list)
{
    enemy * item= dynamic_cast<enemy *>(i);
    if (item)
    {
        myScene->removeItem(item);
    }
}

这对我不起作用。我得到了这个错误 \bullet.cpp:76: error: no matching function for call to 'qobject_cast(QGraphicsItem*&)' enemy * item= qobject_cast<enemy *>(i); - thatboytitz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接