Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
in this program I have built a QPropertyanimation .and add to it my item and pos () property. I override KeyPressEvent.and with using of keys consist of "j,f,z" item go forward ,go back and jump. According gravity when item jump should fall.for this purpose i call down function .but item just once jump and don't fall .and I also have another problem .when the first press "j" and "f" (forward,back) item animate desirably but for next times item go forward and go back all of scene. I mean It should animated for example 40 pixel but It animated 800 pixel.
C++
<small></small>class MainWindow : public QMainWindow
{
    Q_OBJECT
    
public:
    explicit MainWindow(QWidget *parent = 0);
    QPoint start;
    QPoint end;
    ~MainWindow();
private:
    QGraphicsView* view;
    QGraphicsScene* scene;
    void keyPressEvent(QKeyEvent* k);
    MyQgraphicsObject* m;
    QPropertyAnimation* pr;
    QElapsedTimer* timer;
    int f;
    int u;
    int b;
    void forward();
    void up();
    void back();
    void down();
};
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    view=new QGraphicsView;
    scene=new QGraphicsScene;
    m=new MyQgraphicsObject;
    pr=new QPropertyAnimation(m,"pos");
    view->setScene(scene);
    view->resize(800,800);
    view->setFixedSize(800,800);
    setCentralWidget(view);
    scene->addItem(m);
    start= QPoint(0,0);
    f=30;
    u=-30;
    b=-30;
}
void MainWindow::keyPressEvent(QKeyEvent *k)
{
    switch (k->key())
    {
    case Qt::Key_J:{

        forward();
        break;
    }
    case Qt::Key_Z:
    {
        up();
        down();
        break;
    }
    case Qt::Key_F:
    {
        back();
         break;
    }
    default:
        break;
    }
}

void MainWindow::forward()
{
    end.setX(f);
    pr->setEndValue(end);
    pr->setDuration(1000);
    pr->setEasingCurve(QEasingCurve::Linear);
    pr->start();
    f+=40;
}
void MainWindow::up()
{
    end.setY(u);
    pr->setEndValue(end);
    pr->setDuration(1000);
    pr->setEasingCurve(QEasingCurve::Linear);
    pr->start();
    u-=30;
    pr->pause();
}
void MainWindow::back()
{
    end.setX(b);
    pr->setEndValue(end);
    pr->setDuration(1000);
    pr->setEasingCurve(QEasingCurve::Linear);
    pr->start();
    b-=40;
}
void MainWindow::down()
{
    u+=30;
    end.setY(u);
    pr->setEndValue(end);
    pr->setDuration(1000);
    pr->setEasingCurve(QEasingCurve::Linear);
    pr->start();
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900